Why do we need a wait() system call?
Asked Answered
E

2

4

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!

Echolocation answered 29/9, 2020 at 6:51 Comment(0)
D
6

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 (other then SIGKILL) 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 take 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.

Delogu answered 29/9, 2020 at 23:49 Comment(2)
Thank you for your reply! this is quite helpful! quick question then: if we are using the wait9) call to wait for the child process to finish, i was wondering why it only waits for one child to finish instead of waiting for all its children to finish? anyways, I will read more into it! thanks for your helpEcholocation
You're welcome. Because the designers of UNIX decided that to wait on all processes, you should call wait in a loop until you get ECHILD.Delogu
M
2

When you call fork(), a new process is created with you being its parent. When the child process finishes its running with a call to exit(), its process descriptor is still kept in the kernel's memory. It is your responsibility as its parent to collect its exit code, which is done with a call to wait() syscall. wait() blocks the parent process until one of its childrens is finished.

Zombie process is the name given to a process whose exit code was never collected by its parent.

Regarding to your first question - wait() is not called automatically as zombie processes wouldn't exist if it did. It is your responsibility as a programmer. Omitting the call to wait() will still work as you mentioned - but it is considered a bad practice.

Both this link and this link explains it good.

Marnimarnia answered 29/9, 2020 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.