Advertisement
Guest User

Untitled

a guest
May 26th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4.  
  5. void main(){
  6.     int fd[2];
  7.     pid_t childpid;
  8.     char    string[] = "Hello, world!\n";
  9.         char    readbuffer[80];
  10.  
  11.     pipe(fd);
  12.     if((childpid = fork()) == -1)
  13.         {
  14.                 perror("fork");
  15.                 exit(1);
  16.         }
  17.         if(childpid == 0)
  18.         {
  19.                 /* Child process closes up input side of pipe *
  20.                 close(fd[0]);
  21.  
  22.                 /* Send "string" through the output side of pipe */
  23.                 write(fd[1], string, (strlen(string)+1));
  24.                 exit(0);
  25.         }
  26.         else
  27.         {
  28.                 /* Parent process closes up output side of pipe */
  29.                 close(fd[1]);
  30.  
  31.                 /* Read in a string from the pipe */
  32.                 nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
  33.                 printf("Received string: %s", readbuffer);
  34.         }
  35.         return(0);
  36.    
  37. }
  38.  
  39. fd[0] = Input Seite der Pipe
  40. fd[1] = Output Seite der Pipe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement