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.
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.
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.
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.
It is a matter of standardization and history. wait4
is a 4.3BSD system call, but POSIX.1 retained waitpid
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With