Advertisement
Tobiahao

S01_LAB01_07

Nov 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. /*
  2. Napisz program, który stworzy dwa procesy. Proces rodzicielski wy ś le do potomka
  3. sygnał SIGINT (można go wysłać „ręcznie” naciskając na klawiaturze równocze ś nie
  4. Ctrl + C). Proces potomny powinien ten sygnał obsłużyć za pomocą napisanej przez
  5. Ciebie funkcji.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12.  
  13. void sigint_handler(int signum)
  14. {
  15.     printf("\nSygnal SIGINT obsluzony przez potomka!\n");
  16.     exit(EXIT_SUCCESS);
  17. }
  18.  
  19. int main(void)
  20. {
  21.     pid_t pid;
  22.  
  23.     pid = fork();
  24.     if(pid == 0){
  25.         // Potomek
  26.         if(signal(SIGINT, sigint_handler) == SIG_ERR){
  27.             fprintf(stderr, "Signal error");
  28.             return EXIT_FAILURE;
  29.         }
  30.         while(1){}
  31.     }
  32.     else if(pid > 0){
  33.         // Rodzic
  34.         printf("Wcisnij CTRL + C, aby wyslac sygnal SIGINT\n");
  35.         while(1){}
  36.     }
  37.     else if(pid == -1){
  38.         // Blad
  39.         perror("fork");
  40.         return EXIT_FAILURE;
  41.     }
  42.  
  43.     return EXIT_SUCCESS;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement