Advertisement
Guest User

example 2

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