10.13 sigpending函数


函数sigpending被阻塞发送并且当前被调用该函数的进程挂起的信号,这个信号集通过参数set返回.

 
   
  1. #include
  2. int sigpending(sigset_t *set);
  3. Returns:0 if OK,-1 on error.

Example

 
   
  1. #include "apue.h"
  2. static void sig_quit(int);
  3. int main(void)
  4. {
  5. sigset_t newmask,oldmask,pendmask;
  6. if(signal(SIGQUIT, sig_quit) == SIG_ERR)
  7. err_sys("can't catch SIGQUIT");
  8. /*
  9. * Block SIGQUIT and save current signal mask
  10. *
  11. */
  12. sigemptyset(&newmask);
  13. sigaddset(&newmask, SIGQUIT);
  14. if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
  15. err_sys("SIG_BLOCK error");
  16. sleep(5); /*SIGOUT here will remain pending*/
  17. if(sigpending(&pendmask) < 0)
  18. err_sys("sigpending error");
  19. if(sigismember(&pendmask, SIGQUIT))
  20. printf("\nSIGQUIT pending\n");
  21. /*
  22. * Restore signal mask whick unblocks SIGQUIT.
  23. */
  24. if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
  25. err_sys("SIG_SETMASK error");
  26. printf("SIGQUIT unblocked\n");
  27. sleep(5); /*SIGQUIT here will terminate with core file */
  28. exit(0);
  29. }
  30. static void sig_quit(int signo)
  31. {
  32. printf("caught SIGQUIT\n");
  33. if(signal(SIGQUIT, SIG_DFL) == SIG_ERR)
  34. err_sys("can't reset SIGQUIT");
  35. }

执行效果如下图:

 
   
  1. os@debian:~/UnixProgram/Chapter10$ ./10_15.exe
  2. ^\^\^\
  3. SIGQUIT pending
  4. caught SIGQUIT
  5. SIGQUIT unblocked
  6. ^\Quit
  7. os@debian:~/UnixProgram/Chapter10$ ./10_15.exe
  8. SIGQUIT unblocked
  9. os@debian:~/UnixProgram/Chapter10$


来自为知笔记(Wiz)


转载于:https://www.cnblogs.com/U201013687/p/5518361.html

你可能感兴趣的:(10.13 sigpending函数)