c - POSIX 进程组

标签 c process posix

我目前正在将进程组实现到我的操作系统项目的 POSIX 子系统中。然而,我对 POSIX specification (setsid) 有点困惑(以及维基百科有关进程组的页面)。

我们的终端层向前台进程(组,其 id 应等于组领导者的 PID)发送 SIGINT。在这种情况下,前台进程(我们的“登录”应用程序)通过调用 setsid 成为组领导者。当用户登录时,程序会 fork 并执行用户的 shell。在这个阶段,我的理解是,在调用 exec* 之前,我会从 fork 的子进程中调用 setpgid。这意味着执行的程序从一开始就是进程组的一部分。

如果我想在进程组之外运行新 fork 的子进程,我只需在调用 exec* 之前在 fork 的子进程中调用 setsid 即可。

这是正确的吗?有什么我应该检查或做的真正晦涩的事情吗?

作为一个后续问题,我相信我已经知道了,fork 是否需要转移组成员身份?或者是否必须在每次 fork 调用后使用 setpgid 来完成?我收集进程组是通过 forkfork 的 POSIX 定义传输的。

提前致谢。

最佳答案

有趣的问题 - 尤其是因为它很长时间都没有得到部分答案。

POSIX 基本定义

POSIX 定义部分的一些引用:

3.290 Process Group

A collection of processes that permits the signaling of related processes. Each process in the system is a member of a process group that is identified by a process group ID. A newly created process joins the process group of its creator.

3.291 Process Group ID

The unique positive integer identifier representing a process group during its lifetime.

Note: See also Process Group ID Reuse defined in Process ID Reuse .

3.292 Process Group Leader

A process whose process ID is the same as its process group ID.

3.293 Process Group Lifetime

The period of time that begins when a process group is created and ends when the last remaining process in the group leaves the group, due either to the end of the lifetime of the last process or to the last remaining process calling the setsid() or setpgid() functions.

Note: The setsid() and setpgid() functions are defined in detail in the System Interfaces volume of POSIX.1-2008.

[...]

3.337 Session

A collection of process groups established for job control purposes. Each process group is a member of a session. A process is considered to be a member of the session of which its process group is a member. A newly created process joins the session of its creator. A process can alter its session membership; see setsid(). There can be multiple process groups in the same session.

Note: The setsid() function is defined in detail in the System Interfaces volume of POSIX.1-2008.

3.338 Session Leader

A process that has created a session.

Note: For further information, see the setsid() function defined in the System Interfaces volume of POSIX.1-2008.

3.339 Session Lifetime

The period between when a session is created and the end of the lifetime of all the process groups that remain as members of the session.

<小时/>

POSIX 系统接口(interface)

NAME

setsid - create session and set process group ID

SYNOPSIS

   #include <unistd.h>

   pid_t setsid(void);

DESCRIPTION

The setsid() function shall create a new session, if the calling process is not a process group leader. Upon return the calling process shall be the session leader of this new session, shall be the process group leader of a new process group, and shall have no controlling terminal. The process group ID of the calling process shall be set equal to the process ID of the calling process. The calling process shall be the only process in the new process group and the only process in the new session.

还有:

NAME

setpgid - set process group ID for job control

SYNOPSIS

   #include <unistd.h>

   int setpgid(pid_t pid, pid_t pgid);

DESCRIPTION

The setpgid() function shall either join an existing process group or create a new process group within the session of the calling process.

The process group ID of a session leader shall not change.

Upon successful completion, the process group ID of the process with a process ID that matches pid shall be set to pgid.

As a special case, if pid is 0, the process ID of the calling process shall be used. Also, if pgid is 0, the process ID of the indicated process shall be used.

<小时/>

解释

正如定义所明确的,一个 session 可能由多个进程组组成。在广泛的限制内,进程可以更改进程组(尽管它在任何时候只属于一个进程组)。 session 处理的选项更加有限;基本上,一个进程要么保留其原始 session 的成员,要么可以使自己成为新 session 的领导者。

复制问题的部分内容:

Our terminal layer sends SIGINT to the foreground process (group, whose id should equal the group leader's PID). In this case, that foreground process (our "login" application) becomes a group leader by calling setsid. When the user logs in, the program forks and executes the user's shell. At this stage, my understanding is that I call setpgid from the forked child before calling exec*. This means the executed program will be a part of the process group from the outset.

我怀疑括号应该是“前台进程组(其 id 应等于组长的 PID)”。根据定义(3.292),进程组领导者是PID与进程组ID相同的进程。我没有引用相关 Material ,但我相信向进程组组长发送信号是正确的

请注意,前台进程通过调用setsid()成为 session 领导者,并且也成为进程组领导者。我希望登录程序在 fork 之后但在执行 shell 之前将用户的 shell 设置为进程组领导者(也可能是 session 领导者)。所有子进程自动继承父进程的进程组和 session ;如果您希望它有所不同,您必须覆盖它。

If I wanted to run the newly forked child outside the process group I would merely call setsid in the forked child before calling exec*.

您可以这样做,但它也会创建一个新 session 。您可能想要使用 setpgid() (现代标准;可能是 setpgrp() 这是 SVID 的旧标准)而不是 setsid() .

Is this correct? Are there any really obscure things I should be checking or doing?

是的,这基本上是正确的。是的,可能还有一些晦涩的事情需要跟踪。例如,您可能需要考虑控制 TTY。

As a follow-on question, which I believe I already know, is it a requirement for fork to transfer group membership? Or is it something that must be done using setpgid after every fork call? I gather process groups are transferred by fork from the POSIX definition of fork.

fork()之后的子进程属于同一组组(如/etc/group),并且也属于同一 session 和同一进程组 - 但它不是 session 领导者,也不是流程组领导者。

关于c - POSIX 进程组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1046933/

相关文章:

c - 一轮 float 值

c - 如何打印 do while 循环的执行次数?

c - 带字符串的 Switch 语句?

arrays - 使用 .NET 多线程运行同一应用程序的多个实例

c# - 从作为网络服务运行的 Windows 服务生成进程

c++ - 我可以在已经由 main 创建的 pthread 中创建一个 pthread 吗?

C - printf 和 scanf 缓冲区

c# - 简单的 linq 查询会出现错误

c - 线程已创建但无法相应工作

c++ - 关于意外断电的原子写入