【发布时间】:2020-09-21 12:37:32
【问题描述】:
我有这个功能(在一个带有信号量的长程序中,如有必要我会附上任何代码):
void signalHandler(int signumber){
/* for SIGINT */
if(signumber == SIGINT){
printf(" Pressed - Stop the timer & start cleaning process...\n");
flag = 0; //relevant for the rest of the program
}
}
当我运行程序并按CTRL+C 停止它时,而不是得到这个输出:
^C Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
我用双打印得到这个输出:
^C Pressed - Stop the timer & start cleaning process...
Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
这个小东西很烦人,我该如何解决? 谢谢。
【问题讨论】:
-
printf不是异步信号安全的,因此在信号处理程序中调用它会产生未定义的行为。不要那样做。 -
stdio 甚至在signal-safety(7)的第三段中被调用...
-
标签中有标签fork。你在分叉之前设置信号处理程序吗?父母和孩子都收到信号,所以他们都会打印消息。
标签: c linux multiprocessing fork