-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_sol.c
53 lines (46 loc) · 1.27 KB
/
wait_sol.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
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 <sys/wait.h>
#include <string.h>
int main(int argc, char **argv) {
// Declare any variables you need
int result;
int sum = 0;
int status;
// Write the code to loop over the command line arguments.
// (Remember to skip the executable name.)
for (int i = 1; i < argc; i++) {
// call fork
result = fork();
if (result == -1) { // case: a system call error
// handle the error
perror("fork");
exit(1);
} else if (result == 0) { // case: a child process
// child does their work here
int len = strlen(argv[i]);
exit(len);
}
}
// Finish the code to sum up the return values from the children
for (int i = 1; i < argc; i++) {
if (wait(&status) == -1) {
perror("wait");
exit(1);
}
if (WIFEXITED(status)) {
sum += WEXITSTATUS(status);
}
}
printf("The length of all the args is %d\n",sum);
return 0;
}