Tutorial :How can I kill all processes of a program?



Question:

I wrote a program that forks some processes with fork(). I want to kill all child- and the mother process if there is an error. If I use exit(EXIT_FAILURE) only the child process is killed.

I am thinking about a system("killall [program_name]") but there must be a better way...

Thank you all! Lennart


Solution:1

Under UNIX, send SIGTERM, or SIGABRT, or SIGPIPE or sth. alike to the mother process. This signal will then be propagated to all clients automatically, if they do not explicitely block or ignore it.

Use getppid() to get the PID to send the signal to, and kill() to send the signal.

getppid() returns the process ID of the parent of the calling process.

The kill() system call can be used to send any signal to any process group or process.

Remarks: 1. Using system is evil. Use internal functions to send signals. 2. killall would be even more evil. Consider several instances of your program running at once.


Solution:2

See How to make child process die after parent exits?

On Linux there's a prctl() call which is explicitly designed to send a signal to all of a process's children when the parent dies for whatever reason.

I need to check and can't do it where I am at the second, but I'm really not sure that ypnos' assertion about SIGPIPE, SIGTERM and SIGABRT being propagated to all children is correct.

However if you use kill(-ppid) (note the minus sign) then so long as the children are still in the parent process's process group then the kernel will deliver any signal to all of the children.


Solution:3

If your mother process is not started by the command line, it may not be the process group leader, like as a deamon.

To ensure that your mother process is the process group leader, call setsid() during your process initialization.

Then in your child process, if you want to cause all the processes to exit:

pgid = getpgid();
kill(pgid, 15);

You can also do tricks, like telling all your siblings to suspend:

kill(pgid, 20);

And resume:

kill(pgid, 18);


Solution:4

Consider suicidal approach - setting up an alarm() at the beginning of the process (both parent and child) with some positive number of seconds. If computation completes within that time and "there is no error", call alarm(0) to cancel the timer; otherwise the SIGALRM will kill the process (assuming you're not explicitly catching or ignoring it.)

Well, make a case against this instead of just down-voting :)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »