|
/*sigaction.c*/
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
/*自定义的信号处理函数*/
void my_func(int signum)
{
printf("\n%d, If you want to quit,please try SIGQUIT\n", signum);
}
int main()
{
sigset_t set;
sigset_t tmpset;
struct sigaction action1,action2;
printf( "sizeof(sigset_t) = %d\n", sizeof(sigset_t) );
printf( "sizeof(struct sigaction) = %d\n", sizeof(struct sigaction) );
/*初始化信号集为空*/
if (sigemptyset(&set)<0)
perror("sigemptyset ");
/*将相应的信号加入信号集*/
if (sigaddset(&set,SIGQUIT)<0)
perror("sigaddset");
if (sigaddset(&set,SIGINT)<0)
perror("sigaddset");
/*设置信号集屏蔽字为阻塞*/
if (sigprocmask(SIG_BLOCK,&set,NULL)<0)
perror("sigprocmask");
else
{
printf("blocked\n");
sleep(1);
}
/*设置信号集屏蔽字为非阻塞*/
if (sigprocmask(SIG_UNBLOCK,&set,NULL)<0)
perror("sigprocmask");
else
printf("unblock\n");
if (sigismember(&set,SIGINT))
{
sigemptyset(&action1.sa_mask);
action1.sa_handler=my_func;
sigaction(SIGINT,&action1,NULL);
}
if (sigismember(&set,SIGQUIT))
{
sigemptyset(&action2.sa_mask);
action2.sa_handler = SIG_DFL;
sigaction(SIGTERM,&action2,NULL);
}
/*对相应的信号进行循环处理*/
while(1)
{
sleep(1);
}
} |
|