3
我寫了這個簡單的程序:SIGINT處理和函數getline
void sig_ha(int signum)
{
cout<<"received SIGINT\n";
}
int main()
{
string name;
struct sigaction newact, old;
newact.sa_handler = sig_ha;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGINT,&newact,&old);
for (int i=0;i<5;i++)
{
cout<<"Enter text: ";
getline(cin,name);
if (name!="")
cout<<"Text entered: "<<name;
cout<<endl;
}
return 0;
}
如果我打CTRL-C在程序等待輸入我得到以下輸出:
輸入文字:收到SIGINT
進入文本:
輸入文本:
輸入文本:
輸入文本:
(中程序繼續循環而不等待輸入)
我該怎麼辦?
你能描述你想實現什麼?通常對於一個簡單的程序來說,讓信號具有預期的效果比較容易,即對於SIGINT,終止程序。此外,根據您的系統,使用信號處理程序中的高級io(如std :: cout)可能並不安全。 – 2009-11-21 11:47:46
我正在嘗試編寫一個小shell程序。這只是一個例子來幫助我描述我的問題。 – ThP 2009-11-21 12:12:06