本文介绍了它是有效的,以具有用于同一信号的多个信号处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我联系到我的测试应用程序test.out(C / Linux的3.0.0)两个共享库。这两个库有信号处理程序SIGINT。

I got two shared libraries linked to my test application test.out (C/Linux 3.0.0). Both of the libraries have signal handlers for SIGINT.

这是有效的有多个信号处理程序相同的信号(例如SIGINT)?
的顺序时,我产生一个SIGINT信号(例如,通过终止test.out使用Ctrl + C)处理程序将被执行。

It is valid to have multiple signal handlers for same signal (for example SIGINT) ?Which order the handlers will execute when I generate a SIGINT signal (say , by terminating test.out using Ctrl+C).

推荐答案

正如所说别人,只有一个信号处理程序可以设置,这是最后一个。然后,你将不得不调用管理两个函数自己。该函数可以返回pviously安装了$ P $信号处理程序,你可以叫自己。

As said by others, only one signal handler can be set, which is the last one. You would then have to manage calling two functions yourself. The sigaction function can return the previously installed signal handler which you can call yourself.

是这样的(未经测试code):

Something like this (untested code):

/* other signal handlers */
static void (*lib1_sighandler)(int) = NULL;
static void (*lib2_sighandler)(int) = NULL;

static void aggregate_handler(int signum)
{
    /* your own cleanup */
    if (lib1_sighandler)
        lib1_sighandler(signum);
    if (lib2_sighandler)
        lib2_sighandler(signum);
}

... (later in main)
struct sigaction sa;
struct sigaction old;

lib1_init(...);
/* retrieve lib1's sig handler */
sigaction(SIGINT, NULL, &old);
lib1_sighandler = old.sa_handler;

lib2_init(...);
/* retrieve lib2's sig handler */
sigaction(SIGINT, NULL, &old);
lib2_sighandler = old.sa_handler;

/* set our own sig handler */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = aggregate_handler;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);

这篇关于它是有效的,以具有用于同一信号的多个信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:28