signal()覆盖其他信号处理程序 [英] signal() overwriting other signal handlers

查看:173
本文介绍了signal()覆盖其他信号处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

signal()函数是否覆盖进程可能设置的其他信号调用?即如果进程已经设置了 SIGINT 处理程序,并且DLL调用信号(SIGINT,xxx)来处理它自己的终止代码,原来的 SIGINT 处理程序是否被禁用?

解决方案

$($)code code code code code code code $ c
$处理程序和

  • 告诉你旧的处理程序是什么。

  • 新的处理程序将叫而不是旧的。如果你想链接它们,你需要执行以下操作:

      typedef void(* Handler)(int signum); 

    static Handler old_int_handler = SIG_IGN;

    static void int handler(int signum)/ *新的信号处理程序* /
    {
    ...你的信号处理...
    if(old_int_handler! = SIG_IGN&& old_int_handler!= SIG_DFL)
    (* old_int_handler)(signum);
    }

    static void set_int_handler(void)/ *安装新的处理程序* /
    {
    处理程序old = signal(SIGINT,SIG_IGN);
    if(old!= SIG_IGN)
    {
    old_int_handler = old;
    信号(SIGINT,int_handler);
    }
    }

    static void rst_int_handler(void)/ *恢复原始处理程序* /
    {
    处理程序old = signal(SIGINT,SIG_IGN);
    if(old == int_handler)
    {
    signal(SIGINT,old_int_handler);
    old_int_handler = SIG_IGN;
    }
    }

    ...另一个函数...
    {
    ...
    set_int_handler();
    ...
    rst_int_handler();
    ...
    }

    如果中断被忽略,忽略。如果中断由用户定义的中断处理程序处理,那么这将调用您的信号处理代码和原始信号处理代码。



    请注意, Christian.K https://stackoverflow.com/a/10701889/15168\">关于不处理DLL(共享库)中的信号也是相关和有效的。上述说明假设您决定忽略该建议。


    Does the signal() function overwrite other signal calls a process might have set up? I.e. if a SIGINT handler has been setup by a process, and a DLL calls signal(SIGINT,xxx) to handle its own termination code, does the original SIGINT handler get disabled?

    解决方案

    The signal() call:

    1. Installs the handler you specify as a new signal handler, and
    2. Tells you what the old handler was.

    The new handler will be called instead of the old one. If you want to chain them, you need to do something like:

    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;
        }
    }
    
    ...in another function...
    {
        ...
        set_int_handler();
        ...
        rst_int_handler();
        ...
    }
    

    If interrupts were being ignored, this keeps them ignored. If interrupts were being handled by a user-defined interrupt handler, then this calls your signal handling code and the original signal handling code.

    Note that the advice from Christian.K about not handling signals in a DLL (shared library) is also relevant and valid. The description above assumes you decide to ignore that advice.

    这篇关于signal()覆盖其他信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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