[英]printf() prints twice the same line after signal handling
我有這個 function (在帶有信號量的長程序中,如有必要,我將附上任何代碼):
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
停止它時,而不是得到這個 output:
^C Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
我得到這個帶有雙打印的 output:
^C Pressed - Stop the timer & start cleaning process...
Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
這個小東西很煩人,我該如何解決? 謝謝。
在 ISO C 中,信號處理程序只能設置易失原子標志,或中止程序。 在 POSIX 中它也可能調用一些信號安全函數,你會看到printf
不在列表中。
所以,基本上,不要從信號處理程序中調用 printf 。
如果要在沒有 ISO 一致性的情況下遵循 POSIX 一致性,也許您可以write
標准輸出。 或者對於后者,在信號處理程序中設置一個標志並從主循環定期檢查該標志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.