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)
orwaitid(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.
waitpid()
and getrusage()
, I
have to check the return values twice, which was not the case for
wait4()
.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.
It is a matter of standardization and history. wait4
is a 4.3BSD system call, but POSIX.1 retained waitpid
.
Taken from 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
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