gpt4 book ai didi

setpgid() 的第一个参数可以是 session 组长还是组长?

转载 作者:太空狗 更新时间:2023-10-29 11:40:42 25 4
gpt4 key购买 nike

来自 Linux 编程接口(interface)

#include <unistd.h>
int setpgid(pid_t pid , pid_t pgid );

The pid argument may not specify a process that is a session leader. Violation of this rule results in the error EPERM .

  1. 为什么 pid 不能成为 session 负责人?
  2. pid 可以成为组长吗?为什么?

    如果是,调用setpgid()后,原先在组中的其他进程会在哪个组中最初由进程 pid 领导是:

    • gid 为pid 的原始组现在丢失了进程pid 并且没有组长,或者
    • 新组 pgid 更改为哪个进程 pid

    我怀疑第一个可能与书中对 setsid() 的描述相矛盾:

    The restriction against a process group leader being able to call setsid() is necessary because, without it, the process group leader would be able to place itself in another (new) session, while other members of the process group remained in the original session. (A new process group would not be created, since, by definition, the process group leader’s process group ID is already the same as its process ID.) This would violate the strict two-level hierarchy of sessions and process groups, whereby all members of a process group must be part of the same session.

谢谢。

一些相关问题Can a leader of a process session or group leave for another existing session or group?

最佳答案

此行为由 POSIX 指定(这意味着它适用于所有自称为“Unix”的东西,而不仅仅是 Linux)。 http://pubs.opengroup.org/onlinepubs/9699919799/functions/setpgid.html说:

ERRORS
[EPERM] The process indicated by the pid argument is a session leader.

规范没有说明这条规则存在的原因,但我相信 dbush 陈述的理由是正确的: session 领导者必须始终是进程组领导者;如果它可以移动到另一个进程组,它将不再是进程组的领导者,这违反了不变量。

但是,一个进程只是进程组组长,而不是 session 组长,可能将自己放入另一个进程组(不再是进程组组长),然后可能将自己退出过程组的成员,并再次成为过程组组长。在某些情况下,作业控制 shell 实际上 必须 执行此操作:注意规范 RATIONALE 部分底部的位

One non-obvious use of setpgid() is to allow a job control shell to return itself to its original process group (the one in effect when the job control shell was executed). A job control shell does this before returning control back to its parent when it is terminating or suspending itself as a way of restoring its job control "state" back to what its parent would expect.

POSIX 没有解释为什么作业控制 shell 首先会更改其进程组,但是 GNU C Library manual section on implementing job control填补空白:

When a shell program that normally performs job control is started, it has to be careful in case it has been invoked from another shell that is already doing its own job control.

A subshell that runs interactively has to ensure that it has been placed in the foreground by its parent shell before it can enable job control itself.

[...]

Once the subshell has been placed into the foreground by its parent shell, it can enable its own job control. It does this by calling setpgid to put itself into its own process group, and then calling tcsetpgrp to place this process group into the foreground.

然后,当然,如果子 shell 被挂起(例如,Bash 有一个 suspend 内置函数可以执行此操作),它必须再次撤消该操作。

另请注意,作业控制外壳不会将自己建立为 session 领导者,即使它不是任何东西的子外壳。 session 由负责设置控制终端的任何程序初始化;该程序通常是在终端中运行的最外层 shell 的父级和 pre-exec 标识。例如,xterm calls setsid在打开伪终端并 fork 之后,但在 exec 之前 - 将在该终端窗口中运行的程序。

关于setpgid() 的第一个参数可以是 session 组长还是组长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50591754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com