只使用一个简单的信号处理程序怎么样?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
static void sigint_handler( int signum );
volatile static int done = 0;
int main( int argc, char *argv[] )
{
if( signal( SIGINT, sigint_handler ) == SIG_ERR ) {
perror( "signal()" );
exit(1);
}
while( !done ) {
(void)printf( "working...\n" );
(void)sleep( 1 );
}
(void)printf( "got SIGINT\n" );
return 0;
}
void sigint_handler( int signum )
{ done = 1; }
Run Code Online (Sandbox Code Playgroud)
编辑:变得done不稳定,感谢 Joseph Quinsey 在评论中指出了这一点。请参阅此问题以获取相关讨论,还有这篇文章。
| 归档时间: |
|
| 查看次数: |
1234 次 |
| 最近记录: |