- #include "apue.h"
-
-
volatile sig_atomic_t quitflag ; /* set nonzero by signal handler */
-
static void
-
sig_int(int signo)
-
{
-
printf("\n interrupt ");
-
}
-
static void
-
sig_quit(int signo)
-
{
-
quitflag = 1; /* set flag for main loop */
-
-
}
-
int main(void )
-
{
-
sigset_t newmask,oldmask,zeromask;
-
-
if (signal(SIGINT,sig_int) == SIG_ERR)
-
err_sys("signal(SIGINT) error");
-
if (signal(SIGQUIT,sig_quit) == SIG_ERR)
-
err_sys("signal(sigquit) error");
-
sigemptyset(&zeromask);
-
sigemptyset(&newmask);
-
sigaddset(&newmask,SIGQUIT);
-
-
/*
-
*block sigquit and save current signal mask
-
*/
-
if (sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
-
err_sys("SIGPROCMASK block error");
-
while (quitflag == 0)
-
sigsuspend(&zeromask);
-
/*
-
*sigquit has been caught and is now block,do whatever
-
*/
-
quitflag = 0;
-
-
/*
-
* reset signal mask which unblock sigquit
-
*/
-
if (sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
-
err_sys("SIG_SETMASK,ERROR");
-
exit(0);
-
}
- #include "apue.h"
-
/* reliable version of signal() ,using posix signaction()*/
-
Sigfunc *
-
signal(int signo,Sigfunc * func)
-
{
-
struct sigaction act,oact;
-
-
act.sa_handler = func;
-
sigemptyset(&act.sa_mask);
-
act.sa_flags = 0;
-
if ( signo == SIGALRM){
-
#ifdef SA_INTERRUPT
-
act.sa_flags |= SA_INTERRUPT;
-
#endif
-
} else{
-
#ifdef SA_RESTART
-
act.sa_flags |= SA_RESTART;
-
#endif
-
}
-
if (sigaction(signo,&act,&oact) < 0)
-
return(SIG_ERR);
-
return(oact.sa_handler);
-
}
output of program :
- ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ./c10-16
-
^C
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^C interrupt
-
^\ interrupt
我的结果和书上面的不一致,第一次的中断信号被丢失,不知道什么原因?
我试过多次都是这个结果,期待高手解决,signal程序是c10-12
阅读(970) | 评论(0) | 转发(0) |