429人阅读 评论(0)

#include <signal.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
double y;
int i,repeat_factor;
if(argc!=2)
{
fprintf(stderr,"Usage：%s repeat_factor\n\a",argv[0]);
exit(1);
}
if((repeat_factor=atoi(argv[1]))<1)repeat_factor=10;
while(1)
{
/*阻塞信号,我们不希望保存原来的集合所以参数为NULL*/
fprintf(stderr,"SIGINT signal blocked\n");
for(i=0;i<repeat_factor;i++)y=sin((double)i);
fprintf(stderr,"Blocked calculation is finished\n");
/* 取消阻塞 */
fprintf(stderr,"SIGINT signal unblocked\n");
for(i=0;i<repeat_factor;i++)y=sin((double)i);
fprintf(stderr,"Unblocked calculation is finished\n");
}
exit(0);
}

sigemptyset 函数初始化信号集合set,将set 设置为空.

sigfillset 也初始化信号集合,只是将信号集合设置为所有信号的集合.

sigismember 查询信号是否在信号集合之中.s

igprocmask 是最为关键的一个函数.在使用之前要先设置好信号集合set.这个函数的作用是将指定的信号集合set 加入到进程的信号阻塞集合之中去,如果提供了oset 那么当前的进程信号阻塞集合将会保存在oset 里面.参数how 决定函数的操作方式：
SIG_BLOCK：增加一个信号集合到当前进程的阻塞集合之中.
SIG_UNBLOCK：从当前的阻塞集合之中删除一个信号集合.

SIGINT signal blocked
Blocked calculation is finished
SIGINT signal unblocked
Unblocked calculation is finished
SIGINT signal blocked
Blocked calculation is finished
SIGINT signal unblocked
Unblocked calculation is finished
SIGINT signal blocked
Blocked calculation is finished
SIGINT signal unblocked
Unblocked calculation is finished
SIGINT signal blocked
Blocked calculation is finished
SIGINT signal unblocked
Unblocked calculation is finished
SIGINT signal blocked
Blocked calculation is finished
SIGINT signal unblocked
Unblocked calculation is finished

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：976067次
• 积分：17605
• 等级：
• 排名：第541名
• 原创：741篇
• 转载：522篇
• 译文：0篇
• 评论：105条
阅读排行
最新评论