2014-12-04 49 views
0

我希望程序通過鍵入CTRL + C來生成一個SIGINT類型的信號來測試信號的陷阱。我不知道,我只是程序計算的第一中斷信號,並結束程序(只跳直入INTHANDLER功能)如何使Ctrl-c結束程序

#include <stdio.h> 
#include <signal.h> 
#include <stdlib.h> 
#include <time.h> 

void signalHandler(int signalValue); /* prototype */ 
void INThandler(int signalValue); 


int main(void) 
{ 
int i; /* counter used to loop 100 times */ 
int x; /* variable to hold random values between 1-50 */ 

signal(SIGUSR1, signalHandler); 
signal(SIGUSR1, INThandler); 
srand(time(NULL)); 


    for (i = 1; i <= 100; i++) { 
     x = 1 + rand() % 50; 

     if (x == 25) { 
      raise(SIGUSR1); 
     } 
     printf("%4d", i); 


     if (i % 10 == 0) { 
      printf("\n"); 
     } 
    } 
    return 0; 
} 


void signalHandler(int signalValue) 
{ 
    int response; 

    printf("%s%d%s\n%s","\nInterrupt signal (", signalValue, ") received.", 
        "Do you wish to continue (1 = yes or 2 = no)? \n"); 

    scanf("%d", &response); 
    if (response == 1) { 
     signal(SIGINT, signalHandler); 
    } 
    else { 
    signal(SIGINT, INThandler); 
    } 

} 


void INThandler(int signalValue) 
{ 
    signal(signalValue, SIG_IGN); 
    printf("\nCtrl-C command detected!"); 
    exit(0); 

} 
+0

首先你設置'signalHandler'處理'SIGUSR1',然後設置'INThandler'來處理它,而不是。你期望發生什麼? – Biffen 2014-12-04 15:27:38

回答

0

你的代碼,目前說:

signal(SIGUSR1, signalHandler); 
signal(SIGUSR1, INThandler); 

第一次調用到signal()被有效地忽略。您已經安裝了INThandler作爲程序自身發送SIGUSR1信號的信號處理程序,並且您尚未安裝SIGINT的任何信號處理程序。

+0

不能相信我做了這樣一個愚蠢的錯誤,現在它工作..謝謝! – 2014-12-04 15:32:28

+0

另請參見[如何避免在信號處理程序中使用'printf()'](http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler/ 16891799#16891799)瞭解有關您可以在信號處理程序中執行哪些操作的信息。你的信號是同步提升的,而不是異步的,這大大降低了風險。 – 2014-12-04 15:40:11

0

不知道你在用SIGUSR1做什麼。如果你想設置一個處理程序來捕獲SIGINT,只需使用signal(SIGINT,signalHandler)設置信號處理程序。

#include <stdio.h> 
#include <signal.h> 
#include <stdlib.h> 
#include <time.h> 
#include <unistd.h> 

void signalHandler(int signalValue); /* prototype */ 
void INThandler(int signalValue); 


int main(void) 
{ 
    int i; /* counter used to loop 100 times */ 
    int x; /* variable to hold random values between 1-50 */ 

    signal(SIGINT, signalHandler); 
    srand(time(NULL)); 


    for (i = 1; i <= 100; i++) { 
     x = 1 + rand() % 50; 

     if (x == 25) { 
      raise(SIGUSR1); 
     } 
     printf("%4d", i); 


     if (i % 10 == 0) { 
      printf("\n"); 
     } 
     fflush(stdout); 
     sleep(1); 
    } 
    return 0; 
} 


void signalHandler(int signalValue) 
{ 
    signal(SIGINT, signalHandler); 
    int response; 

    printf("%s%d%s\n%s","\nInterrupt signal (", signalValue, ") received.", 
        "Do you wish to continue (1 = yes or 2 = no)? \n"); 

    scanf("%d", &response); 
    if (response != 1) { 
     signal(SIGINT, INThandler); 
    } 
} 


void INThandler(int signalValue) 
{ 
    signal(signalValue, SIG_IGN); 
    printf("\nCtrl-C command detected!"); 
    exit(0); 
} 
 
1 2 3^C 
Interrupt signal (2) received. 
Do you wish to continue (1 = yes or 2 = no)? 
1 
    4 5 6 7^C 
Interrupt signal (2) received. 
Do you wish to continue (1 = yes or 2 = no)? 
1 
    8 9 10 
    11 12^C 
Interrupt signal (2) received. 
Do you wish to continue (1 = yes or 2 = no)? 
1 
    13 14 15 16 17^C 
Interrupt signal (2) received. 
Do you wish to continue (1 = yes or 2 = no)? 

2 
    18 19 20 
    21 22^C 
Ctrl-C command detected!---