child 可以被 not-parent 进程杀死吗?
Can a child be killed by a not-parent process?
我正在做一个项目,其中 parent 派生一个 terminator 进程,随机杀死一个 child 的 parent。看来这会造成一些问题。
允许吗?
这是允许的。在parent.sh
中写入如下代码
terminator() {
sleep 2;
echo "(terminator) Going to kill Pid "
kill -9 "" && echo "(terminator) Pid killed"
}
sleep 7 &
sleep 7 &
sleep 7 &
pid=$!
echo "Random pid=${pid} will be killed"
sleep 7 &
sleep 7 &
terminator ${pid} &
echo "All started"
ps -ef | sed -n '1p; /sleep 7/p'
sleep 3
echo "After kill"
ps -ef | sed -n '1p; /sleep 7/p'
后台进程是child。终结者 child 会在 2 秒后杀死一个随机的其他 child。
Random pid=6781 will be killed
All started
UID PID PPID C STIME TTY TIME CMD
notroot 6779 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6780 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6781 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6782 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6783 6777 0 16:59 pts/0 00:00:00 sleep 7
(terminator) Going to kill Pid 6781
(terminator) Pid 6781 killed
parent.sh: line ...: 6781 Killed sleep 7
After kill
UID PID PPID C STIME TTY TIME CMD
notroot 6779 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6780 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6782 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6783 6777 0 16:59 pts/0 00:00:00 sleep 7
不仅如此:任何进程 运行 作为同一用户或 root 用户都可以终止任何其他进程。
运行 作为我在终端中的用户 ID,我可以使用我的用户 ID 杀死任何东西。甚至是我 运行 所在的终端。或者我的 GUI 进程。
Unix OS 类型中唯一不会被杀死的进程是 PID 1 aka init。因为杀死 init 会立即导致内核恐慌。如果 PID 1 由于任何原因退出,例如内部错误和分段错误,则会立即出现内核恐慌。
我正在做一个项目,其中 parent 派生一个 terminator 进程,随机杀死一个 child 的 parent。看来这会造成一些问题。
允许吗?
这是允许的。在parent.sh
terminator() {
sleep 2;
echo "(terminator) Going to kill Pid "
kill -9 "" && echo "(terminator) Pid killed"
}
sleep 7 &
sleep 7 &
sleep 7 &
pid=$!
echo "Random pid=${pid} will be killed"
sleep 7 &
sleep 7 &
terminator ${pid} &
echo "All started"
ps -ef | sed -n '1p; /sleep 7/p'
sleep 3
echo "After kill"
ps -ef | sed -n '1p; /sleep 7/p'
后台进程是child。终结者 child 会在 2 秒后杀死一个随机的其他 child。
Random pid=6781 will be killed
All started
UID PID PPID C STIME TTY TIME CMD
notroot 6779 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6780 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6781 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6782 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6783 6777 0 16:59 pts/0 00:00:00 sleep 7
(terminator) Going to kill Pid 6781
(terminator) Pid 6781 killed
parent.sh: line ...: 6781 Killed sleep 7
After kill
UID PID PPID C STIME TTY TIME CMD
notroot 6779 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6780 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6782 6777 0 16:59 pts/0 00:00:00 sleep 7
notroot 6783 6777 0 16:59 pts/0 00:00:00 sleep 7
不仅如此:任何进程 运行 作为同一用户或 root 用户都可以终止任何其他进程。
运行 作为我在终端中的用户 ID,我可以使用我的用户 ID 杀死任何东西。甚至是我 运行 所在的终端。或者我的 GUI 进程。
Unix OS 类型中唯一不会被杀死的进程是 PID 1 aka init。因为杀死 init 会立即导致内核恐慌。如果 PID 1 由于任何原因退出,例如内部错误和分段错误,则会立即出现内核恐慌。