-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.cpp
More file actions
148 lines (104 loc) · 3.55 KB
/
driver.cpp
File metadata and controls
148 lines (104 loc) · 3.55 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<vector>
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
using namespace std;
int main (int argc, char **argv) {
vector<pid_t> kids; // create a vector to store process ID's for all children
int Rgen_AOne[2];
int AOne_ATwo[2];
pipe(Rgen_AOne); // create pipe to link Rgen to A1 (pipe 1)
pipe(AOne_ATwo); // create pipe to link A1 to Rgen (pipe 2)
pid_t child_pid; // create a process id variable
// concurrently run Python script
child_pid = fork();
// fork successfully produces a child
if (child_pid == 0) {
// troubleshooting: cerr << "Within Python child\n";
dup2(Rgen_AOne[0], STDIN_FILENO); // map input to "read" end of pipe 1
dup2(AOne_ATwo[1], STDOUT_FILENO); // map output to "write" end of pipe 2
// close all file descriptors to prevent child from sharing parent's resources
close(Rgen_AOne[0]);
close(Rgen_AOne[1]);
close(AOne_ATwo[0]);
close(AOne_ATwo[1]);
// arguments needed to run the Python script
char *args[]={(char*)"python3",(char*)"ece650-a1.py",(char*)NULL};
// replaces child process with image of specified program
execvp("python3",args);
return 1;
}
// fork fails to produce a child - still within parent process
else if (child_pid < 0) {
cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(child_pid);
// concurrently run shortest path interface program
child_pid = fork();
// fork successfully produces a child
if (child_pid == 0){
// troubleshooting: cerr << "Within Shortest Path C++ child\n";
dup2(AOne_ATwo[0], STDIN_FILENO); // map input to "read" end of pipe 2
// close all file descriptors to prevent child from sharing parent's resources
close(AOne_ATwo[0]);
close(AOne_ATwo[1]);
// arguments needed to run the C++ script
char *args[]={(char*)"a2ece650",(char*)NULL};
// replaces child process with image of specified program
execvp("./a2ece650",args);
return 1;
}
// fork fails to produce a child - still within parent process
else if (child_pid < 0) {
cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(child_pid);
// concurrently run randomized generator program
child_pid = fork();
// fork successfully produces a child
if (child_pid == 0){
// troubleshooting: cerr << "Within Random Generator C++ child\n";
dup2(Rgen_AOne[1], STDOUT_FILENO); // map output to "write" end of pipe 1
// close all file descriptors to prevent child from sharing parent's resources
close(Rgen_AOne[0]);
close(Rgen_AOne[1]);
// store arguments specified to driver into array to use for rgen
argv[0] = (char*)"rgen";
// replaces child process with image of specified program
execvp("./rgen",argv);
return 1;
}
else if (child_pid < 0) {
cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(child_pid);
// concurrently accept user input to use shortest path interface
child_pid = fork();
// fork successfully produces a child
if (child_pid == 0) {
dup2(AOne_ATwo[1], STDOUT_FILENO); // map output of keyboard entry to "write" end of pipe 2
// parses user input
while (!cin.eof()){
string input;
getline(cin,input); // parses user input and stores in variable input
cout << input << endl;
}
return 1;
}
// fork fails to produce a child - still within parent process
else if (child_pid < 0) {
cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(child_pid);
wait(NULL); // wait for all processes to complete output
// terminate all child processes
for (pid_t k : kids){
kill (k, SIGTERM);
}
return 0;
}