[关闭]
@lupnfer 2016-12-16T09:37:58.000000Z 字数 2339 阅读 771

signal

Code


  1. #include <signal.h>
  2. #include <errno.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. void sig_handler(int signum)
  7. {
  8. static int j = 0;
  9. static int k = 0;
  10. static int m = 0;
  11. pthread_t sig_ppid = pthread_self();
  12. // used to show which thread the signal is handled in.
  13. if (signum == SIGUSR1) {
  14. printf("thread %ld, receive SIGUSR1 No. %d\n", sig_ppid, j);
  15. j++;
  16. //SIGRTMIN should not be considered constants from userland,
  17. //there is compile error when use switch case
  18. } else if (signum == SIGRTMIN) {
  19. printf("thread %ld, receive SIGRTMIN No. %d\n", sig_ppid, k);
  20. k++;
  21. } else if(signum == 60)
  22. {
  23. //printf("thread %ld, receive SIG119 No. %d\n", sig_ppid, m);
  24. m++;
  25. }
  26. }
  27. /*
  28. void* worker_thread()
  29. {
  30. pthread_t ppid = pthread_self();
  31. pthread_detach(ppid);
  32. while (1) {
  33. printf("I'm thread %d, I'm alive\n", ppid);
  34. sleep(10);
  35. }
  36. }*/
  37. void* sigmgr_thread(void *pwaitset)
  38. {
  39. sigset_t waitset, oset;
  40. sigset_t *pwaitsetEx = (sigset_t *)pwaitset;
  41. siginfo_t info;
  42. int rc, i;
  43. pthread_t ppid = pthread_self();
  44. pthread_detach(ppid);
  45. printf("I'm thread %d, I'm alive\n", ppid);
  46. sigemptyset(&waitset);
  47. sigaddset(&waitset, SIGRTMIN);
  48. sigaddset(&waitset, SIGUSR1);
  49. sigaddset(&waitset, 60);
  50. if (pthread_sigmask(SIG_BLOCK, &waitset, &oset) != 0) //信号屏蔽
  51. printf("!! Set pthread mask failed\n");
  52. while (1) {
  53. rc = sigwaitinfo(&waitset, &info);
  54. if (rc != -1) {
  55. printf("sigwaitinfo() fetch the signal - %d\n", rc);
  56. sig_handler(info.si_signo);
  57. } else {
  58. printf("sigwaitinfo() returned err: %d; %s\n", errno, strerror(errno));
  59. }
  60. }
  61. }
  62. int main()
  63. {
  64. sigset_t bset, oset;
  65. int i, rc;
  66. pid_t pid = getpid();
  67. pthread_t ppid;
  68. siginfo_t info;
  69. // Block SIGRTMIN and SIGUSR1 which will be handled in
  70. //dedicated thread sigmgr_thread()
  71. // Newly created threads will inherit the pthread mask from its creator
  72. sigemptyset(&bset);
  73. sigaddset(&bset, SIGRTMIN);
  74. sigaddset(&bset, SIGUSR1);
  75. sigaddset(&bset, 60); //自定义信号量
  76. if (pthread_sigmask(SIG_BLOCK, &bset, &oset) != 0) //信号屏蔽
  77. printf("!! Set pthread mask failed\n");
  78. // Create the dedicated thread sigmgr_thread() which will handle
  79. // SIGUSR1 and SIGRTMIN synchronously
  80. pthread_create(&ppid, NULL, sigmgr_thread, (void *)&bset);
  81. // Create 5 worker threads, which will inherit the thread mask of
  82. // the creator main thread
  83. /*
  84. for (i = 0; i < 5; i++) {
  85. pthread_create(&ppid, NULL, worker_thread, NULL);
  86. }*/
  87. while (1) {
  88. rc = sigwaitinfo(&bset, &info);
  89. if (rc != -1) {
  90. printf("sigwaitinfo() fetch the signal - %d\n", rc);
  91. sig_handler(info.si_signo);
  92. } else {
  93. printf("sigwaitinfo() returned err: %d; %s\n", errno, strerror(errno));
  94. }
  95. }
  96. // send out 50 SIGUSR1 and SIGRTMIN signals
  97. for (i = 0; i < 500; i++) {
  98. //kill(pid, SIGUSR1);
  99. sleep(10);
  100. }
  101. exit (0);
  102. }

http://blog.csdn.net/i_am_jojo/article/details/7592219

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注