我有一個信號處理程序中設置使用的sigaction像這樣:奇怪的sigaction()和函數getline()的相互作用
struct sigaction act, oldact;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGALRM);
sigaddset(&act.sa_mask, SIGINT);
sigaddset(&act.sa_mask, SIGTERM);
sigaddset(&act.sa_mask, SIGTSTP);
sigaction(SIGALRM, &act, &oldact);
sigaction(SIGINT, &act, &oldact);
sigaction(SIGTERM, &act, &oldact);
sigaction(SIGTSTP, &act, &oldact);
act.sa_flags = 0;
在此之後我運行一個for循環來收集輸入並打印回來了(基本上表現得像貓)。
char *linebuf = NULL;
size_t n = 0;
int len;
while(1){
len = getline(&linebuf, &n, stdin);
if(len>0){
printf("%s", linebuf);
}
}
然而,一旦從我處理的信號返回,getline()
不再塊輸入,而是始終返回-1
同時設定errno
到EINTR
這是一箇中斷的系統調用。我顯然打算打斷getline()
,但我該如何重置它,以便我可以繼續閱讀輸入?
有幾個類似的問題並沒有真正解決我的問題,但它可以幫助您更好地理解問題。 12
另一個有趣的花絮是,我是用signal()
我的信號處理時,我沒有這個問題,但我改sigaction()
因爲它是POSIX兼容。
'的printf(linebuf);'可能不是一個有效的C語法。 – haccks
@haccks,是的,它應該已經'的printf( 「%S」,linebuf);' – Shahbaz
我已經改變了它是'的printf( 「%S」,linebuf);''不過的printf(linebuf);'是有效的c語法(確實給出警告'警告:格式不是字符串文字和格式參數[-Wformat-security],但它不是錯誤),而且不是問題。 – Connor