Catching signal inside its own handler

后端 未结 5 664
抹茶落季
抹茶落季 2021-01-11 10:41
#include
#include

void handler(int signo)
{
    printf(\"Into handler\\n\");
    while(1);
}
int main()
{
    struct sigaction act;
          


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 11:06

    What you are doing seems like a very bad idea, though, and it might be better to simply set a flag from the handler and return from it, and then do the printing from main.

    You need to set SA_NODEFER or otherwise re-enable the signal within the signal handler itself because otherwise the signal gets blocked or switched back to its default behavior right before the call to the handler.

    Calling printf from a signal handler is undefined behavior. It may crash your program. The list of functions that you actually can safely call from a signal handler is very limited. I need a list of Async-Signal-Safe Functions from glibc

提交回复
热议问题