티스토리 뷰

// sigaction() 함수 사용 예제 프로그램
 
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>

static void sigint_handler(int signo) ;
static void sigquit_handler(int signo) ;

int main(void)
{
    struct sigaction act_int, act_quit ;

    act_int.sa_handler = sigint_handler ;
    sigemptyset(&act_int.sa_mask) ;
    sigaddset(&act_int.sa_mask, SIGQUIT) ;
    act_int.sa_flags = 0;

    if ( sigaction(SIGINT, &act_int, NULL) < 0 )
    {
        printf("sigaction() error\n") ;
        exit(-1) ;
    }

    act_quit.sa_handler = sigquit_handler ;
    sigemptyset(&act_quit.sa_mask) ;
    sigaddset(&act_quit.sa_mask, SIGINT) ;
    act_int.sa_flags = 0 ;

    if ( sigaction(SIGQUIT, &act_quit, NULL ) < 0 )
    {
        printf("sigaction() error\n") ;
        exit(-1) ;
    }

    pause() ;
}

static void sigint_handler(int signo)
{
    printf("Signal handler for SIGINT : %d\n", signo) ;
    printf("SIGQUIT signal is blocked : %d\n", signo) ;
    printf("sleep 10 sec\n") ;
    sleep(10) ;
    printf("Signal handler for SIGINT ended\n") ;
}

static void sigquit_handler(int signo)
{
    printf("Signal handler for SIGQUIT : %d\n", signo) ;
    printf("SIGINT signal is blocked : %d\n", signo) ;
    printf("sleep 10 sec\n") ;
    sleep(10) ;
    printf("Signal handler for SIGQUIT ended\n") ;
}






댓글