为什么 fork() before setsid() 是必要的,即让子进程不是组长?
Why is fork() before setsid() necessary, i.e. make child process not a group leader?
当我们创建守护进程时,我们都知道我们在setsid()之前fork()的原因是为了让子进程不是进程组领导,因为我们需要调用setsid()。
我的问题是,为什么父进程可能是进程组组长?如果 fork() 将使子进程不是进程组长,那么为什么父进程是组长?因为父进程被 grand process 派生出来对吗?
谢谢!
shell做到了。
交互式 shells 将使每个执行的程序成为自己的进程组领导,以启用作业控制(命令的前台和后台)。
以下是关于 Implementing a Job Control Shell 的 GNU 文档的摘录:
As each process is forked, it should put itself in the new process group by calling setpgid [...]
if (shell_is_interactive)
{
/* Put the process into the process group and give the process group
the terminal, if appropriate.
This has to be done both by the shell and in the individual
child processes because of potential race conditions. */
pid = getpid ();
if (pgid == 0) pgid = pid;
setpgid (pid, pgid);
if (foreground)
tcsetpgrp (shell_terminal, pgid);
当我们创建守护进程时,我们都知道我们在setsid()之前fork()的原因是为了让子进程不是进程组领导,因为我们需要调用setsid()。
我的问题是,为什么父进程可能是进程组组长?如果 fork() 将使子进程不是进程组长,那么为什么父进程是组长?因为父进程被 grand process 派生出来对吗?
谢谢!
shell做到了。
交互式 shells 将使每个执行的程序成为自己的进程组领导,以启用作业控制(命令的前台和后台)。
以下是关于 Implementing a Job Control Shell 的 GNU 文档的摘录:
As each process is forked, it should put itself in the new process group by calling setpgid [...]
if (shell_is_interactive)
{
/* Put the process into the process group and give the process group
the terminal, if appropriate.
This has to be done both by the shell and in the individual
child processes because of potential race conditions. */
pid = getpid ();
if (pgid == 0) pgid = pid;
setpgid (pid, pgid);
if (foreground)
tcsetpgrp (shell_terminal, pgid);