Advertisement
AnoTest

Exercice_2

Oct 15th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  Exercice2_Test
  4. //
  5. //  Created by Samir  Benjalloul on 14/10/2021.
  6. //
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. int main(int argc, const char * argv[]) {
  13.     int pid;
  14.     char buffer[10];
  15.     int pipe1[2];
  16.     int pipe2[2];
  17.     int s;
  18.    
  19.     if(pipe(pipe1)== -1){
  20.         perror("erreur de pipe 1");
  21.         exit(1);
  22.     }
  23.     if(pipe(pipe2) == -1){
  24.         perror("Erreur de pipe 2");
  25.         exit(1);
  26.     }
  27.        
  28.     pid =fork();
  29.     if(pid < 0){
  30.         perror("erreur de fork");
  31.         exit(1);
  32.     }
  33.     if(pid ==0){ //fils
  34.         close(pipe1[0]);
  35.         close(pipe2[1]);
  36.        
  37.         read(pipe2[0], buffer, 20);
  38.         write(pipe1[1], "hello pipe1\n",20);
  39. //        close(pipe2[1]);
  40.         close(pipe1[1]);
  41.         close(pipe2[0]);
  42.         printf("message reçus de pipe 2 %s \n", buffer);
  43.     }else {
  44.         close(pipe1[1]);
  45.         close(pipe2[0]);
  46.        
  47.         write(pipe2[1],"hello pipe2\n", 20);
  48.         read(pipe1[0], buffer, 20);
  49.        
  50.         close(pipe1[0]);
  51.         close(pipe2[1]);
  52.         printf("message reçu de pipe 1  %s \n",buffer);
  53.     }
  54.    
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement