I tried to run the following command kill -9 1 and it says bash: kill: (1) - Operation not permitted.
It was pretty obvious to me that you should not be able to signal the init process without sudo.
But while writing the code for c-shell i encountered a bug(i think it really is). I compiled the following program and ran it. Now it has confused me and all my OS concepts.
#include <signal.h>
int main()
{
killpg(1,9);
return (0);
}
Please save all your programs and run the code yourself.
Can anyone give me a reason and clarify my confusion.
UPDATE
Man page of killpg() read as...
On Linux, killpg() is implemented as a library function that makes the call kill(-pgrp, sig).
Man page of kill() read as...
A PID of -1 is special; it indicates all processes except the kill process itself and init.
Now the question is, what is the use of such a call that literally kills everything. It has many many dangerous applications rather than useful ones. But still since it has been kept in the linux kernel since so many years then it must have its own usefulness. But i can't figure out any. Does anyone know anything about it?
If pgrp is 0, killpg() sends the signal to the calling process's process group. (POSIX says: If pgrp is less than or equal to 1, the behavior is undefined.