-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelizer.cpp
159 lines (130 loc) · 3.98 KB
/
parallelizer.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
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
149
150
151
152
153
154
155
156
157
158
159
#include "parallelizer.h"
Parallelizer::Parallelizer()
{
//
}
static void Parallelizer::signals_handler(int signo)
{
if(signo == SIGUSR1){
printf("exit success!\n");
exit(EXIT_SUCCESS);
}
else if(signo == SIGPIPE){
perror("writing to the pipe in main process failed!\n");
}
}
int Parallelizer::start()
{
const int childrenCount = execs.size();
int fd1[childrenCount][2];
int fd2[childrenCount][2];
signal(SIGUSR1, signals_handler);
pid_t pid;
for(int i = 1; i <= childrenCount; i++){
pipe(fd1[i-1]);
pipe(fd2[i-1]);
if((pid = fork()) < 0){
perror("Error while calling fork()");
return ERRFORK;
}
else if(pid == 0){
//printf("Child %d PID %d\n", i, getpid());
if(i > 1){
// Here I close connection with previous processes
for(int j = 0; j <= i-2; j++){
close(fd1[j][0]);
close(fd1[j][1]);
}
}
dup2(fd2[i-1][0], 0); // Input fd
close(fd2[i-1][0]);
dup2(fd1[i-1][1], 1); // Output fd
close(fd1[i-1][1]);
// CHILD
struct pollfd fds[1];
fds[0].fd = 0;
fds[0].events = POLLIN;
int i = 0;
while(1){
int ret = poll(fds, 1, -1);
if(ret == -1){
perror("poll error");
return ERRPOLL;
}
if(fds[0].revents & POLLIN){
char c[BUFSIZ];
int len = 0;
int val = 0;
if ((len = read(fds[0].fd, c, BUFSIZ)) != 0){
execs[i-1]->setData(c);
execs[i-1]->exec();
char buf[BUFSIZ];
sprintf(buf, "%s %s", c, execs[i-1]->getResults());
write(1, buf, BUFSIZ);
continue;
}
}
}
break;
}
else{
close(fd1[i-1][1]);
close(fd2[i-1][0]);
fd1[i-1][1] = fd2[i-1][1];
// fd1[i-1][0] read fd
// fd1[i-1][1] write fd
//printf("Parent iteration %d write fd %d\n", i, fd1[i-1][1]);
}
}
signal(SIGPIPE, signals_handler);
// PARENT
// Initializing pipe watcher
int epfd = epoll_create(childrenCount);
if(epfd < 0){
perror("epoll_create!");
return ERRECRT;
}
for(size_t i = 0; i < childrenCount; i++){
struct epoll_event event;
int ret;
event.data.ptr = fd1[i];
event.events = EPOLLIN;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd1[i][0], &event);
if(ret){
perror("epoll_ctl");
return ERRECTL;
}
}
struct epoll_event *eBack;
eBack = malloc(sizeof(struct epoll_event));
if(!eBack){
perror("malloc");
return ERRMALL;
}
for(size_t i = 0; i < childrenCount; i++){
char buf[BUFSIZ];
sprintf(buf, "%s", ctl->getData()); // Send parent controller data to executor
write(fd1[i][1], buf, BUFSIZ);
}
int i = 0;
// There is waiter for 1 event because after getting results of processes this send new data for processing
while(epoll_wait(epfd, eBack, 1, -1) > 0){
if(eBack->events == EPOLLIN){
char c[BUFSIZ];
int len = 0;
int *fd = (int *)eBack->data.ptr;
if((len = read(fd[0], c, BUFSIZ)) != 0){
// it should be in controller
printf("%d: %s\n", i++, c);
ctl->setData(c);
}
char buf[BUFSIZ];
sprintf(buf, "%s", ctl->getData()); // Send parent controller data to executor
write(fd[1], buf, BUFSIZ);
}
}
free(eBack);
while(1)
pause();
return SUCCESS;
}