Tobiahao

S01_LAB02_05

Nov 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. /*
  2. Napisz program, który podzieli się na dwa procesy komunikuj ą ce się przy
  3. pomocy potoków. Komunikacja musi by ć dwukierunkowa.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <wait.h>
  10. #include <fcntl.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14.  
  15. #define BUFFER_SIZE 128
  16.  
  17. volatile sig_atomic_t send_flag = false;
  18.  
  19. void send_handler(int signum)
  20. {
  21.     send_flag = true;
  22. }
  23.  
  24. int main(void)
  25. {
  26.     int fdcp[2]; // children --> parent
  27.     int fdpc[2]; // parent --> children
  28.     pid_t pid;
  29.     char buffer[BUFFER_SIZE];
  30.  
  31.     memset(buffer, '\0', BUFFER_SIZE);
  32.  
  33.     if((pipe(fdcp) == -1) || (pipe(fdpc) == -1)){
  34.         perror("pipe");
  35.         return EXIT_FAILURE;
  36.     }
  37.  
  38.     if(signal(SIGUSR1, send_handler) == SIG_ERR){
  39.         perror("signal");
  40.         return EXIT_FAILURE;
  41.     }
  42.  
  43.     pid = fork();
  44.     if(pid == -1){
  45.         perror("fork");
  46.         return EXIT_FAILURE;
  47.     }
  48.     else if(pid == 0){
  49.         // Wyslij wiadomosc
  50.         printf("Potomek: Podaj wiadomosc do przeslania: ");
  51.         fgets(buffer, BUFFER_SIZE, stdin);
  52.  
  53.         if(write(fdcp[1], buffer, BUFFER_SIZE) == -1){
  54.             perror("write");
  55.             return EXIT_FAILURE;
  56.         }
  57.  
  58.         if(close(fdcp[1]) == -1){
  59.             perror("close");
  60.             return EXIT_FAILURE;
  61.         }  
  62.  
  63.         if(kill(getppid(), SIGUSR1) == -1){
  64.             perror("kill");
  65.             return EXIT_FAILURE;
  66.         }
  67.  
  68.         // Poczekaj i odbierz wiadomosc
  69.         while(!send_flag)
  70.             ;
  71.         send_flag = false;
  72.         memset(buffer, '\0', BUFFER_SIZE);
  73.         if(read(fdpc[0], buffer, BUFFER_SIZE) == -1){
  74.             perror("read");
  75.             return EXIT_FAILURE;
  76.         }
  77.  
  78.         printf("Potomek: Przeslana wiadomosc: %s\n", buffer);
  79.  
  80.         if(close(fdpc[0]) == -1){
  81.             perror("close");
  82.             return EXIT_FAILURE;
  83.         }
  84.     }
  85.     else{
  86.         // Poczekaj i odbierz wiadomosc
  87.         while(!send_flag)
  88.             ;
  89.         send_flag = false;
  90.         if(read(fdcp[0], buffer, BUFFER_SIZE) == -1){
  91.             perror("read");
  92.             return EXIT_FAILURE;
  93.         }
  94.  
  95.         printf("Rodzic: Przeslana wiadomosc: %s\n", buffer);
  96.  
  97.         if(close(fdcp[0]) == -1){
  98.             perror("close");
  99.             return EXIT_FAILURE;
  100.         }
  101.  
  102.         // Wyslij
  103.         printf("Rodzic: Podaj wiadomosc do przeslania: ");
  104.         memset(buffer, '\0', BUFFER_SIZE);
  105.         fgets(buffer, BUFFER_SIZE, stdin);
  106.  
  107.         if(write(fdpc[1], buffer, BUFFER_SIZE) == -1){
  108.             perror("write");
  109.             return EXIT_FAILURE;
  110.         }
  111.  
  112.         if(close(fdpc[1]) == -1){
  113.             perror("close");
  114.             return EXIT_FAILURE;
  115.         }
  116.  
  117.         if(kill(pid, SIGUSR1) == -1){
  118.             perror("kill");
  119.             return EXIT_FAILURE;
  120.         }
  121.        
  122.         wait(NULL);
  123.     }
  124.  
  125.     return EXIT_SUCCESS;
  126. }
Add Comment
Please, Sign In to add comment