Printf is not working in C signal handler

前端 未结 2 1683
长情又很酷
长情又很酷 2020-11-28 15:12

Code in question first (minimized case):

#include 
#include 

int counter = 0;

void react_to_signal(int n) {
    fprintf(stde         


        
相关标签:
2条回答
  • 2020-11-28 15:55

    In short: you cannot safely use printf within a signal handler.

    There's a list of authorized functions in signal handler's man page. There is not fprintf in it.

    That's because this function is not reentrant, mainly because it can use malloc and free. See this post for a detailed explanation.

    0 讨论(0)
  • 2020-11-28 16:12

    You may need to fflush stderr to get the message to write before the program exits.

    0 讨论(0)
提交回复
热议问题