Last active
March 16, 2017 12:34
-
-
Save FrBrGeorge/b2d42956c9ec960de01946b065f945a3 to your computer and use it in GitHub Desktop.
pipe+fork+exec
This file contains 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 <sys/wait.h> | |
int main(void) | |
{ | |
int pipefd[2], res; | |
pid_t pout, pin, pwait; | |
pipe(pipefd); | |
if (!(pout=fork())) { | |
close(1); | |
sleep(1); | |
dup(pipefd[1]); | |
close(pipefd[0]); | |
execl("/usr/bin/cal", "cal", NULL); | |
} | |
close(pipefd[1]); | |
printf("Writer: %d\n", pout); | |
if(!(pin=fork())) { | |
close(0); | |
sleep(1); | |
dup(pipefd[0]); | |
close(pipefd[1]); | |
execl("/usr/bin/rev", "rev", NULL); | |
} | |
close(pipefd[0]); | |
printf("Reader: %d\n", pin); | |
printf("Done %d\n", wait(&res)); | |
printf("Done %d\n", wait(&res)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment