如何控制子进程的SIGTSTP信号?
我正在写一个外壳。它分叉后台和前台进程。我的 SIGTSTP 信号有问题。因此,在输入 Ctrl ^ Z 后,会生成 SIGTSTP,因为该信号被传递到我的 shell 及其子进程(我的 shell 分叉的所有后台和前台进程)。但就像在实际的 shell 中一样,SIGTSTP 仅传递给前台进程,而不传递给后台进程。那么如何控制这种行为意味着阻止信号发送到我的 shell 的后台进程呢?
我也尝试过 setpgid() ,这意味着更改后台进程的 pgid 。但是一旦进程执行完毕,setpgid()
就会返回错误。
I am writing a shell. It forks background and foreground processes. I have a problem with the SIGTSTP signal. So after giving Ctrl ^ Z, SIGTSTP is generated since this signal is delivered to my shell and its child processes (all background and foreground processes that my shell has forked). But like in actual shell, SIGTSTP is delivered to only foreground processes, not to background processes. So how to control this behavior means preventing the signal to be sent to background processes of my shell?
I have tried setpgid()
also, which means changing the pgid of background processes. But once a process has done exec, setpgid()
returns error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您为
SIGTSTP
安装了自己的处理程序,则可以控制它向您的子级的传播(或不传播)。由于您正在编写 shell,因此您可能希望使用一些其他信号(至少是SIGINT
)来执行此操作。子进程
exec
后无法setpgid
吗?很简单,在子exec
之前调用setpgid
。您不需要事后更改它 - 每个作业都应该有一个唯一的pgid
,并且您应该继续设置终端的控制组以匹配预期活动作业的pgid
。If you install your own handler for
SIGTSTP
, you can control its propagation to your children (or not). Since you're writing a shell, you probably want to do this with a few other signals (SIGINT
at least).Can't
setpgid
after a child hasexec
ed? Easy, callsetpgid
before the childexec
s. You shouldn't need to change it afterwards – each job should have a uniquepgid
and you should keep setting the terminal's controlling group to match the intended active job'spgid
.