我正在编写一个将作为 init 执行的 Linux C 程序。它最终将需要关闭系统。我有卸载所有文件系统并实际关闭系统的代码;现在我只需要一种方法让它发送 SIGTERM
到所有进程,sleep(5)
,然后发送 SIGKILL
到任何剩余的进程。
最佳答案
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.
对 pid 使用 -1 会将信号发送到调用进程有权访问的每个进程,不包括进程 1,例如
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
...
kill(-1, SIGTERM);
sleep(5);
kill(-1, SIGKILL);
谨慎使用。
关于c - 在 Linux 中从 C 杀死所有进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52013111/