1 What the pipeline is:
Pipes everyone knows that there are two ends, at which end is used to read one end to write, the output of the end as the input of the other end.
2 function prototypes
int pipe (int pipefd[2]);//The two ends of the argument represented separately
3 Example: One end of the pipeline as the writing of the other end as a read-father process implementation
1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMain ()6 {7 pid_t pid;8 inttemp;9 intpipedes[2];Ten Chars[ -]="Letgo"; One Chard[ -]; A if(Pipe (pipedes)) ==-1) - { -Perror ("Pipe"); the exit (exit_failure); - } - - if((Pid=fork ()) ==-1) + { -Perror ("Fork Error"); + exit (exit_failure); A}Else if(pid==0) at { - //printf ("sub-process writes data to the pipe Reader"); -printf"Sub-process writes data to the pipe read \ n"); - if(Write (pipedes[1],s, -))==-1) - { -Perror ("Write"); in exit (exit_failure); -}Else to { +printf"the data written is%s\n.", s); - exit (exit_success); the } *}Else if(pid>0) $ {Panax NotoginsengSleep2);//wait for the child process to end -printf"the parent process reads data from the pipeline \ n"); the if(Read (pipedes[0],d, -))==-1) + { APerror ("Read"); the exit (exit_failure); + } -printf"The data read from the pipe read is%s\n.", d); $ } $ - return 1; -}
Operation Result:
Linux uses nameless pipes to read and write files