是否可以向处理程序发出信号,使其在"exec"执行后继续存在? [英] Is it possible to signal handler to survive after "exec"?

查看:68
本文介绍了是否可以向处理程序发出信号,使其在"exec"执行后继续存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个进程编写了一个信号处理程序,然后编写了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.

我知道会发生这种情况,因为"exec"调用将使用其自身覆盖子进程地址空间. 我只想知道即使在"exec"调用之后,是否有一种方法可以使信号处理程序正常工作?

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 ?

推荐答案

否.在man页面上:

execve()不会成功返回,并且调用过程的文本,数据,bss和堆栈将被加载的程序覆盖.调用的程序将继承调用进程的PID,以及所有未设置为在exec上关闭的打开文件描述符.呼叫过程中待处理的信号被清除.设置为由呼叫过程捕获的任何信号都将重置为其默认行为. SIGCHLD信号(设置为SIG_IGN时)可能会也可能不会重置为SIG_DFL.

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).谁知道当您替换整​​个文本段时该地址将是什么代码段?

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?

这篇关于是否可以向处理程序发出信号,使其在"exec"执行后继续存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆