信号处理程序是否有可能在“ exec”之后生存?

发布于 2025-02-09 06:17:19 字数 163 浏览 1 评论 0原文

我为一个过程写了一个信号处理程序,然后fork()之后,信号处理程序将应用于父母和子过程。如果我用“ exec”替换子过程,则信号处理程序已不复存在。

我知道发生这种情况是因为“执行”呼叫将用自己的呼叫覆盖子过程地址空间。 我只想知道即使在“执行”呼叫之后,是否有一种方法可以使信号处理程序工作?

I wrote a signal handler for a process, and fork() after that, the signal handler will be applied to both parent and child processes. If I replace the child process with "exec", the signal handler is no more.

I know this happens because "exec" call will overwrite the child process address space with it's own.
I just want to know if there is a way to make signal handler work even after "exec" call ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2025-02-16 06:17:19

否。从man页面:

execve()不会返回成功,并且呼叫过程的文本,数据,BS和堆栈被加载程序的文本覆盖。调用程序的程序继承了调用过程的PID,以及任何未设置在EXEC上关闭的打开文件描述符。清除了呼叫过程中的信号。通话过程捕获的任何信号都重置为其默认行为。 SIGCHLD信号(设置为sig_ign)可能会或可能不会重置为sig_dfl。

实际上,如果信号处理程序在代码被一些非常不同的代码替换后仍处于活动状态,则您可能会期望在信号发生时各种混乱。毕竟,信号处理程序是发生某些事情时要调用的地址(折扣sig_ignsig_dfl暂时)。当您替换整​​个文本段时,谁知道该地址的代码会在该地址上是什么?

No. From the man pages:

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.

In fact, if the signal handler were still active after the code had been replaced with some very different code, you could expect all sorts of mayhem when the signal occurred. The signal handler is, after all, just an address to call when something happens (discounting SIG_IGN and SIG_DFL for now). Who knows what piece of code would be at that address when you replace the entire text segment?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文