Is it possible to ignore all signals?

后端 未结 3 990
無奈伤痛
無奈伤痛 2021-01-17 11:49

I have a server application which I want to protect from being stopped by any signal which I can ignore. Is there a way to ignore all possible signals at once, without setti

3条回答
  •  借酒劲吻你
    2021-01-17 12:20

    Blocking signals is NOT the same as ignoring them.

    When you block signals as suggested by C2H5OH, it gets added to a pending signal queue and will be delivered to the process as soon as you unblock it.

    Unblocking can be done using

    #include 
    
    sigset_t mask;
    sigemptyset(&mask);
    sigprocmask(SIG_SETMASK, &mask, NULL);
    

    To answer your question on how to ignore signals, it has to be handled by a Signal Handler which is a user-defined function which executes whenever a signal is delivered to the process

    static void foo (int bar)
    {
         /*some code here. In your case, nothing*/
    }
    

    then register this function by using

    signal(SIGINT,foo);   //or whatever signal you want to ignore
    

    If you want to ignore all signals

    int i;
    for(i = 1; i <=31 ; i++)
    {
       signal(i,foo);
    }
    

    This code will take all the signals delivered to the process and ignore them instead of blocking them.

    NOTE: According to man pages , it is not the recommended way, instead sigaction is suggested. Do check out man sigaction

提交回复
热议问题