Skip to content

Instantly share code, notes, and snippets.

@FrBrGeorge
Last active March 16, 2017 12:34
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 FrBrGeorge/b2d42956c9ec960de01946b065f945a3 to your computer and use it in GitHub Desktop.
Save FrBrGeorge/b2d42956c9ec960de01946b065f945a3 to your computer and use it in GitHub Desktop.
pipe+fork+exec
#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