管理 fork() 守护进程的信号处理
我想用 perl 编写一个强大的守护进程,它将在 Linux 上运行,并遵循描述的模板 在这个优秀的答案中。但是,我的情况有一些差异:首先,我使用 Parallel::ForkManager 开始()和下一个
; fork 一个事件,紧接着 exec('handle_event.pl')
在这种情况下,我有以下问题:
- 我应该在哪里定义我的信号处理程序。我应该在父级(守护进程)中定义它们并假设它们将在子级中继承吗?
- 如果我运行
exec('handle_event.pl')
处理程序是否会在 exec 中继承(我知道它们是在fork
中继承)? - 如果我在
handle_event.pl
中重新定义一个新的信号处理程序,该定义是否会覆盖父级中定义的信号处理程序? - 在这种情况下,最佳做法是什么?
谢谢
I want to write a robust daemon in perl that will run on Linux and am following the template described in this excellent answer. However there are a few differences in my situation: First I am using Parallel::ForkManager start() and next
; to fork on an event immediately followed by exec('handle_event.pl')
In such a situation, I have the following questions:
- Where should I define my signal handlers. Should I define them in the parent (the daemon) and assume that they will be inherited in the children?
- If I run
exec('handle_event.pl')
will the handlers get inherited across the exec (I know that they are inherited across thefork
)? - If I re-define a new signal handler in
handle_event.pl
will this definition override the one defined in the parent? - What are best practices in a situation like this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您分叉时,子进程具有与父进程相同的信号处理程序。当您执行时,任何被忽略的信号仍然被忽略;任何已处理的信号都会重置回默认处理程序。
When you fork, the child process has the same signal handlers as the parent. When you exec, any ignored signals remain ignored; any handled signals are reset back to the default handler.
exec
将整个进程代码替换为将要执行的代码。由于信号处理程序是进程映像中的代码,因此它们不能跨 exec 继承,因此 exec 会将已处理信号的信号处理配置重置为其默认状态(忽略信号)将保持忽略状态)。因此,您需要在启动时在exec
ed 进程中安装任何信号处理。The
exec
replaces the whole process code with the code that will be executed. As signal handlers are code in the process image, they cannot be inherited across anexec
, soexec
will reset the signal handling dispositions of handled signals to their default states (ignored signals will remain ignored). You will therefore need to install any signal handling in theexec
ed process when it starts up.