没有 sudo 的 SIGKILL init(PID=1)? linux 中的错误?

SIGKILL init(PID=1) without sudo? Bug in linux?

我尝试 运行 以下命令 kill -9 1,它显示 bash: kill: (1) - Operation not permitted

对我来说很明显,如果没有 sudo,您将无法向 init 进程发出信号。

但是在为 c-shell 编写代码时,我遇到了一个错误(我认为确实如此)。我编译了以下程序并 运行 它。现在它让我和我所有的 OS 概念都感到困惑。

#include <signal.h>
int main()
{
    killpg(1,9);
    return (0);
}

Please save all your programs and run the code yourself.

谁能给我个理由,解开我的困惑。

更新
killpg() 的手册页读作...

On Linux, killpg() is implemented as a library function that makes the call kill(-pgrp, sig).

kill() 的手册页阅读为...

A PID of -1 is special; it indicates all processes except the kill process itself and init.

现在的问题是,这样一个字面上杀死一切的调用有什么用。它有许多危险的应用程序而不是有用的应用程序。但既然它一直保存在 linux 内核中这么多年,那么它一定有它自己的用处。但我想不通。有人知道吗?

来自手册页:

(POSIX says: If pgrp is less than or equal to 1, the behaviour is undefined.)

所以如果你这样做,你不能依赖任何特定的行为。

来自 killpg 的 Linux 手册页:

On Linux, killpg() is implemented as a library function that makes the call kill(-pgrp, sig).

来自 kill 的 Linux 手册页:

If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init)

所以你 运行 进入了一个特例,其中 killpg(1, 9) 实际上并不意味着发送 SIGKILL 到 pgrp 1,而是发送 SIGKILL 由于实施的怪癖,它有权访问所有内容。正如其他人指出的那样,POSIX 在第一个参数为 1 时没有指定 killpg 的行为,因此可以说这不是错误。