'programming/C_C++'에 해당되는 글 279건

  1. 2016.04.12 signal 함수 구현

반응형


출처 :Advanced Programming in the UNIX® Environment: Second Edition


typedef void Sigfunc(int);   /* for signal handlers */

static Sigfunc *_Signal(int signo, Sigfunc *func);
static Sigfunc *_Signal_Intr(int signo, Sigfunc *func);

/*
* Reliable version of signal(), using POSIX sigaction().
*/
static 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);

}

/*
* a version of the signal function that tries to
* prevent any interrupted system calls from being restarted.
*/
static Sigfunc *_Signal_Intr(int signo, Sigfunc *func)
{
    struct sigaction    act, oact;

    act.sa_handler = func;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;

    #ifdef  SA_INTERRUPT

        act.sa_flags |= SA_INTERRUPT;

    #endif

    if (sigaction(signo, &act, &oact) < 0) {
        return(SIG_ERR);
    }

    return(oact.sa_handler);

}





http://revoman.tistory.com/122



반응형
Posted by 공간사랑
,