Skip to content

Instantly share code, notes, and snippets.

@udaya1223
Created May 2, 2014 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udaya1223/6ef089077f4c0403af82 to your computer and use it in GitHub Desktop.
Save udaya1223/6ef089077f4c0403af82 to your computer and use it in GitHub Desktop.
How to use PIPE & FORK system calls to run three commands together.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define oops(m,x) { perror(m); exit(x); }
main(int ac, char **av)
{
int thepipe[2], /* first pipe for the first and second command */
thesecondpipe[2], /* second pipe for the second and third commands*/
pid; /* and the pid */
if ( ac != 4 ){
fprintf(stderr, "usage: pipe cmd1 cmd2 cmd3\n");
exit(1);
}
if ( pipe( thepipe ) == -1 ) /* get the first pipe*/
oops("Cannot get a pipe", 1);
if ( pipe(thesecondpipe) == -1 ) /* get the second pipe*/
oops("Cannot get a pipe", 1);
if ( (pid = fork()) == -1 ) /* get the first child for executing first command*/
oops("Cannot fork", 2);
if(pid == 0){ /*First child reads from the stdin, executes the first command and writes the output to the first pipe*/
close(thesecondpipe[0]); /*First child doesn't read or write the second pipe*/
close(thesecondpipe[1]);
close(thepipe[0]); /* First child doesn't read from the first pipe*/
if ( dup2(thepipe[1], 1) == -1 )
oops("could not redirect stdout", 4);
close(thepipe[1]); /* stdout is duped, close the first pipe write end*/
execlp( av[1], av[1], NULL);
oops(av[1], 5);
}
if ( pid > 0 ){
if((pid = fork()) == -1 ) /*create the second child to execute the second command*/
oops("Cannot fork", 2);
if(pid == 0){ /*second child reads from the first pipe, executes the second command and writes the output to the second pipe*/
close(thepipe[1]); /*Second child doesn't write to the first pipe*/
if( dup2(thepipe[0], 0) == -1)
oops("Could not redirect stdin", 3);
close(thepipe[0]); /*stdin is duped, close the first pipe read end*/
close(thesecondpipe[0]);/*Second child doesn't read from the second pipe*/
if( dup2(thesecondpipe[1], 1) == -1)
oops("Could not redirect stdout", 3);
close(thesecondpipe[1]);/*stdout is duped, close the second pipe write end*/
execlp( av[2], av[2], NULL);
oops(av[2], 5);
}
if(pid > 0){ /*parent process reads from the second pipe, executes the third command and writes the output to the stdout*/
close(thepipe[0]); /*Parent doesn't read or write the first pipe*/
close(thepipe[1]);
close(thesecondpipe[1]); /* parent doesn't write to the second pipe*/
if ( dup2(thesecondpipe[0], 0) == -1 )
oops("could not redirect stdin",3);
close(thesecondpipe[0]); /* stdin is duped, close the second pipe read end*/
execlp( av[3], av[3], NULL);
oops(av[2], 4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment