Kill all processes in the process group

Process group

concept
    在 linux 中,进程不但有自己的 id 号,还会有一个组 id 号。我们把拥有相同组 id 号的进程的集合称为一个进程组。
Send signals to processes and process groups
使用 kill -n -pgid 可以将信号 n 发送到进程组 pgid 中的所有进程。例如命令 kill -9 -4115 表示杀死进程组 4115 中的所有进程
使用kill  -n pid 可以将信号n发送到指定pid进程。
The life cycle of the process group
从进程组被创建开始,到组内所有进程终止或者离开。
Process group leader
进程组组长是进程组中的某一个进程。该进程的进程号等于进程组的进程号。需要注意的是,进程组可以没有组长。如进程 ps_swing,它的进程组组长 4239 已经被 kill 掉,但是进程组中的其它进程仍然存在。

Process group creation and setting

// 获取进程 pid 的进程组 id.
pid_t getpgid(pid_t pid);

// 指定 pid 为进程组组长或将 pid 加入到组 pgid.
int setpgid(pid_t pid, pid_t pgid);
New process group

By designating a certain process as the group leader, a new process group is created. It is equivalent to calling setpgid(pid, pid), that is, the process pid is set as the process group leader, and the process group pid is created at the same time.

Add the process to the process group

To add a process to an existing process group, you can use setpgid(pid, pgid).

注意:在使用上面的函数时,必须保证调用者进程、被设置的进程以及要添加的进程组属于同一个会话,否则会出现权限错误。在这里,“会话”这个概念可以暂时理解为同一个终端。

这也就是说,倘若你在你的终端中去设置其它终端的进程组,会失败。
命令 ps ajx 可以查看进程的 pid, gid, sid 等相关属性。

Guess you like

Origin blog.csdn.net/sun172270102/article/details/112682556