0

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?

cmm user
  • 2,206
  • 6
  • 33
  • 47

1 Answers1

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?

Community
  • 1
  • 1
Ionut
  • 5,838
  • 1
  • 15
  • 17