Advertisement
piffy

pipe

Aug 31st, 2014
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void  leggi_dalla_pipe (int file) {
  6.   FILE *f;
  7.   int c;
  8.   f = fdopen (file, "r");
  9.   while ((c = fgetc (f)) != EOF)
  10.     putchar (c);
  11.   fclose (f);
  12. }
  13.  
  14. void scrivi_nella_pipe (int file) {
  15.   FILE *f;
  16.   f = fdopen (file, "w");
  17.   fprintf (f, "Messaggio!\n");
  18.   fclose (f);
  19. }
  20.  
  21. int  main (void) {
  22.   pid_t pid;
  23.   int mypipe[2];
  24.  
  25.   /* Crea la pipe. */
  26.  if (pipe (mypipe)) {
  27.    fprintf (stderr, "Pipe non creata.\n");
  28.    return EXIT_FAILURE; }
  29.   /* Fork!*/  
  30.   pid = fork ();
  31.  if (pid < 0) {
  32.    fprintf (stderr, "Fork fallita.\n");
  33.    return EXIT_FAILURE;  }
  34.  
  35.   if (pid == 0)  {
  36.    /* Processo figlio: chiudere la parte di scrittura */
  37.    close (mypipe[1]);
  38.    leggi_dalla_pipe (mypipe[0]);
  39.    return EXIT_SUCCESS;   }
  40.   else {
  41.    /* Processo padre: chiudere la parte di lettura */
  42.    close (mypipe[0]);
  43.    scrivi_nella_pipe (mypipe[1]);
  44.    return EXIT_SUCCESS;    }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement