why did wait4() get replaced by waitpid()
Asked Answered
B

2

7

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.

Byte answered 10/2, 2016 at 13:18 Comment(3)
Performance perhaps? Sometimes you do not want to get resource usage. Also better to have a function just do one thing.Sweyn
@EdHeal, But there is a way to use wait4() without getting rusage. One has to make the last parameter NULL.Byte
@Byte Now I wonder which "rusage()" function wait4() is using since it's not accessible via the standard rusage() function. I know that we can find the info in the /proc file system, but I'd say that's even worse!Bushman
T
3

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

Trooper answered 10/2, 2016 at 18:22 Comment(2)
retained? Is waitpid() also an old system call or was it introduced later?Byte
Can't remember exactly but both are old. If think waitpid is a litlle bit more recent than wait3/wait4??? It was very common at this time that BSD leads innovation, POSIX standardization was a difficult task...Marchak
A
1

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

Alanaalanah answered 10/2, 2016 at 15:59 Comment(1)
I think the reasons mentioned can also be achieved using wait() variants.Byte

© 2022 - 2024 — McMap. All rights reserved.