forked from IpovsOperatingSystems/Lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_min_max.c
192 lines (159 loc) · 4.76 KB
/
parallel_min_max.c
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
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <getopt.h>
#include "find_min_max.h"
#include "utils.h"
int main(int argc, char **argv) {
//cloud9
int seed = -1;
int array_size = -1;
int pnum = -1;
bool with_files = false;
while (true) {
int current_optind = optind ? optind : 1;
static struct option options[] = {{"seed", required_argument, 0, 0},
{"array_size", required_argument, 0, 0},
{"pnum", required_argument, 0, 0},
{"by_files", no_argument, 0, 'f'},
{0, 0, 0, 0}};
int option_index = 0;
int c = getopt_long(argc, argv, "f", options, &option_index);
if (c == -1) break;
switch (c) {
case 0:
switch (option_index) {
case 0:
seed = atoi(optarg);
if(seed < 0){
printf("seed < 0");
exit(1);
}
break;
case 1:
array_size = atoi(optarg);
if(array_size < 0){
printf("array_size < 0");
exit(1);
}
break;
case 2:
pnum = atoi(optarg);
if(pnum < 0){
printf("pnum < 0");
exit(1);
}
break;
case 3:
with_files = true;
break;
defalut:
printf("Index %d is out of options\n", option_index);
}
break;
case 'f':
with_files = true;
break;
case '?':
break;
default:
printf("getopt returned character code 0%o?\n", c);
}
}
if(seed == -1 || array_size == -1 || pnum == -1){
printf("usage isn't valid!");
return 1;
}
int *array = malloc(sizeof(int) * array_size);
GenerateArray(array, array_size, seed);
int active_child_processes = 0;
struct timeval start_time;
gettimeofday(&start_time, NULL);
struct MinMax min_max;
min_max.min = INT_MAX;
min_max.max = INT_MIN;
//min
int fd1[2];
if (pipe(fd1) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// max
int fd2[2];
if (pipe(fd2) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
int * els = malloc(pnum * sizeof(int) * 2);
for (int i = 0; i < pnum; i++) {
pid_t child_pid = fork();
// if child
if (child_pid == 0)
{
printf("\nChild process: %i\n", i);
min_max = GetMinMax(array, (int)(array_size * i / pnum), (int)(array_size * (i+1) / pnum));
char message[20];
int max = min_max.max;
sprintf(message,"%i", max);
int min = min_max.min;
sprintf(message,"%i", min);
if(with_files){
printf("with_files");
FILE *fp;
fp=fopen("test.txt", "w");
fprintf(fp, "%d %d", min, max);
fclose(fp);
} else {
write(fd1[1], message, strlen(message));
write(fd2[1], message, strlen(message));
}
exit(EXIT_SUCCESS);
}
// if parent
else if (child_pid > 0)
{
wait(NULL);
printf("\nParent process: %i\n", i);
char message[20];
if(with_files){
FILE *fp;
fp=fopen("test.txt", "r");
fscanf(fp, "%d %d", &els[2*i], &els[2*i+1]);
fclose(fp);
} else {
read(fd1[0], message, sizeof(message));
//max
sscanf(message, "%d", &els[2*i]);
read(fd2[0], message, sizeof(message));
//min
sscanf(message, "%d", &els[2*i+1]);
}
continue;
}
}
struct MinMax min_max_res = min_max_res = GetMinMax(els, 0, pnum * 2);
printf("Result min: %d\n", min_max_res.min);
printf("Result max: %d\n", min_max_res.max);
struct timeval finish_time;
gettimeofday(&finish_time, NULL);
double elapsed_time = (finish_time.tv_sec - start_time.tv_sec) * 1000.0;
elapsed_time += (finish_time.tv_usec - start_time.tv_usec) / 1000.0;
//printf("\nelapsed_time = %l\n",elapsed_time);
for (int i = 0; i < array_size; i++)
printf("%i, ", array[i]);
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
free(array);
fflush(NULL);
exit(0);
return 0;
}