I am writing a server. It uses fork()
to create new child processes. I want to kill the child processes when a parent process dies. I am planning to use an array to store the pids of the child processes.But there is chance that the child process might terminate before the parent process. In this case if we call kill
function using SIGKILL
and pid of the child process that already terminated, will it throw an exception?
Asked
Active
Viewed 614 times
0

cmm user
- 2,426
- 7
- 34
- 48
-
@n.m Thanks for the response.The wait() system call suspends execution of the calling process until one of its children terminates.But I don't want the execution of the calling process to wait until the child is terminated. – cmm user Feb 27 '16 at 08:16
-
1Read more carefully, pay atentiopn to `WNOHANG`. – n. m. could be an AI Feb 27 '16 at 08:18
1 Answers
3
When a child process terminates, a SIGCHLD
signal is sent to the parent. You can handle this signal in the parent to check what child has terminated on it's own, before you got a chance to wait
for it. And you should wait()
for all children to avoid zombie processes.
You can also take a look at this thread for an alternative way to have the children terminate when the parent terminats: How to make child process die after parent exits?