-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_pipe.c
62 lines (52 loc) · 1.11 KB
/
w_pipe.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
55
56
57
58
59
60
61
62
#include "w_pipe.h"
#include "w_run_command.h"
/**
* Simple diagram of the process tree for squish calling pipes.
*
* squish
* /\
* / \
* /\ p3
* / \
* p1 p2
*/
int
ipc(FILE* ofp, char*** listTokens, int nListTokens) {
int fd[2];
int status = 0;
pid_t cpid;
int exitStatus = 0;
int statLoc = 0;
// if there is only one process, just run the command
if (nListTokens == 1) {
return runCmd(ofp, listTokens[0], &statLoc);
}
// create the pipe
pipe(fd);
// fork first child
cpid = fork();
if (cpid == 0 ) {
// first child
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
exitStatus = ipc(ofp, listTokens, nListTokens - 1);
free(listTokens);
_exit(exitStatus);
}
// fork second child
cpid = fork();
if (cpid == 0) {
// second child
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
exitStatus = runCmd(ofp, listTokens[nListTokens - 1], &statLoc);
free(listTokens);
_exit(exitStatus);
}
close(fd[0]);
close(fd[1]);
// reap children processes on children exit
waitpid(-1, &status, 0);
waitpid(-1, &status, 0);
return 0;
}