-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipetest.c
54 lines (41 loc) · 873 Bytes
/
pipetest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <fcntl.h>
int main(void){
pid_t pid;
pid_t pid2;
int fd[2];
char *argv[] = {"ls",0};
char *argv2[] = {"wc","-w",0};
pipe(fd);
pid2 = fork();
switch(pid2){
case -1 : printf("Error in fork!\n"); return EXIT_FAILURE;
case 0 :
dup2(fd[0],0);
close(fd[1]);
execvp(argv2[0],argv2);
default :
break;
}
pid = fork();
switch(pid){
case -1 : printf("Error in fork!\n"); return EXIT_FAILURE;
case 0 :
dup2(fd[1],1);
close(fd[0]);
execvp(argv[0],argv);
default :
break;
}
close(fd[0]); close(fd[1]);
waitpid(pid, NULL, 0);
waitpid(pid2, NULL, 0);
char *argv3[] = {"ps",0};
execvp(argv3[0],argv3);
exit(0);
}