Advertisement
Guest User

example 1

a guest
Apr 8th, 2012
3,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. /*
  2. Output:
  3. ################################################################################
  4. PID = [6498]
  5. th2 = 140463568520960
  6. th1 = 140463576913664
  7.  
  8. The signal handler for thread: '140463592892192' receive: SIGUSR1
  9.  
  10. The signal handler for thread: '140463568520960' receive: SIGUSR1
  11. th2 = exit
  12.  
  13. The signal handler for thread: '140463576913664' receive: SIGUSR1
  14. th1 = exit
  15. ################################################################################
  16. */
  17.  
  18. #include <iostream>
  19. #include <pthread.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #include <sstream>
  23.  
  24. void hdl(int sig)
  25. {
  26.     std::cout << std::endl;
  27.  
  28.     std::string signal;
  29.     if(sig == SIGUSR1)
  30.         signal = "SIGUSR1";
  31.     else if(sig == SIGUSR2)
  32.         signal = "SIGUSR2";
  33.     else
  34.         signal = "Something else";
  35.        
  36.     std::cout << "The signal handler for thread: '" << pthread_self()
  37.                 << "' receive: " << signal << std::endl;
  38. }
  39.  
  40. void show_me(void* arg)
  41. {
  42.     std::stringstream str;
  43.     str << (char*)arg << " = " << pthread_self() << std::endl;
  44.     std::cout << str.str();
  45. }
  46.  
  47. void show_exit(void* arg)
  48. {
  49.     std::stringstream str;
  50.     str << (char*)arg << " = " << "exit" << std::endl;
  51.     std::cout << str.str();
  52. }
  53.  
  54. void show_signal(int sig)
  55. {
  56.     if(sig == SIGUSR1)
  57.         std::cout << "SIGUSR1 was received\n";
  58.     else if(sig == SIGUSR2)
  59.         std::cout << "SIGUSR2 was received\n";
  60.     else
  61.         std::cout << "Received = " << sig << std::endl;
  62. }
  63.  
  64. void* th_sleep(void* arg)
  65. {
  66.     show_me(arg);
  67.     sleep(30);
  68.     show_exit(arg);  
  69. }
  70.  
  71. int main()
  72. {
  73.     std::cout << "PID = [" << getpid() << "]" << std::endl;
  74.    
  75.     struct sigaction act;
  76.     memset(&act, 0, sizeof(act));
  77.     act.sa_handler = hdl;
  78.     sigset_t   set;
  79.     sigemptyset(&set);                                                            
  80.     sigaddset(&set, SIGUSR1);
  81.     sigaddset(&set, SIGUSR2);
  82.     act.sa_mask = set;
  83.     sigaction(SIGUSR1, &act, 0);
  84.     sigaction(SIGUSR2, &act, 0);
  85.    
  86.     pthread_t th1, th2;
  87.    
  88.     char s_th_1[] = "th1";
  89.     char s_th_2[] = "th2";
  90.    
  91.     pthread_create(&th1, NULL, th_sleep, (void*)s_th_1);
  92.     pthread_create(&th2, NULL, th_sleep, (void*)s_th_2);
  93.  
  94.     sleep(2);
  95.     raise(SIGUSR1);
  96.     pthread_kill(th1, SIGUSR1);
  97.     pthread_kill(th2, SIGUSR1);
  98.  
  99.     pthread_join(th1, NULL);
  100.     pthread_join(th2, NULL);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement