进程组必须有一个正在运行的领导进程吗? [英] Must a process group have a running leader process?

查看:18
本文介绍了进程组必须有一个正在运行的领导进程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类Unix操作系统中,如果一个进程的pid和它的pgid相等,那么这个进程就是进程组长.

In Unix-like operating systems, if a process' pid and its pgid are equal, then the process is a process group leader.

但是,如果进程leader已经退出,同组的其他进程还在运行,那么接下来的leader进程是谁?

However, if the process leader has exited and the other processes in the same group are still running, who is the succeeding leader process?

推荐答案

没有继任的leader:一旦某个进程组leader退出,该组就失去了领导权.没有什么要求进程组有领导者,没有领导者也很好,您仍然可以使用 kill(2) 向组中的每个元素发送信号.

There is no succeeding leader: once a process group leader exits, the group loses leadership. Nothing requires a process group to have a leader, it's perfectly fine not to have one, and you can still send signals to every element in the group with kill(2).

leader 退出时究竟会发生什么,取决于组中进程的状态以及该组是否被归类为孤立进程组.

What exactly happens when the leader exits depends on the status of the processes in the group and whether or not the group classifies as an orphaned process group.

首先,让我们看看什么是孤儿组.

First, let's see what is an orphaned group.

POSIX 将孤立进程组定义为一个组,其中属于该组的每个进程的父进程要么是同一组的成员,要么是另一个会话的一部分.

POSIX defines an orphaned process group as a group in which the parent of each process belonging to that group is either a member of that same group or is part of another session.

换句话说,只要组中的至少一个进程在不同的进程组中但在同一会话中具有父进程,进程组就不是孤立的.

In other words, a process group is not orphaned as long as at least one process in the group has a parent in a different process group but in the same session.

这个定义起初可能看起来很奇怪,但其背后有一个基本原理,这将(希望)稍后会清楚.

This definition may seem odd at first, but there is a rationale behind this, which will (hopefully) be clear in a moment.

那么为什么知道一个群体是否是孤儿很重要呢?由于已停止的进程.如果一个进程组是孤立的,并且该组中至少有一个进程被停止(例如,它被 SIGSTOPSIGTSTP 挂起),那么 POSIX.1 要求孤立组中的每个进程都被发送SIGHUP,然后是SIGCONT.这样做的原因是为了避免进程永远停止:考虑会话领导者和进程组领导者退出的情况,并且组留下一个停止的进程.由于父进程在另一个会话中,它没有权限发送它SIGCONT,所以进程永远不会再次运行.

So why is it important to know if a group is orphaned? Because of processes that are stopped. If a process group is orphaned, and there is at least one process in that group that is stopped (e.g. it was suspended with SIGSTOP or SIGTSTP), then POSIX.1 requires that every process in the orphaned group be sent SIGHUP followed by SIGCONT. The reason for doing this is to avoid having the process stopped forever: consider the case where the session leader and the process group leader exit, and the group is left with a stopped process. Since the parent is in another session, it doesn't have permission to send it SIGCONT, so the process would never run again.

OTOH,如果父级在同一个会话中但在不同的组中,那么它有可能会使用 SIGCONT 发出停止进程的信号,因此该组不会被视为孤立的并且存在无需强制唤醒停止的进程.

OTOH, if the parent is in the same session but in a different group, then there is a chance that it will signal the stopped process with SIGCONT, so the group is not considered orphaned and there is no need to forcefully wake up stopped processes.

这篇关于进程组必须有一个正在运行的领导进程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆