Skip to content

Instantly share code, notes, and snippets.

@blt
Created April 25, 2010 23:37
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 blt/378832 to your computer and use it in GitHub Desktop.
Save blt/378832 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) {
int pfd0[2], pfd1[2];
pid_t ls_pid, wc_pid, wc1_pid;
pipe(pfd0);
pipe(pfd1);
if ((ls_pid = fork()) == 0) {
dup2(pfd0[1],STDOUT_FILENO);
close(pfd0[0]);
close(pfd1[0]);
close(pfd1[1]);
execl("/bin/ls", "ls","-al", NULL);
perror("exec ls failed");
exit(EXIT_FAILURE);
}
if ((wc_pid = fork()) == 0) {
dup2(pfd0[0], STDIN_FILENO);
dup2(pfd1[1], STDOUT_FILENO);
close(pfd0[1]);
close(pfd1[0]);
execl("/usr/bin/wc", "wc", NULL);
perror("exec wc failed");
exit(EXIT_FAILURE);
}
if ((wc1_pid = fork()) == 0) {
dup2(pfd1[0], STDIN_FILENO);
close(pfd0[0]);
close(pfd0[1]);
close(pfd1[1]);
execl("/usr/bin/wc", "wc", NULL);
perror("exec wc failed");
exit(EXIT_FAILURE);
}
close(pfd0[0]);
close(pfd0[1]);
close(pfd1[0]);
close(pfd1[1]);
waitpid(ls_pid, NULL, 0);
waitpid(wc_pid, NULL, 0);
waitpid(wc1_pid, NULL, 0);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment