Advertisement
Guest User

Untitled

a guest
May 19th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9. int main(char argument)
  10. {
  11.    int pipa[2];
  12.    int pipa_2[2];
  13.    int des_fifo;
  14.    const char* subor_fifo="FIFO_PIPA";
  15.    char BUFFER_MAIN[BUFSIZ];
  16.    char buffer[BUFSIZ];
  17.    char buffer_B[BUFSIZ];
  18.    pid_t fork_1;
  19.    pid_t fork_2;
  20.  
  21.                                                              //   vytvorenie fifo + prist.prava
  22.    des_fifo=mkfifo(subor_fifo,0777);    
  23.     if(des_fifo==0) printf("\nFIFO PIPA VYTVORENA\n");
  24.    else { perror("\nNASTALA CHYBA V FIFO => ");  
  25.           printf("\n");  }
  26.  
  27.    if (pipe(pipa) != 0){                           //     pipa 1
  28.     perror("\nNASTALA CHYBA V PIPE_1 => ");
  29.     exit(EXIT_FAILURE); }
  30.    else {printf("PIPA_1 VYTVORENA.");}
  31.  
  32.    if (pipe(pipa_2) != 0){
  33.     perror("\nNASTALA CHYBA V PIPE_2 => ");             //  pipa 2
  34.     printf("\n");
  35.     exit(EXIT_FAILURE); }
  36.     else {printf("\nPIPA_2 VYTVORENA.\n");}
  37.  
  38.  
  39.     fork_1 = fork();                                  //     vytvorenie potomka 1
  40.  
  41.    if (fork_1 == -1) {
  42.        printf("\nNASTALA CHYBA V PROCESE FORK_1 => ");
  43.     printf("\n");
  44.          exit(EXIT_FAILURE);
  45.          }
  46.  
  47.    if (fork_1 == 0) {  
  48.          printf("\nPOTOMOK 1 BOL USPESNE VYTVORENY.\n");                      
  49.  
  50.     fork_2 = fork();                           //       vytvorenie potomka 2
  51.                  
  52.     if (fork_2 == -1) {
  53.             perror("\nNASTALA CHYBA V PROCESE FORK_2 => ");
  54.         printf("\n");
  55.             exit(EXIT_FAILURE);
  56.             }
  57.     if (fork_2 == 0){  
  58.                 printf("POTOMOK 2 BOL USPESNE VYTVORENY.\n");  
  59.  
  60.                                                                //        -- potomok2 --
  61.         read(pipa_2[0], buffer, 1);
  62.  
  63.         if (buffer[0]=='2'){                   //    ak je "2", zapis do FIFA potomok 2
  64.             int des = open(subor_fifo,O_WRONLY);
  65.             if (des<0){
  66.                 perror("\nNASTALA CHYBA V OTVORENI FIFO => ");
  67.                 printf("\n");
  68.                 exit(EXIT_FAILURE);
  69.             }
  70.             write(des,"POTOMOK 2",9);
  71.             close(des);
  72.         }
  73.  
  74.             exit(EXIT_SUCCESS);
  75.     }
  76.     else {                                          //          -- potomok1 --
  77.  
  78.         read(pipa[0], buffer_B, 1);
  79.         if (buffer_B[0]=='1'){     
  80.             int des = open(subor_fifo,O_WRONLY);
  81.             if (des<0){
  82.                 perror("\nCHYBA FIFO : ");
  83.                 printf("\n");
  84.                 exit(EXIT_FAILURE);
  85.             }
  86.             write(des,"POTOMOK 1",9);
  87.             close(des);
  88.         }
  89.         write(pipa_2[1], buffer_B, 1);
  90.        
  91.             exit(EXIT_SUCCESS);
  92.     }
  93.  
  94.    }
  95.    else {                                            //  --  RODIC  + argumenty--    
  96.  
  97.     memset(BUFFER_MAIN, '\0', sizeof(BUFFER_MAIN));
  98.    
  99.      printf("\nVLOZTE ARGUMENT (1-2): ");
  100.      scanf("%c",&argument);
  101.           printf("\n");
  102.            if (argument == '2' || argument == '1' ){
  103.         write(pipa[1],&argument,1);
  104.         int des = open(subor_fifo,O_RDONLY);
  105.         if (des<0){
  106.             perror("\nFIFO CHYBA : ");
  107.             exit(EXIT_FAILURE);
  108.         }
  109.  
  110.         read(des,BUFFER_MAIN,BUFSIZ);
  111.                 printf("-----------------------------------------------------\n");
  112.             printf("Rodic z FIFA NACITAL HLASKU : >>>>  %s  <<<<\n" ,BUFFER_MAIN);
  113.         printf("-----------------------------------------------------\n");
  114.         close(des);
  115.        }
  116.       else printf("CHYBA - ZADAL SI INY ARGUMENT AKO {1} alebo {2}\n");
  117.       printf("\n");
  118.       exit(EXIT_FAILURE);
  119. }
  120.    exit(EXIT_SUCCESS);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement