/**********************************************************************
*NAME
*
*DESCRIPTION
* This program Using sigsuspend to wait for a global variable to be set.
**********************************************************************/
#include "common.h"
#include "error.h"
volatile sig_atomic_t quitflag; /* set nonzero by signal handler */
static void
sig_int (int signo) /* one signal handler for SIGINT and SIGQUIT */
{
if (signo == SIGINT)
printf("\ninterrupt\n");
else if (signo == SIGQUIT)
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_int) == 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("SIG_BLOCK error");
while (quitflag == 0)
sigsuspend(&zeromask);
/*
* SIGQUIT has been caught and is now blocked; do whatever.
*/
quitflag = 0;
/*
* Reset signal mask which unblocks SIGQUIT.
*/
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");
exit(0);
}
책에 나와있는 정상적인 실행화면은 다음과 같습니다.
$ ./a.out
^? type the interrupt character
interrupt
^? type the interrupt character again
interrupt
^? and again
interrupt
^? and again
interrupt
^? and again
interrupt
^? and again
interrupt
^? and again
interrupt
^\ $ now terminate with quit character
근데 제 피시는 한번 인터럽트 뜨고 바로 종료되는데 이거 뭔가요 ;;;
댓글 0