Tobiahao

S01_LAB02_07

Nov 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. /*
  2. Napisz program, w którym komunikacja mi ę dzy spokrewnionymi procesami
  3. b ę dzie odbywa ł a się w jednym kierunku za pomocą łą cza nienazwanego,
  4. a w drugim za pomoc ą łą cza nazwanego.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <wait.h>
  11. #include <signal.h>
  12. #include <fcntl.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <string.h>
  16. #include <stdbool.h>
  17.  
  18. #define BUFFER_SIZE 512
  19. #define FIFO_DIR "./fifo"
  20.  
  21. volatile sig_atomic_t send_flag = false;
  22.  
  23. void send_handler(int signum)
  24. {
  25.     send_flag = true;
  26. }
  27.  
  28. int main(void)
  29. {
  30.     pid_t pid;
  31.     int pipefd[2];
  32.     int fifofd;
  33.     char buffer[512];
  34.  
  35.     memset(buffer, '\0', BUFFER_SIZE);
  36.     if(signal(SIGUSR1, send_handler) == SIG_ERR){
  37.         perror("signal");
  38.         return EXIT_FAILURE;
  39.     }
  40.  
  41.     if(pipe(pipefd) == -1){
  42.         perror("pipe");
  43.         return EXIT_FAILURE;
  44.     }
  45.  
  46.         if(mkfifo(FIFO_DIR, 0600) == -1){
  47.         perror("mkfifo");
  48.         return EXIT_FAILURE;
  49.     }
  50.  
  51.     pid = fork();
  52.     if(pid == -1){
  53.         perror("fork");
  54.         return EXIT_FAILURE;
  55.     }  
  56.     else if(pid == 0){
  57.         // Wyslij
  58.         printf("Podaj wiadomosc do wyslania laczem nienazwanym: ");
  59.         fgets(buffer, BUFFER_SIZE, stdin);
  60.    
  61.         if(write(pipefd[1], buffer, BUFFER_SIZE) == -1){
  62.             perror("write");
  63.             return EXIT_FAILURE;
  64.         }
  65.  
  66.         if(close(pipefd[1]) == -1){
  67.             perror("close");
  68.             return EXIT_FAILURE;
  69.         }
  70.        
  71.         if(kill(getppid(), SIGUSR1) == -1){
  72.             perror("kill");
  73.             return EXIT_FAILURE;
  74.         }
  75.  
  76.         // Odbierz
  77.         while(!send_flag)
  78.             ;
  79.         send_flag = false;
  80.    
  81.         memset(buffer, '\0', BUFFER_SIZE);
  82.         if((fifofd = open(FIFO_DIR, O_RDONLY)) == -1){
  83.             perror("fifofd");
  84.             return EXIT_FAILURE;
  85.         }
  86.  
  87.         if(read(fifofd, buffer, BUFFER_SIZE) == -1){
  88.             perror("read");
  89.             return EXIT_FAILURE;
  90.         }
  91.  
  92.         if(close(fifofd) == -1){
  93.             perror("close");
  94.             return EXIT_FAILURE;
  95.         }
  96.  
  97.         printf("Przeslana wiadomosc: %s\n", buffer);
  98.     }
  99.     else{
  100.         // Odbierz
  101.         while(!send_flag)
  102.             ;
  103.         send_flag = false;
  104.  
  105.         if(read(pipefd[0], buffer, BUFFER_SIZE) == -1){
  106.             perror("read");
  107.             return EXIT_FAILURE;
  108.         }
  109.  
  110.         if(close(pipefd[0]) == -1){
  111.             perror("close");
  112.             return EXIT_FAILURE;
  113.         }
  114.  
  115.         printf("Przeslana wiadomosc: %s\n", buffer);
  116.  
  117.         // Wyslij
  118.         memset(buffer, '\0', BUFFER_SIZE);
  119.         printf("Podaj odpowiedz, ktora zostanie wyslana laczem nazwanym: ");
  120.         fgets(buffer, BUFFER_SIZE, stdin);
  121.  
  122.         if(kill(pid, SIGUSR1) == -1){
  123.             perror("kill");
  124.             return EXIT_FAILURE;
  125.         }
  126.  
  127.         if((fifofd = open(FIFO_DIR, O_WRONLY)) == -1){
  128.             perror("open");
  129.             return EXIT_FAILURE;
  130.         }
  131.  
  132.         if(write(fifofd, buffer, BUFFER_SIZE) == -1){
  133.             perror("write");
  134.             return EXIT_FAILURE;
  135.         }
  136.  
  137.         if(close(fifofd) == -1){
  138.             perror("close");
  139.             return EXIT_FAILURE;
  140.         }
  141.  
  142.         if(unlink(FIFO_DIR) == -1){
  143.             perror("unlink");
  144.             return EXIT_FAILURE;
  145.         }
  146.        
  147.         wait(NULL);
  148.     }
  149.  
  150.     return EXIT_SUCCESS;   
  151. }
Add Comment
Please, Sign In to add comment