Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 486
Billy Avatar asked Mar 08 '26 02:03

Billy


1 Answers

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.

like image 156
Jim Janney Avatar answered Mar 09 '26 17:03

Jim Janney