Why Did Wait4() Get Replaced by Waitpid()

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.

3

2 Answers

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

2

Taken from :

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

so the waitpid() function is provided for three reasons:

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

And it also include all previous abilities of wait() as explained

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.