Advertisement
HAR1F

Untitled

May 24th, 2020
1,375
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.  
  6. int main()
  7. {
  8.    int fd[2], result;
  9.  
  10.    size_t size;
  11.    char resstring[14];
  12.  
  13.    if (pipe(fd) < 0)
  14.    {
  15.       printf("Can\'t open pipe\n");
  16.       exit(-1);
  17.    }
  18.  
  19.    result = fork();
  20.  
  21.    if (result < 0)
  22.    {
  23.       printf("Can\'t fork child\n");
  24.       exit(-1);
  25.    }
  26.    else if (result > 0)
  27.    {
  28.  
  29.       /* Parent process */
  30.  
  31.       close(fd[0]);
  32.      
  33.       size = write(fd[1], "Hello, world!", 14);
  34.  
  35.       if (size != 14)
  36.       {
  37.          printf("Can\'t write all string to pipe\n");
  38.          exit(-1);
  39.       }
  40.  
  41.       close(fd[1]);
  42.       printf("Parent exit\n");
  43.    }
  44.    else
  45.    {
  46.  
  47.       /* Child process */
  48.  
  49.       close(fd[1]);
  50.       size = read(fd[0], resstring, 14);
  51.  
  52.       if (size < 0)
  53.       {
  54.          printf("Can\'t read string from pipe\n");
  55.          exit(-1);
  56.       }
  57.  
  58.       printf("Child exit, resstring:%s\n", resstring);
  59.  
  60.       close(fd[0]);
  61.    }
  62.  
  63.    return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement