Advertisement
weeez

pipe.c

Nov 12th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h> // for pipe()
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7.  
  8. //
  9. // unnamed pipe example
  10. //
  11. int main(int argc, char *argv[])
  12. {
  13.    
  14.     srand(time(NULL));
  15.     int r = (rand() % 10)+1;
  16.     printf("%d\n",r);
  17.    
  18.     int tomb[r];
  19.     int i;
  20.     for(i = 0; i < r; i++){
  21.         tomb[i] = rand() % 10;
  22.     }
  23.    
  24.     for(i = 0; i < r; i++){
  25.         printf("tomb elemeke: %d\n", tomb[i]);
  26.     }
  27.    
  28.     int pipefd[2]; // unnamed pipe file descriptor array
  29.     int pipefd2[2]; // unnamed pipe file descriptor array
  30.     pid_t pid;
  31.     char sz[100];  // char array for reading from pipe
  32.    
  33.     if (pipe(pipefd) == -1 || pipe(pipefd2)) {
  34.         perror("Hiba a pipe nyitaskor!");
  35.         exit(EXIT_FAILURE);
  36.     }
  37.     pid = fork();    // creating parent-child processes
  38.     if (pid == -1) {
  39.         perror("Fork hiba");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.    
  43.     if (pid == 0){ //1 az iró, 0 az olvasó? pipe = olvasó és író vége van
  44.         // child process
  45.         //sleep(3);    // sleeping a few seconds, not necessary - nem kéne erőltetni
  46.         //close(pipefd[1]);  //Usually we close the unused write end
  47.         printf("Gyerek elkezdi olvasni a csobol az adatokat!\n");
  48.         int i = 0;
  49.         while(read(pipefd[0],sz,sizeof(sz))){
  50.             printf("Gyerek olvasta uzenet: %s\n",sz);        
  51.             //write(pipefd2[1],"Hajra vasas\n",12);
  52.             write(pipefd2[1],*tomb[i],sizeof(tomb[i]));
  53.             i++;
  54.         }; // reading max 100 chars - read addig vár, ameddig nem érkezik adat -> szinkronizálás
  55.  
  56.         close(pipefd[0]); // finally we close the used read end
  57.         close(pipefd2[1]);
  58.     }
  59.     else{    // szulo process
  60.         //sleep(3);
  61.         printf("Szulo indul!\n");
  62.        
  63.         //close(pipefd[0]); //Usually we close unused read end
  64.         int i = 0;
  65.         while(i < r){
  66.             //write(pipefd[1], "Hajra Fradi!",13); //13 = tobábbítandó karakterek száma
  67.             write(pipefd[1],*tomb[i],sizeof(tomb[i])); //13 = tobábbítandó karakterek száma
  68.             read(pipefd2[0],sz,sizeof(sz));
  69.             printf("Szülő olvasta uzenet: %s\n",sz);
  70.             i++;
  71.         }
  72.         close(pipefd[1]); // Closing write descriptor
  73.         close(pipefd2[0]);
  74.         printf("Szulo beirta az adatokat a csobe!\n");
  75.         fflush(NULL);     // flushes all write buffers (not necessary)
  76.         //wait();        // waiting for child process (not necessary)
  77.         // try it without wait()
  78.         printf("Szulo befejezte!\n");
  79.     }
  80.     exit(EXIT_SUCCESS);    // force exit, not necessary
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement