gpt4 book ai didi

linux - 是否有可能在 "exec"之后向处理程序发出信号以生存?

转载 作者:IT王子 更新时间:2023-10-29 00:13:22 26 4
gpt4 key购买 nike

我为一个进程写了​​一个signal handler,然后fork(),这个signal handler会同时应用到父进程和子进程。如果我用“exec”替换子进程,信号处理程序就不再存在。

我知道发生这种情况是因为“exec”调用会用它自己的地址空间覆盖子进程地址空间。我只想知道是否有办法让信号处理程序即使在“exec”调用后也能正常工作?

最佳答案

没有。来自 man 页面:

execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded. The program invoked inherits the calling process's PID, and any open file descriptors that are not set to close on exec. Signals pending on the calling process are cleared. Any signals set to be caught by the calling process are reset to their default behaviour. The SIGCHLD signal (when set to SIG_IGN) may or may not be reset to SIG_DFL.

事实上,如果在代码被一些非常不同的代码替换后信号处理程序仍然处于事件状态,那么当信号发生时,您可以预料到各种困惑。毕竟,信号处理程序只是一个在发生某些事情时调用的地址(目前不包括 SIG_IGNSIG_DFL)。当您替换整​​个文本段时,谁知道该地址会是哪一段代码?

关于linux - 是否有可能在 "exec"之后向处理程序发出信号以生存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com