Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Last active December 19, 2015 10:29
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 dionyziz/5940631 to your computer and use it in GitHub Desktop.
Save dionyziz/5940631 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
int main() {
int fd[2];
int a = 0;
pipe(fd);
if (vfork()) {
sleep(1);
close(fd[0]);
printf("[parent] Give me an a: ");
// this should fail, as stdin has been modified by dup2
scanf("%i", &a);
printf("[parent] The a you gave was: %i\n", a);
}
else {
close(fd[1]);
dup2(fd[0], 0);
execlp("echo", "echo", "[child]", NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment