Tobiahao

S01_WATKI_05

Dec 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /*
  2. Zademonstruj działanie funkcji pthread kill() wysyłając do wątku sygnał, dla którego będzie on
  3. miał własną procedurę obsługi. Do podmiany procedury obsługi wykorzystaj funkcję sigaction().
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9. #include <signal.h>
  10. #include <unistd.h>
  11.  
  12. static void signal_handler(int signum, siginfo_t *info, void *context)
  13. {
  14.     printf("Sygnal zostal obsluzony przez watek!\n");
  15.     exit(EXIT_SUCCESS);
  16. }
  17.  
  18. void *thread_function(void *p)
  19. {
  20.     struct sigaction act;
  21.     act.sa_sigaction = &signal_handler;
  22.  
  23.     if(sigaction(SIGUSR1, &act, NULL) == -1){
  24.         perror("sigaction");
  25.         return (void *)EXIT_FAILURE;
  26.     }
  27.  
  28.     while(1){}
  29.  
  30.     return (void *)EXIT_FAILURE;
  31. }
  32.  
  33. int main(void)
  34. {
  35.     pthread_t thread;
  36.  
  37.     pthread_create(&thread, NULL, thread_function, (void *)NULL);
  38.  
  39.     sleep(1);
  40.     printf("Wysylam sygnal do watku...\n");
  41.     pthread_kill(thread, SIGUSR1);
  42.     pthread_join(thread, NULL);
  43.  
  44.     return EXIT_SUCCESS;
  45. }
Add Comment
Please, Sign In to add comment