【问题标题】:How to make Ctrl-c to end the program如何让Ctrl-c结束程序
【发布时间】:2014-12-04 23:43:37
【问题描述】:

我希望程序通过键入 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);

}

【问题讨论】:

  • 首先设置signalHandler 来处理SIGUSR1,然后设置INThandler 来处理它。你预计会发生什么?

标签: c signals interrupt


【解决方案1】:

您的代码当前显示:

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

signal() 的第一次调用被有效忽略。您已经安装了INThandler 作为您的程序自己发送的SIGUSR1 信号的信号处理程序,并且您还没有为SIGINT 安装任何信号处理程序。

【讨论】:

  • 不敢相信我犯了这么愚蠢的错误,现在它起作用了..谢谢!
  • 另请参阅How to avoid using printf() in a signal handler?,了解您可以在信号处理程序中做什么和不能做什么。您的信号是同步发出的,而不是异步发出的,这大大降低了所涉及的风险。
【解决方案2】:

不确定您在使用 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 收到中断信号 (2)。 您想继续吗(1 = 是或 2 = 否)? 1 4 5 6 7^C 收到中断信号 (2)。 您想继续吗(1 = 是或 2 = 否)? 1 8 9 10 11 12^C 收到中断信号 (2)。 您想继续吗(1 = 是或 2 = 否)? 1 13 14 15 16 17^C 收到中断信号 (2)。 您想继续吗(1 = 是或 2 = 否)? 2 18 19 20 21 22^C 检测到 Ctrl-C 命令!---

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多