Created
November 2, 2017 16:09
-
-
Save killme2008/1148a532ee3a2731a1c57daa01b45f53 to your computer and use it in GitHub Desktop.
Simple pipe example
This file contains hidden or 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 <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