-
Notifications
You must be signed in to change notification settings - Fork 0
/
ece650-a3.cpp
123 lines (111 loc) · 2.64 KB
/
ece650-a3.cpp
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
#include<unistd.h>
#include <iostream>
#include<sys/types.h>
#include<sys/wait.h>
#include <signal.h>
#include<vector>
int procB(void) {
// Process B writing to C
while (!std::cin.eof()) {
// read a line of input until EOL and store in a string
std::string line;
std::getline(std::cin, line);
if (line.size () > 0)
std::cout << line << std::endl;
}
// std::cout << "[B] saw EOF" << std::endl;
return 0;
}
#define MAXLINE 512
int main(int argc, char **argv) {
std::vector<pid_t> kids;
int AtoB[2];
pipe(AtoB);
int AtoC[2];
pipe(AtoC);
FILE *fpin;
FILE *write_a2;
char line[MAXLINE];
pid_t kid;
kid = fork ();
if (kid == 0) {
dup2(AtoC[1],STDOUT_FILENO);
close(AtoC[0]);
close(AtoC[1]);
//sleep (4);
execvp ("./rgen",argv);
// execl("/bin/ls", "ls", "-l", nullptr);
perror ("Error from arie");
return 1;
}
else if (kid < 0) {
std::cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(kid);
kid = fork ();
if (kid == 0) {
dup2(AtoC[0],STDIN_FILENO);
close(AtoC[1]);
close(AtoC[0]);
dup2(AtoB[1],STDOUT_FILENO);
close(AtoB[0]);
close(AtoB[1]);
//sleep (4);
execlp ("python", "python", "a1ece650.py", "test", (char*) NULL);
// execl("/bin/ls", "ls", "-l", nullptr);
perror ("Error from arie");
return 1;
}else if (kid < 0) {
std::cerr << "Error: could not fork\n";
return 1;
}
kids.push_back(kid);
//std::cout <<"B start:"<< std::endl;
write_a2=popen("./ece650-a2","w");
kid=fork();
if(kid==0){
fpin=fdopen(AtoB[0],"r");
while(fgets(line, MAXLINE, fpin) != NULL){
//if(line[0]=='V'||line[0]=='E')
//std::cout<<line;
fputs(line,write_a2);
fflush(write_a2);
}
}
/*
kid = fork ();
if (kid == 0) {
dup2(AtoB[0],STDIN_FILENO);
close(AtoB[1]);
close(AtoB[0]);
//sleep (4);
execlp("./ece650-a2", "a2", nullptr);
perror ("Error from arie");
return 1;
}
else if (kid < 0) {
std::cerr << "Error: could not fork\n";
return 1;
}
*/
kids.push_back(kid);
kid = 0;
dup2(AtoB[1],STDOUT_FILENO);
close(AtoB[0]);
close(AtoB[1]);
//std::cout<<"kids size:"<<kids.size();
// start process B
int res = procB();
// std::cout <<"B end:"<< std::endl;
for (pid_t k : kids) {
int status;
kill (k, SIGTERM);
waitpid(k, &status, 0);
}
//dup2(AtoB[0],STDIN_FILENO);
// close(AtoB[1]);
//close(AtoB[0]);
std::cout << "ls returned status: " << res << std::endl;
return 0;
}