This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathparallel_min_max.c
155 lines (125 loc) · 3.39 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
#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) {
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);
// your code here
// error handling
break;
case 1:
array_size = atoi(optarg);
// your code here
// error handling
break;
case 2:
pnum = atoi(optarg);
// your code here
// error handling
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 (optind < argc) {
printf("Has at least one no option argument\n");
return 1;
}
if (seed == -1 || array_size == -1 || pnum == -1) {
printf("Usage: %s --seed \"num\" --array_size \"num\" --pnum \"num\" \n",
argv[0]);
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);
for (int i = 0; i < pnum; i++) {
pid_t child_pid = fork();
if (child_pid >= 0) {
// successful fork
active_child_processes += 1;
if (child_pid == 0) {
// child process
// parallel somehow
if (with_files) {
// use files here
} else {
// use pipe here
}
return 0;
}
} else {
printf("Fork failed!\n");
return 1;
}
}
while (active_child_processes > 0) {
// your code here
active_child_processes -= 1;
}
struct MinMax min_max;
min_max.min = INT_MAX;
min_max.max = INT_MIN;
for (int i = 0; i < pnum; i++) {
int min = INT_MAX;
int max = INT_MIN;
if (with_files) {
// read from files
} else {
// read from pipes
}
if (min < min_max.min) min_max.min = min;
if (max > min_max.max) min_max.max = 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;
free(array);
printf("Min: %d\n", min_max.min);
printf("Max: %d\n", min_max.max);
printf("Elapsed time: %fms\n", elapsed_time);
fflush(NULL);
return 0;
}