Skip to content
Advertisement

Kill all processes from C in Linux

I am writing a Linux C program that will be executed as init. It will eventually need to shut down the system. I have code for unmounting all the filesystems and actually turning off the system; now I just need a way for it to send SIGTERM to all processes, sleep(5), then send SIGKILL to any remaining processes.

Advertisement

Answer

If pid is -1: If the user has super-user privileges, the signal is sent to all processes excluding system processes and the process sending the signal. If the user is not the super user, the signal is sent to all processes with the same uid as the user, excluding the process sending the signal. No error is returned if any process could be signaled.

Using -1 for the pid will send the signal to every process that the calling process has permission for, excluding process 1, e.g.

#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
...
kill(-1, SIGTERM);
sleep(5);
kill(-1, SIGKILL);

Use with caution.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement