int sigsuspend(const sigset_t *sigmask);
此函數用於進程的掛起,sigmask指向一個信號集。當此函數被調用時,sigmask所指向的信號集中的信號將賦值給信號掩碼。之後進程掛起。直到進程捕捉到信號,並調用處理函數返回時,函數sigsuspend返回。信號掩碼恢復為信號調用前的值,同時將errno設為EINTR。進程結束信號可將其立即停止。
#include <stdio.h>
#include <signal.h>
void checkset();
void func();
void main()
{
sigset_tblockset,oldblockset,zeroset,pendmask;
printf("pid:%ld\n",(long)getpid());
signal(SIGINT,func);
sigemptyset(&blockset);
sigemptyset(&zeroset);
sigaddset(&blockset,SIGINT);
sigprocmask(SIG_SETMASK,&blockset,&oldblockset);
checkset();
sigpending(&pendmask);
if(sigismember(&pendmask,SIGINT))
printf("SIGINTpending\n");
if(sigsuspend(&zeroset)!= -1)
{
printf("sigsuspenderror\n");
exit(0);
}
printf("afterreturn\n");
sigprocmask(SIG_SETMASK,&oldblockset,NULL);
printf("SIGINTunblocked\n");
}
void checkset()
{ sigset_tset;
printf("checksetstart:\n");
if(sigprocmask(0,NULL,&set)<0)
{
printf("checksetsigprocmask error!!\n");
exit(0);
}
if(sigismember(&set,SIGINT))
printf("sigint\n");
if(sigismember(&set,SIGTSTP))
printf("sigtstp\n");
if(sigismember(&set,SIGTERM))
printf("sigterm\n");
printf("checksetend\n");
}
void func()
{
printf("hellofunc\n");
}
