Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a wait() system call?

Hello I am new to learning about system calls. I am currently learning about fork() and wait() system calls. I know that fork() creates a new child process. What confuses me is the wait() call.
This is what I understand so far:
(1) When a process dies, it goes into a 'Zombie State' i.e. it does not release its PID but waits for its parent to acknowledge that the child process has died and then the PID is released
(2) So we need a way to figure out when the child process has ended so that we don't leave any processes in the zombie state

I am confused with the following things:
(1) When running a C program where I fork a new child process, if I don't call wait() explicitly, is it done internally when the child process ends? Because you could still write a block of code in C where you run fork() without wait() and it seems to work fine?
(2) What does wait() do? I know it returns the PID of the child process that was terminated, but how is this helpful/related to releasing the PID of the terminated process?

I am sorry for such naive questions but this is something I was really curious about and I couldn't find any good resources online! Your help is much appreciated!

like image 591
JANVI SHARMA Avatar asked Sep 07 '26 20:09

JANVI SHARMA


1 Answers

wait isn't about preventing zombie states. Zombie states are your friend.

POSIX more or less lets you do two things with pids: signal them with kill or reap them (and synchronize with them) with wait/waitpid/waittid.

The wait syscalls are primarily for waiting on a process to exit or die from a signal (though they can also be used to wait on other process status changes such as the child becoming stopped or the child waking up from being stopped).

Secondarily, they're about reaping exit/died statuses, thereby releasing (zombified) pids.

Until you release a pid with wait/waitpid/waittid, you can continue flogging the pid with requests for it to die (kill(pid,SIGTERM);) or with some other signal and you can rest assured the pid represents the process you've forked off and that you're not accidentally killing someone else's process.

But once you reap a zombified pid by waiting on it, then the pid is no longer yours and another process might've taken it (which typically happens after some time, as pids in the system typically increment and then wrap arround).

That's why auto-wait would be a bad idea (in some cases it isn't and then you can achieve it with globally with signal(SIGCHLD,SIG_IGN);) and why (short-lived) zombies states are your friend. They keep the child pid stable for you until you're ready to release it.

If you exit without releasing any of your children's pids, then you don't have to worry about zombie children anymore--your child processes will be reparented to the init process, which will wait on them for you when they die.

like image 187
PSkocik Avatar answered Sep 10 '26 16:09

PSkocik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!