如何使我的子进程在后台运行?

时间:2019-07-10 03:59:36

标签: c

当结尾带有&符号时,我想将子进程发送到后台。

我试图在子进程中执行以下代码,当前,shell的前台gid为1000,子进程的前台组ID为1000。但是从现在开始,孩子的pgid为2000,即与父级的pgid不同,如果我将子进程的前台gid更改为 NOT 与shell的pgd一样容易。

// we are in the child process.
/* if it is in the background mode, we want to set the child process's
   foreground id to not be the same as the terminal's foreground id, so
   that the child process can run in the background. */
   if(back_signal){
       tcsetpgrp(0, child_gpid);
   }

但是,我认为以上情况是错误的,因为函数tcsetpgrp()使具有进程组ID pgrp的进程组成为与fd相关联的终端上的前台进程组,这表示{{{ 1}}仅将子进程设置为另一个前台进程。

简而言之,我停留在如何使子进程在后台运行的问题上。另外,我还没有找到fg和bg的任何源代码,如果您知道的话,请帮助我。 预先感谢。

1 个答案:

答案 0 :(得分:0)

根据此答案:

How do I get tcsetpgrp() to work in C?

不可能在子进程中同时创建子进程,而需要在父进程中调用。

相关问题