Managing Signal Handling for daemons that fork()

帅比萌擦擦* 提交于 2019-12-11 00:44:43

问题


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:

  1. 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?
  2. If I run exec('handle_event.pl') will the handlers get inherited across the exec (I know that they are inherited across the fork)?
  3. If I re-define a new signal handler in handle_event.pl will this definition override the one defined in the parent?
  4. What are best practices in a situation like this?

Thank you


回答1:


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 an exec, so exec 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 the execed process when it starts up.




回答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.



来源:https://stackoverflow.com/questions/1944693/managing-signal-handling-for-daemons-that-fork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!