Posted on 2009-11-17 19:55
Prayer 閱讀(1897)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
LINUX/UNIX/AIX
功能描述:
設(shè)定對(duì)信號(hào)屏蔽集內(nèi)的信號(hào)的處理方式(阻塞或不阻塞)。
用法:
#include <signal.h>
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
參數(shù):
how:用于指定信號(hào)修改的方式,可能選擇有三種
SIG_BLOCK //加入信號(hào)到進(jìn)程屏蔽。
SIG_UNBLOCK //從進(jìn)程屏蔽里將信號(hào)刪除。
SIG_SETMASK //將set的值設(shè)定為新的進(jìn)程屏蔽。
set:為指向信號(hào)集的指針,在此專指新設(shè)的信號(hào)集,如果僅想讀取現(xiàn)在的屏蔽值,可將其置為NULL。
oldset:也是指向信號(hào)集的指針,在此存放原來(lái)的信號(hào)集。
返回說(shuō)明:
成功執(zhí)行時(shí),返回0。失敗返回-1,errno被設(shè)為EINVAL。
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
//#include <>
static void sig_quit(int signo)
{
printf("caught SIGQUIT\n");
signal(SIGQUIT, SIG_DFL);//將SIGQUIT的動(dòng)作設(shè)為缺省值
}
int main()
{
sigset_t newmask;
sigset_t oldmask;
sigset_t pendmask;
signal(SIGQUIT, sig_quit);//信號(hào)量捕捉函數(shù),捕捉到SIGQUIT,跳轉(zhuǎn)到函數(shù)指針sig_quit處執(zhí)行
sigemptyset(&newmask);//初始化信號(hào)量集
sigaddset(&newmask, SIGQUIT);//將SIGQUIT添加到信號(hào)量集中
sigprocmask(SIG_BLOCK, &newmask, &oldmask);//將newmask中的SIGQUIT阻塞掉,并保存當(dāng)前信號(hào)屏蔽字
sleep (5);//休眠5秒鐘
sigpending(&pendmask);//檢查信號(hào)是懸而未決的
if (sigismember(&pendmask, SIGQUIT))//SIGQUIT是懸而未決的。所謂懸而未決,是指SIGQUIT被阻塞還沒有被處理
{
printf("\nSIGQUIT pending\n");
}
sigprocmask(SIG_SETMASK, &oldmask, NULL);//恢復(fù)被屏蔽的信號(hào)SIGQUIT
printf("SIGQUIT unblocked\n");
sleep(5);//再次休眠5秒鐘
return (0);
以上示例是APUE P260,
執(zhí)行結(jié)果是
$./a.out
^\
SIGQUIT pending
caught SIGQUIT 在sigprocmask返回之前處理阻塞信號(hào)SIGQUIT,輸出它
SIGQUIT unblocked
^\Quit (coredump)//因?yàn)橐呀?jīng)被設(shè)置為缺省值,所以再次產(chǎn)生SIGQUIT信號(hào),直接退出