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
     }
 }

の2番目のパラメータが何に使用されているのか誰かに説明してもらえます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 に答える