-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
71 lines (41 loc) · 1.32 KB
/
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
63
64
65
66
67
68
69
70
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv) {
// Declare any variables you need
// Write the code to loop over the command line arguments
for (int i = 1; i < argc; i++) {
// Call pipe before we fork
// Call fork
int result = fork();
if (result < 0) {
perror("fork");
exit(1);
} else if (result == 0) {
// Child only writes to the pipe, so close reading end
// Before we forked, parent had open the reading ends to
// all previously forked children; so close those.
// Now do the work - write the value in binary to the pipe
int length = strlen(argv[i]);
// Close the pipe since we are done with it.
exit(0);
} else {
// In the parent, but before doing the next loop iteration,
// close the end of the pipe that we don't want open
}
}
// Only the parent gets here
int sum = 0;
// Read one integer from each child
printf("Sum is %d\n", sum);
return 0;
}