信号() 覆盖其他信号处理程序
signal() overwriting other signal handlers
signal()
函数是否会覆盖进程可能已设置的其他信号调用? 即,如果进程设置了SIGINT
处理程序,并且 DLL 调用signal(SIGINT,xxx)
来处理自己的终止代码,则原始SIGINT
处理程序是否被禁用?
signal()
调用:
- 安装指定为新信号处理程序的处理程序,并且
- 告诉您旧的处理程序是什么。
将调用新处理程序而不是旧处理程序。 如果要链接它们,则需要执行以下操作:
typedef void (*Handler)(int signum);
static Handler old_int_handler = SIG_IGN;
static void int_handler(int signum) /* New signal handler */
{
/* ...do your signal handling... */
if (old_int_handler != SIG_IGN && old_int_handler != SIG_DFL)
(*old_int_handler)(signum);
}
static void set_int_handler(void) /* Install new handler */
{
Handler old = signal(SIGINT, SIG_IGN);
if (old != SIG_IGN)
{
old_int_handler = old;
signal(SIGINT, int_handler);
}
}
static void rst_int_handler(void) /* Restore original handler */
{
Handler old = signal(SIGINT, SIG_IGN);
if (old == int_handler)
{
signal(SIGINT, old_int_handler);
old_int_handler = SIG_IGN;
}
}
void another_function()
{
/* ... */
set_int_handler();
/* ... */
rst_int_handler();
/* ... */
}
如果中断被忽略,这会使它们被忽略。 如果中断由用户定义的中断处理程序处理,则这将调用信号处理代码和原始信号处理代码。
请注意,Christian.K 关于不处理 DLL(共享库(中的信号的建议也是相关且有效的。 上面的描述假设您决定忽略该建议。
这不是对问题的"字面"回答,而是建议:您不应该在 DLL 中执行此操作。
对于使用 DLL 的应用程序来说,这是出乎意料的,而且通常很烦人。DLL 应该(通常(是"被动的",并且只提供供应用程序调用的函数。
因此,而是从您的 DLL 中提供一个公共函数,应用程序需要调用该函数,例如 MyDllCleanup()
.然后让应用程序决定如何调用该函数(通过信号处理程序或其他(。顺便说一句,初始化也是如此:而不是依赖DllMain
(或 _init
/_fini
在 UNIX 上libdl
(为应用程序调用提供显式函数。
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 有可能在信号处理程序中设置promise吗
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 通过安装信号处理程序关闭多线程应用程序
- 为什么我的信号处理程序只执行一次?
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- C++ 在信号处理程序后继续执行
- 没有信号处理程序的POSIX定时器的目的是什么?
- 为什么这个信号处理程序不能捕获 SIGHUP 或 SIGQUIT?
- 如何在C++中使用 std::bind 函数作为信号处理程序?
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 如何将信号处理程序添加为方法
- 窗口上信号处理程序的异步安全写入函数
- 使用信号处理程序处理从 FIFO 接收的数据
- gtkmm/glade - 将信号处理程序连接到 MenuShell
- GTK+3 编译错误"找不到信号处理程序",您是否使用 -rdynamic 进行了编译?
- Linux 中的信号处理程序使用 sigaction (C++)
- 使用 enum 参数将 C++ 对象信号连接到 QML 信号处理程序
- 信号处理程序不要求使用dlopen或dlclose