/*
* process 1 reads from stdin and writes to a pipe to process 2.
* process 2 reads from pipe from process 1, converts to caps and
* writes to pipe to process 3
* process 3 reads from pipe from process 2 and writes to stdout.
*/
#define STDIN 0
#define STDOUT 1
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
char *process="./cat"; /* process to exec */
char *caps="caps";
int fd[2]; /* pipe file desc. */
pipe(fd);
if( fork() != 0 ) {/* process 1, parent */
close(fd[0]); /* parent does not read from pipe */
close(STDOUT); /* prepare to redirect stdout */
dup(fd[1]); /* stdout now writes to the pipe */
close(fd[1]); /* extra file descriptor */
execl( process, process, 0 ); /* call cat */
} else { /* process 2, child */
/*
* First deal with pipe between proc. 1 & 2.
*/
close(fd[1]); /* child does not write to pipe */
close(STDIN); /* prepare to redirect stdin */
dup(fd[0]); /* stdin now reads from the pipe */
close(fd[0]); /* extra file descriptor */
/*
* now for process 3. and pipe between 2 & 3.
*/
pipe(fd);
if( fork() != 0 ) {/* process 2, parent of second fork */
close(fd[0]); /* parent does not read from pipe */
close(STDOUT); /* prepare to redirect stdout */
dup(fd[1]); /* stdout now writes to the pipe */
close(fd[1]); /* extra file descriptor */
execl( process, process, caps, 0 ); /* call cat */
} else { /* process 3, child of second fork */
close(fd[1]); /* child does not write to pipe */
close(STDIN); /* prepare to redirect stdin */
dup(fd[0]); /* stdin now reads from the pipe */
close(fd[0]); /* extra file descriptor */
execl( process, process, 0 ); /* call cat */
}
}
return 0;
}