-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild.c
64 lines (52 loc) · 1.14 KB
/
child.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
63
64
/*
* Please see README for copying information.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int child(char *cmd, FILE **input, FILE **output) {
int childpid, pipe1[2], pipe2[2];
/*
* Create two pipes.
*/
if ((pipe(pipe1) < 0) || (pipe(pipe2) < 0)) {
perror("pipe");
exit(-1);
}
/*
* Fork into two processes
*/
if ((childpid = fork()) <0) {
perror("fork");
exit(-1);
} else if (childpid > 0) {
/*
* This is the parent. It will only be able
* to write to pipe1 and read from pipe2.
*/
close(pipe1[0]);
close(pipe2[1]);
*input = fdopen(pipe2[0], "r");
*output = fdopen(pipe1[1], "w");
return childpid;
} else {
/*
* This is the child. It will only be able
* to read from pipe1 and write to pipe2.
*/
close(pipe1[1]);
close(pipe2[0]);
/*
* The commands "standard in" while now
* come from pipe1[0], and the "standard
* out" will go to pipe2[1].
*/
dup2(pipe1[0], 0);
dup2(pipe2[1], 1);
if (execlp(cmd, cmd, NULL) <0) {
perror(cmd);
exit(-1);
}
}
return 0;
}