- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
来自 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 .
pid
不能成为 session 负责人?pid
可以成为组长吗?为什么?
如果是,调用setpgid()
后,原先在组中的其他进程会在哪个组中最初由进程 pid
领导是:
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 callingtcsetpgrp
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/
在为我的 OS 类编写程序时,我发现了一个有趣的案例,该案例涉及一个似乎涉及 setpgid 的竞争条件。 分别编译下面的每个程序。在执行完./test 3(或任意数字> 2)后,ps jx 会显示所
我不知道为什么我的计算机上会出现这样的错误。我已经在其他一些计算机上测试了代码。效果很好。 如果我删除 sleep(2),它就会起作用。我想我应该能够为僵尸进程设置 PGID。它取决于操作系统的实现吗
我正在尝试将进程的 pgrp 更改为子进程的 pgrp,以便我可以在父进程上 setsid。唯一的问题是我不断收到 EPERM 错误代码。根据 htop,这两个进程具有相同的 session 组。 我
来自 Linux 编程接口(interface) #include int setpgid(pid_t pid , pid_t pgid ); The pid argument may not
我就是不明白。 我的进程树: 0 / \ 1 2 / \ 5 3 / 4 我想创建一个进程组(3、4、5),并向该组
当我执行这个函数时,我计算出了 setpgid() 的函数,结果将被拒绝权限。然后我当时以 root 用户身份登录,这也会打印错误消息,因为权限被拒绝。那么哪个用户可以使用这个功能。谁能给我解释一下?
根据 man setpgid(2) 页面, EACCES An attempt was made to change the process group ID of one of the childr
我正在尝试重新编写一个 shell,我想更改用我的 shell 启动的程序的 pgid! 我尝试在我的 fork() 之后执行不同的功能: setpgid(0, 0) 使诸如 vim、emacs 之类
root@arch:/ 19:27:32 # docker run -t -i ubuntu /bin/bashroot@74b77bf42943:/# lsbash: child setpgid (
我正在尝试编写一个执行子命令的程序,并且不允许该子命令被 Ctrl+C 杀死。 我读到可以使用 setpgid/setpgrp 来完成此操作。 以下代码适用于 OSX,但在 Linux(2.6.32、
我是一名优秀的程序员,十分优秀!