2012-08-06 106 views
4

从存在的问题here,有人给了这个例子代码:waitpid()的第二个参数是什么意思?

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()第二个参数是用来做什么?

回答

2

这是一个位域的选项,唯一可用的是WNOWAIT,这意味着让孩子处于等待状态;稍后的等待呼叫可以用于再次检索儿童状态信息。

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

+0

感谢您的快速回复,我问的是'&status'部分,对于 – mko 2012-08-06 08:27:03

+0

使用的状态参数有什么抱歉,我错过了,它是程序的当前状态,您可以使用宏来测试每个条件,如WIFEXITED和WIFSIGNALED。有关这些信息,请参阅相同的URL。 – Geoffrey 2012-08-06 08:31:09

+0

得到它,waitPid实际存储在该变量中的状态, – mko 2012-08-06 08:38:03

6

从手册页:

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. 

那么它存储的 “孩子如何终止” 状态。

您可以使用宏来调查孩子终止的确切程度,您可以根据孩子的终止状态定义一些操作。

+0

+1对于很好的解释'孩子如何终止',如果状态是一个int(如1,2,3,4,5),每个都表示终止状态,为什么声明它为pid_t类型,而不是int类型 – mko 2012-08-06 10:06:22

+1

可移植性问题,它隐藏在typedef下。因此,平台的本地整数类型可以不同,而不会破坏使用像fork()或waitpid()这样的过程控制系统调用的代码。 – Aftnix 2012-08-06 11:33:26

+0

哇我想我需要了解更多关于线程编程的知识。听起来真的很深刻,很有趣 – mko 2012-08-07 02:34:12

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行,子进程将正常终止。