问题描述
我不确定如何使用tcsetpgrp()int tcsetpgrp(int fildes, pid_t pgid_id);
,我想将前台组pid设置为孩子的新组pid,这样当我键入control-C时,它只会杀死我的孩子进程.终端,而不是后台终端. (我认为当前,父组pid与外壳的前台gpid相同)
I am not sure how to use tcsetpgrp() int tcsetpgrp(int fildes, pid_t pgid_id);
, I want to set the foreground group pid to become the child's new group pid so that when I typed control-C, it will only kill my child process in my terminal, not the background terminal. (I think currently, the parent group pid is the same as the shell's foreground gpid)
// we are in the child process
// check the foreground process group id.
pid_t fore_pgid = tcgetpgrp(0);
printf("the foregroud pgid is: %d \n", fore_pgid);
// change the foreground process group id to the redefined child process's group pid.
tcsetpgrp(0, child_gpid);
fore_pgid = tcgetpgrp(0);
printf("the foregroud pgid is: %d \n", fore_pgid);
结果:程序只是停留在此tcsetpgrp(0, child_gpid);
行上因为它之后没有打印出一些字符串.
result: the program just stuck on this tcsetpgrp(0, child_gpid);
linebecause it didn't print out some strings after that.
请注意,以上只是我的想法,可能不正确;我不知道如何仅杀死我的子进程,而不是后台shell.如果您知道该怎么做,请帮助我.提前表示感谢!
note that, above is only my thought, it might not be correct; I don't know how to only kill my child process, but not the background shell. If you know how to do this, please help me.Tons of thanks in advance!
推荐答案
我不确定我了解您要做什么.
I'm not sure I understand what you're trying to do.
这是IBM所做的事的一个例子
Here is an example from IBM of what they have done
这篇关于不知道如何使用tcsetpgrp()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!