标签:
函数sigpending被阻塞发送并且当前被调用该函数的进程挂起的信号,这个信号集通过参数set返回.
#include<signal.h>
int sigpending(sigset_t*set);
Returns:0if OK,-1 on error.
#include"apue.h"
staticvoid 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);/*SIGOUT here will remain pending*/
if(sigpending(&pendmask)<0)
err_sys("sigpending error");
if(sigismember(&pendmask, SIGQUIT))
printf("\nSIGQUIT pending\n");
/*
* Restore signal mask whick unblocks SIGQUIT.
*/
if(sigprocmask(SIG_SETMASK,&oldmask, NULL)<0)
err_sys("SIG_SETMASK error");
printf("SIGQUIT unblocked\n");
sleep(5);/*SIGQUIT here will terminate with core file */
exit(0);
}
staticvoid sig_quit(int signo)
{
printf("caught SIGQUIT\n");
if(signal(SIGQUIT, SIG_DFL)== SIG_ERR)
err_sys("can‘t reset SIGQUIT");
}
执行效果如下图:
os@debian:~/UnixProgram/Chapter10$./10_15.exe
^\^\^\
SIGQUIT pending
caught SIGQUIT
SIGQUIT unblocked
^\Quit
os@debian:~/UnixProgram/Chapter10$./10_15.exe
SIGQUIT unblocked
os@debian:~/UnixProgram/Chapter10$
标签:
原文地址:http://www.cnblogs.com/U201013687/p/5507210.html