2016-04-22 65 views
1

爲了編寫必須通過發送信號SIGINT來響應Ctrl + C單擊的代碼,我使用Eclipse for C/C++ Developers版本。但是,當我從控制檯運行我的代碼時,我發現它完全不響應Ctrl + C。我將Ctrl + C的功能作爲複製的鍵映射進行了分解,但它仍不能解決問題。 你知道我能做些什麼來解決它嗎?針對C/C++開發人員的Eclipse和針對Ctrl + C的Eclipse

一個代碼示例:

#include <sys/types.h> 
#include <sys/wait.h> 
#include <stdio.h> 


static void sig_int(int num) { 
    printf("\nerror\n"); 
} 

int main (int argc, char *argv[]) { //7 

    sigset_t zeromask; 

    if (signal(SIGINT, sig_int) == SIG_ERR) 
        fprintf(stderr,"signal(SIGINT) error"); 

    printf("Hello\n"); 

    if (sigsuspend(&zeromask) != -1) 
     fprintf(stderr,"sigsuspend error"); 

} 

如果當你點擊「CTRL + C」從運行Linux終端,這個代碼,你會得到一個「錯誤」的輸出。但是,從Eclipse控制檯運行它時,什麼都不會發生。 Eclipse控制檯不會將Ctrl + C當作鍵盤中斷。

+0

請發佈一些代碼,除非你說它是從終端,但不是從eclise的控制檯。 – Shark

+0

我編輯了我的帖子並添加了一些代碼。 – CrazySynthax

+0

https://stackoverflow.com/questions/8814383/sending-sigint-ctrl-c-to-program-running-in-eclipse-console https://stackoverflow.com/search – Rei

回答

1

原因是gdb正在捕獲SIGINT信號而沒有傳遞它。

如果你去gdb的控制檯(帶有gdb的版本,而不是一個標有「痕跡」),進入「信息信號」,你會看到類似下面的列表:

info signals 
Signal  Stop Print Pass to program Description 

SIGHUP  Yes Yes Yes  Hangup 
SIGINT  Yes Yes No  Interrupt 
SIGQUIT  Yes Yes Yes  Quit 
SIGILL  Yes Yes Yes  Illegal instruction 

請注意,SIGINT的Pass設置爲NO。

輸入:處理SIGINT通

SIGINT也被GDB因此它可能會問,如果你真的相信。

編輯

當我試圖從Eclipse的一個簡單的程序,該程序不接受SIGINT,直到程序已暫停。這可能是Eclipse的一個問題。當直接從gdb運行時,程序按預期工作。