Skip to content

Instantly share code, notes, and snippets.

@theasder
Last active December 30, 2015 22:39
Show Gist options
  • Save theasder/7895405 to your computer and use it in GitHub Desktop.
Save theasder/7895405 to your computer and use it in GitHub Desktop.
12-3
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
int main(int argc, char** argv)
{
close(2);
int fd[2], fd2[2];
pipe(fd);
pipe(fd2);
pid_t pr2 = 0;
//first process
if(!(fork()))
{
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
close(fd2[1]);
close(fd2[0]);
execlp(argv[1], argv[1], NULL);
return -1;
}
close(fd[1]);
//second process started
if(!(pr2 = fork()))
{
dup2(fd2[1], 1);
dup2(fd[0], 0);
close(fd[0]);
close(fd2[1]);
close(fd2[0]);
execlp(argv[2], argv[2], NULL);
return -1;
}
close(fd[0]);
//forth process started
if(!(fork()))
{
dup2(fd2[0], 0);
close(fd2[1]);
close(fd2[0]);
execlp(argv[4], argv[4], NULL);
return -1;
}
close(fd2[0]);
if(wait(NULL) == pr2)
wait(NULL);
if(!(fork()))
{
dup2(fd[1], 1);
close(fd2[1]);
execlp(argv[3], argv[3], NULL);
return -1;
}
close(fd2[1]);
wait(NULL);
wait(NULL);
wait(NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment