c - 在 Linux 中从 C 杀死所有进程

标签 c linux process signals

我正在编写一个将作为 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/

相关文章:

C: 关闭多线程程序

django - 安装 django-CMS 后 .iPython 的权限被拒绝

linux - 使用 netcat 和 grep 有条件地运行命令

ruby-on-rails - rails 使用用户输入运行 shell 命令

c# - 在 perl 和 c# 中使用套接字在本地计算机上进行进程间通信

c - 在 C(不是 C++)中使用 fork() 从 1 个父项中生成 3 个子项

c++ - cmake 构建 .so 库

c++ - 控制台中的 OutputDebugString()

c# - 面向对象的概念适用于C语言吗?

c# - 我想打开一个excel文件并等待用户编辑并保存它