C fork(),wait4()问题

C fork(),wait4()问题,c,linux,fork,C,Linux,Fork,我试图用wait4对fork'ed进程计时,但是stime和utime的rusage返回值始终为零。我已经验证了当我执行一个只占用时钟周期几秒钟的测试进程时,该进程是否正确地进行了fork,exec,并且rusage值是否正常(>0) 我怀疑正在使用的时钟分辨率不足以为短进程正确计时,但是a)对于查询的所有clockid\t类型,我都得到1ns的响应,并且b)我看不到任何方法来指定应将哪个时钟用于wait4() 这是我的密码: double spawnprocess(char *arg1, ch

我试图用
wait4
fork
'ed进程计时,但是
stime
utime
的rusage返回值始终为零。我已经验证了当我执行一个只占用时钟周期几秒钟的测试进程时,该进程是否正确地进行了
fork
exec
,并且rusage值是否正常(
>0

我怀疑正在使用的时钟分辨率不足以为短进程正确计时,但是a)对于查询的所有
clockid\t
类型,我都得到
1ns
的响应,并且b)我看不到任何方法来指定应将哪个时钟用于
wait4()

这是我的密码:

double spawnprocess(char *arg1, char *arg2){
  struct rusage usage;
  struct timeval time;
  double t1 = 0.0;
  int nogo;
  pid_t pid;
  char* args[] = { "/path/to/process", arg1, arg2, (char *) 0 };

  do {
    pid = fork();
    if (pid == -1) { printf("Error forking... trying again.\n"); sleep(1); }
  } while (pid == -1);

  if (pid == 0) {
    if ((nogo=execv(args[0], args)) == -1) printf("Error starting new process.\n");
  } else {
    printf("Waiting for child %d...\n", pid);
    int status;
    int options = 0;
    pid_t cpid = wait4(pid, &status, options, &usage);
    if (cpid != pid) {
      printf("Error: %d\n", cpid);
      int tmp;
      if (WIFEXITED(status)) printf("%s", "WIFEXITED");
      if ((tmp = WEXITSTATUS(status))) printf("WEXITSTATUS: %d", tmp);
      if (WIFSIGNALED(status)) printf("%s", "WIFSIGNALED");
      if ((tmp = WTERMSIG(status))) printf("WTERMSIG: %d", tmp);
      if (WCOREDUMP(status)) printf("%s", "Core dumped.");
      if (WIFSTOPPED(status)) printf("%s", "WIFSTOPPED");
      if (WSTOPSIG(status)) printf("%s", "WSTOPSIG");
      if (WIFCONTINUED(status)) printf("%s", "WIFCONTINUED");
    }  else {
      t1 = (usage.ru_utime.tv_sec + (usage.ru_utime.tv_usec/1000000.0));
    }
  }
printf("Sent value is: %e\n", t1);
return t1;
}
系统计时器可能具有纳秒分辨率,但您需要一个专为实时操作而设计的系统,以充分避开您的干扰,使您能够看到这种计时分辨率。我在这里的非Linux系统上尝试了你的代码。在奔腾III上运行的旧FreeBSD系统有时会打印0,有时会打印4e-03。较新的PowerPC和Intel Mac打印4e-05至2e-04范围内的数字。

系统计时器可能具有纳秒分辨率,但您需要一个专为实时操作设计的系统,以充分避开您的干扰,使您能够看到这种计时分辨率。我在这里的非Linux系统上尝试了你的代码。在奔腾III上运行的旧FreeBSD系统有时会打印0,有时会打印4e-03。较新的PowerPC和Intel Mac打印4e-05至2e-04范围内的数字