-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork2.c
74 lines (61 loc) · 2.11 KB
/
fork2.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
71
72
73
74
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>
/*
Write a program that takes at least one command-line argument, where
each argument is the name of an executable to run.
The program should fork a new child for each command-line argument, and the
child should call exec to run the executable named by the argument. (The
executable itself takes no arguments.) The child should exit with a non-zero
value if the exec fails.
The parent process should wait for all of its child processes to complete.
If all of its child processes exit successfully with an exit status of 0,
the parent prints "Success\n". Otherwise, the parent prints "Failure\n".
Here is an example of how the program could be called:
./run_all ls date ps
*/
/* What if we wanted to run each one in sequence? */
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (fork() == 0) {
// Child process: run an executable.
// First argument is the name of the file to be executed
// Remaining arguments are what gets set into argv for main
execl(argv[i], argv[i], NULL);
/*
// Another way to call exec, using an array (vector) of arguments
char *args[2];
args[0] = argv[i]; // 0th element is the name of the program
args[1] = NULL;
// First argument is the name of the file to be executed
execvp(argv[i], args);
*/
perror("exec");
exit(1);
}
}
// Parent here.
int status;
int all_success = 1;
for (int i = 1; i < argc; i++) {
// This is not a good way to call wait, unless all
// we care about is making sure the parent doesn't
// exit before its children
wait(&status);
}
if (all_success) {
printf("Success\n");
} else {
printf("Failure\n");
}
return 0;
}