红联Linux门户
Linux帮助

Linux信号量sigprocmask使用

发布时间:2014-11-24 09:43:22来源:linux网站作者:chencong112

源程序:

#include <signal.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
double y;
sigset_t intmask;
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;
sigemptyset(&intmask); /* 将信号集合设置为空 */
sigaddset(&intmask,SIGINT); /* 加入中断 Ctrl+C 信号*/
while(1)
{
/*阻塞信号,我们不希望保存原来的集合所以参数为NULL*/
sigprocmask(SIG_BLOCK,&intmask,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");
/* 取消阻塞 */
sigprocmask(SIG_UNBLOCK,&intmask,NULL);
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 也初始化信号集合,只是将信号集合设置为所有信号的集合.

sigaddset 将信号signo 加入到信号集合之中,sigdelset 将信号从信号集合中删除.

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

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


上述源程序,在不带参数执行时,输出是个错误的结果是:Usage:./sigset repeat_factor

带参数执行后的结果将是不断重复如下语言:

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


在输入Ctrl+c后将会停止。