Advertisement
Tobiahao

S01_LAB01_08

Nov 12th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. /*
  2. Napisz cztery osobne programy. Każdy z nich powinien obsługiwać wybrany przez
  3. Ciebie sygna ł . Pierwszy z procesów b ę dzie co sekundę wysy ł a ł sygnał do drugiego
  4. procesu, drugi proces pod odebraniu sygna ł u powinien wypisać na ekranie
  5. komunikat, a nast ę pnie przes ł a ć sygnał do procesu trzeciego. Proces trzeci powinien
  6. zachowywać się podobnie jak drugi, a proces czwarty powinien jedynie wypisywać
  7. komunikat na ekranie. Odliczanie czasu w pierwszym procesie nale ż y zrealizować
  8. za pomoc ą SIGALARM.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <signal.h>
  16. #include <wait.h>
  17. #include <stdbool.h>
  18.  
  19. volatile sig_atomic_t first_send_flag = false;
  20. volatile sig_atomic_t second_send_flag = false;
  21. volatile sig_atomic_t third_send_flag = false;
  22.  
  23. void first_send_signal(int sig)
  24. {
  25.     printf("Proces 1: Sygnal wyslany\n");
  26.     first_send_flag = true;
  27. }
  28.  
  29. void first_process(pid_t *pid)
  30. {
  31.     if(signal(SIGALRM, first_send_signal) == SIG_ERR){
  32.         perror("signal");
  33.         exit(EXIT_FAILURE);
  34.     }
  35.  
  36.     alarm(1);
  37.     while(1){
  38.         if(first_send_flag){
  39.             kill((getpid()+1), SIGUSR1);
  40.             first_send_flag = false;
  41.             alarm(1);
  42.         }
  43.     }
  44. }
  45.  
  46. void second_print_and_send(int sig)
  47. {
  48.     printf("Proces 2: Sygnal otrzymany i przeslany dalej\n");
  49.     second_send_flag = true;
  50. }
  51.  
  52. void second_process(pid_t *pid)
  53. {
  54.     if(signal(SIGUSR1, second_print_and_send) == SIG_ERR){
  55.         perror("signal");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.  
  59.     while(1){
  60.         if(second_send_flag){
  61.             kill((getpid()+1), SIGUSR1);
  62.             second_send_flag = false;
  63.         }
  64.     }
  65. }
  66.  
  67. void third_print_and_send(int sig)
  68. {
  69.     printf("Proces 3: Sygnal otrzymany i przeslany dalej\n");
  70.     third_send_flag = true;
  71. }
  72.  
  73. void third_process(pid_t *pid)
  74. {
  75.     if(signal(SIGUSR1, third_print_and_send) == SIG_ERR){
  76.         perror("signal");
  77.         exit(EXIT_FAILURE);
  78.     }
  79.  
  80.     while(1){
  81.         if(third_send_flag){
  82.             kill((getpid()+1), SIGUSR1);
  83.             third_send_flag = false;
  84.         }
  85.     }
  86. }
  87.  
  88. void fourth_print(int sig)
  89. {
  90.     if(sig == SIGUSR1)
  91.         printf("Proces 4: Sygnal otrzymany\n\n");
  92. }
  93.  
  94. void fourth_process(pid_t *pid)
  95. {
  96.     if(signal(SIGUSR1, fourth_print) == SIG_ERR){
  97.         perror("signal");
  98.         exit(EXIT_FAILURE);
  99.     }
  100.  
  101.     while(1){}
  102. }
  103.  
  104. int main(void)
  105. {
  106.     pid_t pid[4];  
  107.  
  108.     for(int i = 0; i < 4; i++){
  109.         usleep(100);
  110.         pid[i] = fork();
  111.         if(pid[i] == -1){
  112.             perror("fork");
  113.             return EXIT_FAILURE;
  114.         }
  115.         else if(pid[i] == 0){
  116.             switch(i){
  117.                 case 0:
  118.                     first_process(pid);
  119.                     break;
  120.                 case 1:
  121.                     second_process(pid);
  122.                     break;
  123.                 case 2:
  124.                     third_process(pid);
  125.                     break;
  126.                 case 3:
  127.                     fourth_process(pid);
  128.                     break;
  129.                 default:
  130.                     return EXIT_FAILURE;
  131.             }
  132.         }
  133.         else{
  134.             waitpid(pid[3], NULL, 0);
  135.         }
  136.     }
  137.  
  138.     return EXIT_SUCCESS;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement