为什么wait4()被waitpid()

时间:2016-02-10 13:18:21

标签: c linux wait system-calls waitpid

我正在阅读系统调用wait4()的文档,并在其手册页中编写了

  

这些功能已经过时;在新计划中使用waitpid(2)waitid(2)

所以,我浏览了waitpid()的文档,我发现两者之间存在差异。

waitpid()wait4()的内容相同,但根据手册页<{3}} {/ 3>}

  

另外返回rusage指向的结构中有关子项的资源使用信息。

两个系统调用定义如下

pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);

pid_t waitpid(pid_t pid, int *status, int options);

现在,我还读到还有另一个系统调用来完成获取孩子rusage的额外工作,那就是wait4()

因此,我可以理解wait4()可以做什么,通过使用waitpid()getrusage()的组合可以做同样的事情。

但是,我不理解的是,总是有一个强大的理由让系统调用过时。但在这种情况下,它感觉功能已被拆分。

  • 如果我想使用waitpid()getrusage()的组合,我 必须检查两次返回值,但情况并非如此 wait4()
  • 此外,可以使用wait4()来获取特定的rusage 孩子,但waitpid()会给出所有孩子的rusage 在一起(如果与RUSAGE_CHILDREN一起使用)。如果有更多的子进程,这听起来像额外的开销。

为什么wait4()已过时?这似乎让事情变得更难。

2 个答案:

答案 0 :(得分:3)

这是一个标准化和历史问题。 wait4是一个4.3BSD系统调用,但POSIX.1保留waitpid

答案 1 :(得分:1)

取自http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html

The waitpid() function shall be equivalent to wait() if the pid argument is 
(pid_t)-1 and the options argument is 0. Otherwise, its behavior shall be 
modified by the values of the pid and options arguments

所以提供waitpid()函数有三个原因:

To support job control

To permit a non-blocking version of the wait() function

To permit a library routine, such as system() or pclose(), to wait for its 
children without interfering with other terminated children for which the 
process has not waited

它还包括所有先前的wait()功能,正如所解释的那样