Pipes provide a convenient communication between a parent process and a child process.
The file system provides an implementation of pipes/
Redirection ccould be combined with pipes so that ordinary input/output uses the pipes.
A simple pipe example (no redirection):
1
2 /**
3 /* p[0] and p[1] will hold file descriptors for the pipe
4 * p[0] descriptor is for reading
5 * p[1] descriptor is for writing
6 */
7 int p[2];
8 pipe(p); /* create a pipe (assume no errors) */
9
10 if ( fork() == 0 ) {
11 char buf[80];
12 close(p[1]); /* Child will only read from, not write to the pipe */
13 while( read(p[0], buf, 80) > 0 ) {
14 /* use data from the parent */
15 }
16 exit(0);
17 } else {
18 char buf[80];
19 close(p[0]); /* Parent will only write to, not read from the pipe */
20 int more_data_to_write = 1;
21 while(more_data_to_write) {
22 /* generate data in buf */
23 ...
24 write(p[1], buf, 80);
25 more_data_to_write = moreData();
26 }
27 }