Created
March 30, 2014 18:18
-
-
Save stevendanna/9877254 to your computer and use it in GitHub Desktop.
Basic pipe implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <strings.h> | |
int main(int argc, char *argv[]) { | |
int pipefd[2]; | |
pid_t cpid1; | |
pid_t cpid2; | |
if (argc != 3) { | |
fprintf(stderr, "Usage: %s CMD1 CMD2\n", argv[0]); | |
exit(1); | |
} | |
if (pipe(pipefd) == -1) { | |
perror("pipe"); | |
exit(EXIT_FAILURE); | |
} | |
cpid1 = fork(); | |
if (cpid1 == 0) { | |
dup2(pipefd[1], 1); | |
close(pipefd[0]); | |
close(pipefd[1]); | |
execlp(argv[1], argv[1], NULL); | |
} else { | |
cpid2 = fork(); | |
if (cpid2 == 0) { | |
dup2(pipefd[0], 0); | |
close(pipefd[0]); | |
close(pipefd[1]); | |
execlp(argv[2], argv[2], NULL); | |
} else { | |
exit(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment