guozhiwei 发表于 2013-1-26 12:31:07

sigaction signal 信号 posix

<div class="Section0">
<div class="Section0">#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void show_handler(int sig)
{
    printf("I got signal %d\n", sig);
    static int count;
    count = count+1;
    printf("count=%d\n",count);
    sleep(50);
}

int main(void)
{
    int i = 0;
    struct sigaction act, oldact;
    act.sa_handler = show_handler;
    sigaddset(&act.sa_mask, SIGINT); 
//act.sa_flags = SA_RESETHAND | SA_NODEFER; 
//show_handler中sleep了50秒,如果设置SA_NODEFER,信号处理函数将不会阻塞,该进程连续收到多个SIGINT信号,它都会处理,即show_handler中的printf都会打印,如果没有设置SA_NODEFER,show_handler执行到sleep(50),在这50秒期间,再次收到的SIGINT信号将不会处理,printf将不会执行
//act.sa_flags = SA_NODEFER; 
//SA_RESETHAND设置以后,下次再接收到SIGINT信号就会按照系统默认的处理方式去执行了,即终止进程,而不会去调用show_handler函数
    act.sa_flags = 0; 

    sigaction(SIGINT, &act, &oldact);
    while(1) {
        sleep(0.1);
        printf("sleeping %d\n", i);
        i++;
    }
}

页: [1]
查看完整版本: sigaction signal 信号 posix