[英]How to make Ctrl-c to end the program
I want the program to test the trapping of the signal by typing CTRL+C to generate a signal of type SIGINT. 我希望程序通过键入CTRL + C生成SIGINT类型的信号来测试信号的捕获。 I don't know, my program just counts to the first interrupt signal and ends the program (just jumps straight into the INThandler function)
我不知道,我的程序只是计数到第一个中断信号并结束程序(只是直接跳到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);
}
Your code currently says: 您的代码当前显示为:
signal(SIGUSR1, signalHandler);
signal(SIGUSR1, INThandler);
The first call to signal()
is effectively ignored. 对
signal()
的第一次调用实际上被忽略。 You've installed INThandler
as the signal handler for the SIGUSR1
signals your program sends itself, and you have not installed any signal handler for SIGINT
. 您已经安装了
INThandler
作为SIGUSR1
信号的信号处理程序,您的程序发送了该信号,并且尚未为SIGINT
安装任何信号处理程序。
Not sure what you are doing with SIGUSR1. 不知道您在使用SIGUSR1。 If you want to setup a handler to trap SIGINT, just setup the signal handler with signal( SIGINT, signalHandler ).
如果要设置捕获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!---
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.