如何使Ctrl-c结束程序

时间:2014-12-04 15:20:40

标签: c signals interrupt

我希望程序通过键入CTRL + C来生成SIGINT类型的信号来测试信号的捕获。我不知道,我的程序只计入第一个中断信号并结束程序(直接跳转到INThandler函数)

#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);

}

2 个答案:

答案 0 :(得分:0)

您的代码目前说:

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

有效忽略对signal()的第一次调用。您已安装INThandler作为程序自行发送的SIGUSR1信号的信号处理程序,并且您尚未为SIGINT安装任何信号处理程序。

答案 1 :(得分: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!---