Tobiahao

S01_LAB01_10

Nov 12th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. /*
  2. Ze wzgledow beczeństwa zaleca się , aby w ramach funkcji obs ł uguj ą cej sygnał
  3. wykonywane by ł y tylko proste czynno ś ci, jak np. ustawienie flagi informuj ą cej
  4. o otrzymaniu sygna ł u, a skomplikowane czynno ś ci ż eby by ł y wykonywane w osobnym kodzie. Przedstaw schemat takiego rozwi ą zania stosuj ą c proces
  5. macierzysty i potomny
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <wait.h>
  13. #include <stdbool.h>
  14.  
  15. volatile sig_atomic_t signal_flag = false;
  16.  
  17. void signal_handler(int signum)
  18. {
  19.     signal_flag = true;
  20. }
  21.  
  22. void some_complicated_function()
  23. {
  24.     printf("\nSygnal obsluzony...\n");
  25.     exit(EXIT_SUCCESS);
  26. }
  27.  
  28. int main(void)
  29. {
  30.     pid_t pid;
  31.  
  32.     if(signal(SIGINT, signal_handler) == SIG_ERR){
  33.         perror("signal");
  34.         return EXIT_FAILURE;
  35.     }
  36.  
  37.     pid = fork();
  38.     if(pid == -1){
  39.         perror("fork");
  40.         return EXIT_FAILURE;
  41.     }
  42.     else if(pid == 0){
  43.         printf("Nacisnij CTRL + C aby wyslac sygnal\n");
  44.     }
  45.     else{
  46.         wait(NULL);
  47.         while(1){
  48.             if(signal_flag)
  49.                 some_complicated_function();
  50.                    
  51.         }
  52.     }
  53.  
  54.  
  55.     return EXIT_SUCCESS;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment