我读过“APUE”,我发现10.11的例子无法创造正确的答案。 10.11文件是:
static void sig_quit( int );
int main( void )
{
sigset_t newmask, oldmask, pendmask;
if ( signal( SIGQUIT, sig_quit ) == SIG_ERR )
err_sys( "can't catch SIGQUIT" );
sigemptyset( &newmask );
sigaddset( &newmask, SIGQUIT );
/* block SIGQUIT and save current signal mask */
if ( sigprocmask( SIG_BLOCK, &newmask, &oldmask ) < 0 )
err_sys( "SIG_BLOCK error" );
sleep( 5 ); /* SIGQUIT here will remain pending */
if ( sigpending( &pendmask ) < 0 )
err_sys( "sigpending error" );
if ( sigismember( &pendmask, SIGQUIT ) )
printf( "\nSIGQUIT pengding\n" );
/* reset signal mask which unblocks SIGQUIT */
if ( sigprocmask( SIG_SETMASK, &oldmask, NULL ) > 0 )
err_sys( "SIG_SETMASK error" );
printf( "SIGQUIT unblocked\n" );
sleep( 5 ); /* SIGQUIT here will terminate with core file */
exit( 0 );
}
static void sig_quit( int signo )
{
printf( "caught SIGQUIT\n" );
if ( signal( SIGQUIT, SIG_DFL ) == SIG_ERR )
err_sys( "can't reset SIG_QUIT" );
return ;
}
当我执行./a.out并在不到5秒内键入ctrl + c时,程序将立即结束。 如果它在“APUE”中以正确的方式工作,它将打印:
^\
SIGQUIT pending
caught SIGQUIT
SIGQUIT unblocked
^\Quit(coredump)
我是一个学习信号的新人。请问我如何理解为什么? 当其他错误出现时,我可以使用gdb。但信号,请,谢谢 我在ubuntu 10.04中工作