-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_pool.c
More file actions
213 lines (168 loc) · 4.66 KB
/
Copy pathworker_pool.c
File metadata and controls
213 lines (168 loc) · 4.66 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define MAX_LINE 1024
#define MAX_WORKERS 10
typedef struct {
int lines;
int words;
int chars;
int vowels;
} result_t;
int count_words(char *line) {
int count = 0;
char *token = strtok(line, " \n");
while (token != NULL) {
count++;
token = strtok(NULL, " \n");
}
return count;
}
int count_vowels(char *line) {
int v = 0;
for (int i = 0; line[i]; i++) {
char c = line[i];
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U') {
v++;
}
}
return v;
}
int main(int argc, char *argv[]) {
int n = 2;
char *filename = NULL;
// parse args
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-n") == 0) {
n = atoi(argv[++i]);
} else if (strcmp(argv[i], "-f") == 0) {
filename = argv[++i];
}
}
if (n < 1 || n > MAX_WORKERS) {
fprintf(stderr, "Error: workers must be 1-%d\n", MAX_WORKERS);
return 1;
}
FILE *fp = stdin;
if (filename != NULL) {
fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return 1;
}
}
int task_pipe[MAX_WORKERS][2];
int result_pipe[MAX_WORKERS][2];
pid_t workers[MAX_WORKERS];
// create workers
for (int i = 0; i < n; i++) {
if (pipe(task_pipe[i]) < 0) {
perror("pipe");
exit(1);
}
if (pipe(result_pipe[i]) < 0) {
perror("pipe");
exit(1);
}
workers[i] = fork();
if (workers[i] < 0) {
perror("fork");
exit(1);
}
if (workers[i] == 0) {
// child
close(task_pipe[i][1]);
close(result_pipe[i][0]);
char line[MAX_LINE];
while (1) {
int nread = read(task_pipe[i][0], line, MAX_LINE);
if (nread < 0) {
perror("read");
exit(1);
}
if (nread == 0) break;
result_t r;
r.lines = 1;
char temp[MAX_LINE];
strcpy(temp, line);
r.words = count_words(temp);
r.chars = strlen(line);
r.vowels = count_vowels(line);
if (write(result_pipe[i][1], &r, sizeof(r)) < 0) {
perror("write");
exit(1);
}
}
close(task_pipe[i][0]);
close(result_pipe[i][1]);
exit(0);
} else {
// parent
close(task_pipe[i][0]);
close(result_pipe[i][1]);
}
}
// distribute tasks
char line[MAX_LINE];
int current = 0;
int tasks_sent = 0;
while (fgets(line, MAX_LINE, fp) != NULL) {
if (write(task_pipe[current][1], line, MAX_LINE) < 0) {
perror("write");
exit(1);
}
current = (current + 1) % n;
tasks_sent++;
}
// close task pipes
for (int i = 0; i < n; i++) {
close(task_pipe[i][1]);
}
// collect results
int total_lines = 0;
int total_words = 0;
int total_chars = 0;
int total_vowels = 0;
int results_received = 0;
int worker_usage[MAX_WORKERS] = {0};
result_t r;
for (int i = 0; i < n; i++) {
while (1) {
int nread = read(result_pipe[i][0], &r, sizeof(r));
if (nread < 0) {
perror("read");
exit(1);
}
if (nread == 0) break;
total_lines += r.lines;
total_words += r.words;
total_chars += r.chars;
total_vowels += r.vowels;
worker_usage[i]++;
results_received++;
}
close(result_pipe[i][0]);
}
// wait children
for (int i = 0; i < n; i++) {
wait(NULL);
}
// print summary
printf("\n========== FINAL SUMMARY ==========\n");
printf("Run status : completed normally\n");
printf("Tasks sent : %d\n", tasks_sent);
printf("Results received: %d\n", results_received);
printf("Total lines : %d\n", total_lines);
printf("Total words : %d\n", total_words);
printf("Total chars : %d\n", total_chars);
printf("Total vowels : %d\n", total_vowels);
printf("\nWorker usage:\n");
for (int i = 0; i < n; i++) {
printf(" Worker %d processed %d task(s)\n", i, worker_usage[i]);
}
printf("===================================\n");
return 0;
}