5

从这里的一个现有问题,有人给出了这个示例代码:

int status;
child_pid = fork();
if (child_pid == 0) {
     // in child; do stuff including perhaps exec
} else if (child_pid == -1) {
     // failed to fork 
} else {
     if (waitpid(child_pid, &status, 0) == child_pid) {
          // child exited or interrupted; now you can do something with status
     } else {
          // error etc
     }
 }

谁能向我解释第二个参数的waitpid()用途是什么?

4

3 回答 3

8

来自手册页:

   If  status is not NULL, wait() and waitpid() store status infor-
   mation in the int to which  it  points.   This  integer  can  be
   inspected  with  the  following  macros  (which take the integer
   itself as an argument, not a pointer to it, as is done in wait()
   and waitpid()!):

   WIFEXITED(status)
          returns  true  if the child terminated normally, that is,
          by calling exit(3) or  _exit(2),  or  by  returning  from
          main().

   WEXITSTATUS(status)
          returns  the  exit status of the child.  This consists of
          the least significant 8 bits of the status argument  that
          the  child  specified in a call to exit(3) or _exit(2) or
          as the argument for a return statement in  main().   This
          macro should only be employed if WIFEXITED returned true.

   WIFSIGNALED(status)
          returns true if the child process  was  terminated  by  a
          signal.

   WTERMSIG(status)
          returns  the  number  of the signal that caused the child
          process to terminate.  This macro should only be employed
          if WIFSIGNALED returned true.

   WCOREDUMP(status)
          returns  true  if  the  child produced a core dump.  This
          macro should only be  employed  if  WIFSIGNALED  returned
          true.  This macro is not specified in POSIX.1-2001 and is
          not available on some Unix  implementations  (e.g.,  AIX,
          SunOS).   Only  use this enclosed in #ifdef WCOREDUMP ...
          #endif.

   WIFSTOPPED(status)
          returns true if the child process was stopped by delivery
          of  a  signal; this is only possible if the call was done
          using WUNTRACED or when the child is  being  traced  (see
          ptrace(2)).

   WSTOPSIG(status)
          returns  the  number of the signal which caused the child
          to stop.  This macro should  only  be  employed  if  WIF-
          STOPPED returned true.

   WIFCONTINUED(status)
          (since  Linux  2.6.10)  returns true if the child process
          was resumed by delivery of SIGCONT.

因此它存储“孩子如何终止”的状态。

您可以使用宏来调查孩子究竟是如何终止的,并且您可以根据孩子的终止状态定义一些操作。

于 2012-08-06T09:54:10.387 回答
3

它是选项的位域,唯一可用的是 WNOWAIT,意思是让孩子处于等待状态;稍后的等待调用可用于再次检索子状态信息。

见: http: //linux.die.net/man/2/waitpid

于 2012-08-06T08:16:57.927 回答
2
      pid = fork();
      if(pid < 0)
      {
        printf("fork failed\n");
        return -1;
      }
      else if(pid == 0)
      {
        sleep(5);
        printf("Child process\n");
        return 2;
      }
      else
      {
        printf("Parent process\n");
        kill(pid, SIGKILL);
        waitpid(pid, &ret, 0);
        if(WIFEXITED(ret))
          printf("Child process returned normally\n");
        if(WIFSIGNALED(ret))
          printf("Child process terminated by signal\n");
        return 1;
      }

如您所见,返回值可用于检查特定进程如何终止并在此基础上采取措施。

如果您注释代码中的 kill 行,子进程将正确终止。

于 2015-07-30T16:20:44.590 回答