2013-06-05 34 views
0

你好,我有下面的代碼:爲什麼我不能捕捉到SIGINT信號?

server s; 
namespace signals_handler 
{ 
    //sig_atomic_t is_stop=0; 
    void signal_handler(int sig) 
    { 
     if(sig==SIGHUP) 
     { 
      printf("recived :SIGHUP\n"); 
      s.restart(); 
     } 
     else if(sig==SIGINT) 
     { 
      printf("recived :SIGINT\n"); 
      //is_stop = 1; 
      s.interupt(); 
     } 
    } 
} 
int main(int argc, char* argv[]) 
{ 
    signal(SIGHUP, signals_handler::signal_handler); 
    signal(SIGINT, signals_handler::signal_handler); 
    s.start(); 
    s.listen(); 
    return 0; 
} 

當我開始這個代碼的執行我能趕上SIGHUP,SIGINT無法實現我的應用程序,但調試器在「聽」的功能採空,但不能移動到signalhandler功能,爲什麼發生這種情況,我做錯了什麼?

回答

1

這很正常。 gdb捕獲信號。從手冊:

通常情況下,GDB設置,讓非錯誤信號如SIGALRM 默默傳遞給你的程序(以免在程序的運行他們的 作用,干擾),但停止您的程序 立即發生錯誤信號。您可以使用句柄命令更改這些 設置。

要改變行爲,使用:

handle SIGINT nostop pass 

手柄信號[關鍵字...] 改變GDB處理信號,信號的方式。信號可以是信號或其名稱的編號(在開始時有或沒有'SIG'); 「低 - 高」形式的信號編號列表;或「全部」一詞, 表示所有已知信號。可選參數關鍵字,描述如下 ,說明要做出什麼改變。

句柄命令允許的關鍵字可以縮寫。他們 全名是:

nostop 
    gdb should not stop your program when this signal happens. It may still print a message telling you that the signal has come in. 
stop 
    gdb should stop your program when this signal happens. This implies the print keyword as well. 
print 
    gdb should print a message when this signal happens. 
noprint 
    gdb should not mention the occurrence of the signal at all. This implies the nostop keyword as well. 
pass 
noignore 
    gdb should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled. pass and noignore are synonyms. 
nopass 
ignore 
    gdb should not allow your program to see this signal. nopass and ignore are synonyms. 
相關問題