Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why did wait4() get replaced by waitpid()

I was going through the documentation of the system call wait4() and in its man page it is written

These functions are obsolete; use waitpid(2) or waitid(2) in new programs.

So, I went through the documentation of waitpid() and I saw that there is a difference between the two.

waitpid() does the same things as wait4(), but wait4(), according to the man page,

additionally return resource usage information about the child in the structure pointed to by rusage.

The two system calls are defined as follows

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

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

Now, I also read that there is a another system call that does that extra job of getting the rusage of the child and that is getrusage().

So, I can understand that what wait4() could do, one can do the same thing by using a combination of waitpid() and getrusage().


But, what I am not understanding is, there is always a strong reason for making a system call obsolete. But in this case it feels that the functionality has been split.

  • If I want to use the combination of waitpid() and getrusage(), I have to check the return values twice, which was not the case for wait4().
  • Additionally one can use wait4() to get rusage of a specific child, but waitpid() would gives rusage of all its children together (if used with RUSAGE_CHILDREN). That sounds like extra overhead if there are more then few child processes.

Why was wait4() made obsolete? It seems like it made things harder.

like image 631
Haris Avatar asked Feb 10 '16 13:02

Haris


People also ask

What does Waitpid () return?

Returned value If successful, waitpid() returns a value of the process (usually a child) whose status information has been obtained. If WNOHANG was given, and if there is at least one process (usually a child) whose status information is not available, waitpid() returns 0.

What is the difference between wait () and waitpid ()?

Difference between wait and waitpid():Wait() waits for any child process but waitpid() waits for a specific child equal to pid. By default waitpid() waits for the only terminated child where as wait() waits for both terminated or a signaled child.

What is Waitpid?

The waitpid() function allows the calling thread to obtain status information for one of its child processes. The calling thread suspends processing until status information is available for the specified child process, if the options argument is 0.


1 Answers

It is a matter of standardization and history. wait4 is a 4.3BSD system call, but POSIX.1 retained waitpid.

like image 129
Jean-Baptiste Yunès Avatar answered Sep 29 '22 12:09

Jean-Baptiste Yunès