1 #include <unistd.h> 2 #include <signal.h> 3 #include <stdio.h> 4 5 //设置绑定的信号函数句柄 6 void sig_handler(int signo){ 7 //打印接收到的信号 8 printf("%d\n", signo); 9 } 10 int main(){ 11 struct sigaction act, oact; 12 sigset_t newmask, oldmask, suspmask; 13 while(1){ 14 act.sa_handler = sig_handler; 15 sigemptyset(&(act.sa_mask)); 16 act.sa_flags = 0; 17 //内核中注册SIGALRM信号 18 sigaction(SIGALRM, &act, &oact); 19 20 sigemptyset(&newmask); 21 sigaddset(&newmask, SIGALRM); 22 //屏蔽SIGALRM信号 23 sigprocmask(SIG_BLOCK, &newmask, &oldmask); 24 //设置时钟 25 alarm(3); 26 //进程挂起等待时钟超时 27 //pause(); 28 sigdelset(&suspmask, SIGALRM); 29 //解除SIGALRM信号屏蔽并等待 30 sigsuspend(&suspmask); 31 //取消闹钟 32 alarm(0); 33 //3秒钟时间过后发送时钟信号,SIGALRM信号递达后suspend自动恢复屏蔽 34 sigaction(SIGALRM, &oact, NULL); 35 //解除屏蔽 36 sigprocmask(SIG_SETMASK, &oldmask, NULL); 37 };//穷忙 38 return 0; 39 }
浙公网安备 33010602011771号