views:

35

answers:

2

The main process in my program forks 3 more process with say process ids as pid1, pid2, pid3. Pid1 and pid2 processes are in infinite loop. What I want is when pid3 process is over all the process including the main are terminated. As of now, I am using :

wait(pid3);
kill(0, SIGKILL);

which do all above as i said, but it prints Killed on the terminal. I don't want the signal to display killed and infact nothing, but gracefully exits from the program. How can I do that?

+4  A: 

Use SIGTERM instead, and install a signal handler that performs a clean exit (e.g. exit(0)) on receiving SIGTERM.

R..
Actually, since you may want to allow the caller of the program to detect if the user killed it with `SIGTERM` (normal behavior of the `kill` command), it might be better to use a separate signal, like `SIGUSR1`, for notifying the other processes that they should exit.
R..
+1  A: 

An alternate method is to set up a pipe() between the processes, and have the pid1 and pid2 processes exit when they see end-of-file on their pipe.

caf