Skip to content

Instantly share code, notes, and snippets.

@killme2008
Created November 2, 2017 16:09
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 killme2008/1148a532ee3a2731a1c57daa01b45f53 to your computer and use it in GitHub Desktop.
Save killme2008/1148a532ee3a2731a1c57daa01b45f53 to your computer and use it in GitHub Desktop.
Simple pipe example
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int pp[2];
int pid;
char* word;
if(pipe(pp) == -1){
perror("bad pipe");
exit(1);
}
if((pid=fork()) == -1){
perror("bad fork");
exit(1);
}
if(pid == 0) {
close(0);
if(dup(pp[0])!=0){
perror("bad dup for child");
exit(1);
}
close(pp[0]);
close(pp[1]);
word = "java";
if(argc > 1){
word = argv[1];
}
execlp("grep", "grep", word, NULL);
perror("bad exec");
exit(1);
}
close(1);
if(dup(pp[1])!=1) {
perror("bad dup for parent");
exit(1);
}
close(pp[0]);
close(pp[1]);
execlp("ps", "ps", "aux", NULL);
perror("bad exec");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment