- #include <signal.h>
-
int sigprocmask(int how,const sigset_t * restrict set,sigset_t * restrict oset);
- int sigpending(sigset_t * set)
- 信号集被阻塞而不能递送,直至解除阻塞
- int sigemptyset(sigset_t *set)
- 初始化有set指向的信号集,清楚其中所有的信号
how 对信号集的处理方式,
newmask 新的信号集
oldmask 旧的信号集
如果oldmask不空,则将原有的信号集放到oldmask中
- #include "apue.h"
-
-
static void sig_quit(int);
-
-
int main(void)
-
{
-
sigset_t newmask,oldmask,pendmask;
-
if (signal(SIGQUIT,sig_quit) == SIG_ERR)
-
err_sys("can't catch sigquit");
-
/*
-
* Block sigquit and save current signal mask
-
*/
-
sigemptyset(&newmask);
-
sigaddset(&newmask,SIGQUIT);
-
if (sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
-
err_sys("sig_block error");
-
sleep(5);
-
/* sigquit here will remain pending */
-
if (sigpending(&pendmask) < 0)
-
err_sys("sigpending error");
-
if (sigismember(&pendmask,SIGQUIT))
-
printf("\n SIGQUIT pending \n");
-
-
/*
-
* reset the signal mask which unblock sigquit
-
*/
-
if (sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
-
err_sys("sig_setmask error");
-
printf("sig_quit unblock\n");
-
sleep(5);
-
exit(0);
-
}
-
static void
-
sig_quit(int signo)
-
{
-
printf( "caught sigquit \n");
-
if (signal(SIGQUIT,SIG_DFL) == SIG_ERR)
-
err_sys("can't reset sigquit\n");
-
}#include "apue.h"
-
-
static void sig_quit(int);
-
-
int main(void)
-
{
-
sigset_t newmask,oldmask,pendmask;
-
if (signal(SIGQUIT,sig_quit) == SIG_ERR)
-
err_sys("can't catch sigquit");
-
/*
-
* Block sigquit and save current signal mask
-
*/
-
sigemptyset(&newmask);
-
sigaddset(&newmask,SIGQUIT);
-
if (sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
-
err_sys("sig_block error");
-
sleep(5);
-
/* sigquit here will remain pending */
-
if (sigpending(&pendmask) < 0)
-
err_sys("sigpending error");
-
if (sigismember(&pendmask,SIGQUIT))
-
printf("\n SIGQUIT pending \n");
-
-
/*
-
* reset the signal mask which unblock sigquit
-
*/
-
if (sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
-
err_sys("sig_setmask error");
-
printf("sig_quit unblock\n");
-
sleep(5);
-
exit(0);
-
}
-
static void
-
sig_quit(int signo)
-
{
-
printf( "caught sigquit \n");
-
if (signal(SIGQUIT,SIG_DFL) == SIG_ERR)
-
err_sys("can't reset sigquit\n");
-
}
- gcc -Wall c10-11 c10-11.c error.c
- ./c10-11
-
-
^\
-
-
SIGQUIT pending
-
-
caught sigquit
-
-
sig_quit unblock
- $ ./c10-11
^\^\^\^\^\^\^\^\^\^\^\^\
SIGQUIT pending
caught sigquit
sig_quit unblock
本文对信号集的处理方式是:
为了阻塞某信号,保留了 旧的信号屏蔽字,为了接触对该信号的阻塞,用旧的信号屏蔽字重新设置
进程信号屏蔽字
另外一种方法是 使用sig_unblock解阻塞信号,但是这并不能保证信号就解阻塞,原因是信号在进程设置阻塞以前就有可能是阻塞的,这样你只能解阻塞你自己阻塞的信号,实际上信号仍旧处于阻塞状态。
阅读(1223) | 评论(0) | 转发(0) |