-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj1main.cpp
More file actions
72 lines (61 loc) · 2.63 KB
/
proj1main.cpp
File metadata and controls
72 lines (61 loc) · 2.63 KB
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
#include "prog1_functions.hpp"
int main(int argc, char** argv) {
//get number of children from command line
int user_proc = atoi(argv[1]);
pid_t pid = getpid();
int child_num[25]; //array to hold the child number
//can expect max. 25 children per the spec (section 3c.)
int n_processes = 0; //counts number of finished child processes
cout << "Parent pid is " << pid << endl;
//used to track which proccess is started
int tracker = 0;
for(int i = 0; i < user_proc; i++) { //for loop to create child processes
if (pid != 0) {
pid = fork();
child_num[i] = pid;
}
tracker++;
if (pid == 0) { //pid == 0 within child process, child prints its own pid
cout << "Started child with pid " << getpid() << endl;
//switch statement with tracker to determine which test should be started on child
//TODO replace each cout below with execlp call to start each test
switch(tracker) {
case 1:
execl("./test1", "test1", nullptr);
cout << "Error starting child " << getpid() << endl;
break;
case 2:
execl("./test2", "test2", nullptr);
cout << "Error starting child " << getpid() << endl;
break;
case 3:
execl("./test3", "test3", nullptr);
cout << "Error starting child " << getpid() << endl;
break;
case 4:
execl("./test4", "test4", nullptr);
cout << "Error starting child " << getpid() << endl;
break;
case 5:
execl("./test5", "test5", nullptr);
cout << "Error starting child " << getpid() << endl;
break;
}
break;
}
//once all 5 programs have been started, reset to beginning
if(tracker == 5){
tracker = 0;
}
}
if (pid > 0) { //pid > 0 within parent process, waits for child to finish
while(n_processes < user_proc) { //while the number of running processes is less than the amount the user requested
pid_t child_pid = wait(NULL);
if(child_pid > 0) { //needed to include this to prevent invalid PIDs of -1? not sure why
cout << "Child " << findIndex(child_pid, child_num)+1 << " (PID " << child_pid << ") finished" << endl;
n_processes++; //iterate the number of finished child processes
}
}
}
return 0;
}