-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppmp_consumer.c
342 lines (276 loc) · 8.95 KB
/
ppmp_consumer.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "shm_com.h"
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
#define NUMBER_OF_PROCESSORS sysconf(_SC_NPROCESSORS_ONLN)
sem_t mutex;
void *thread_start_quick(void *arg);
void *thread_start_bubble(void *arg);
int cmpfunc(const void *a, const void *b);
void print_matrix(int **mat, int m, int n);
void bubblesort(int *mat, int n);
void quicksort(int *mat, int n);
void swap(int *x, int *y);
#define handle_error_en(en, msg) \
do \
{ \
errno = en; \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
#define handle_error(msg) \
do \
{ \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
int cmpfunc(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}
struct process_info
{ /* Used as argument to process_start_xyz() */
int m;
int n;
int process_num; /* Application-defined process # */
int processingPackSize; /* Which position this process will work on sorting */
int number_of_elements; /* Number of elements */
double processing_time; /* How many seconds it took for calculations */
int startIndex; /* Starting index where process will get tasks */
int endIndex; /* Ending index where process will stop getting tasks */
};
void start_quick(void *arg, void *arg1)
{
clock_t t;
struct process_info *tinfo = arg;
int *arr = arg1;
t = clock();
int j = 0;
// printf("\n[son] pid %d from [parent] pid %d and value ou i=%d, startIndex %d endIndex %d.\n",
// getpid(), getppid(), tinfo->process_num, tinfo->startIndex, tinfo->endIndex);
for (int i = tinfo->startIndex; i < tinfo->endIndex; i++)
{
// 0 * 5 = 0 1 2 3 4
// 1 * 5 = 5 6 7 8 9
// 2 * 5 = 10 11 12 13 14
// 3 * 5 = 15 16 17 18 19
j = i * tinfo->number_of_elements;
quicksort(&arr[j], tinfo->number_of_elements);
// qsort(&arr[j], tinfo->number_of_elements, sizeof(int), cmpfunc);
}
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC;
tinfo->processing_time += time_taken;
printf("time i took %f sorting %d elements (quick sort)\n", tinfo->processing_time, (tinfo->endIndex - tinfo->startIndex) * tinfo->number_of_elements);
}
void start_bubble(void *arg, void *arg1)
{
clock_t t;
struct process_info *tinfo = arg;
int *arr = arg1;
t = clock();
int j = 0;
// printf("\n[son] pid %d from [parent] pid %d and value ou i=%d, startIndex %d endIndex %d.\n",
// getpid(), getppid(), tinfo->process_num, tinfo->startIndex, tinfo->endIndex);
for (int i = tinfo->startIndex; i < tinfo->endIndex; i++)
{
// 0 * 5 = 0 1 2 3 4
// 1 * 5 = 5 6 7 8 9
// 2 * 5 = 10 11 12 13 14
// 3 * 5 = 15 16 17 18 19
j = i * tinfo->number_of_elements;
bubblesort(&arr[j], tinfo->number_of_elements);
}
t = clock() - t;
double time_taken = ((double)t) / CLOCKS_PER_SEC;
tinfo->processing_time += time_taken;
printf("time i took %f sorting %d elements (bubble sort)\n", tinfo->processing_time, (tinfo->endIndex - tinfo->startIndex) * tinfo->number_of_elements);
}
void quicksort(int *mat, int n)
{
if (n < 2)
return;
int pivot = mat[n / 2];
int i, j;
for (i = 0, j = n - 1;; i++, j--)
{
while (mat[i] < pivot)
i++;
while (mat[j] > pivot)
j--;
if (i >= j)
break;
int temp = mat[i];
mat[i] = mat[j];
mat[j] = temp;
}
quicksort(mat, i);
quicksort(mat + i, n - i);
}
void bubblesort(int *mat, int n)
{
for (int step = 0; step < n - 1; ++step)
{
for (int i = 0; i < n - step - 1; ++i)
{
if (mat[i] > mat[i + 1])
{
swap(&mat[i], &mat[i + 1]);
}
}
}
}
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main(int argc, char const *argv[])
{
int shmid;
int shmid2;
void *shared_memory = (void *)0;
void *shared_memory2 = (void *)0;
struct shared_use_st *shared_stuff;
key_t keyv = 1337;
key_t kayv = 1447;
shmid = shmget(keyv, sizeof(struct shared_use_st), 0666 | O_RDWR | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
shared_stuff = (struct shared_use_st *)shared_memory;
shmid2 = shmget(kayv, sizeof(int) * (shared_stuff->m * shared_stuff->n), 0666 | O_RDWR | IPC_CREAT);
if (shmid2 == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory2 = (int *)shmat(shmid2, (void *)0, 0);
if (shared_memory2 == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
int processingPackSize = (shared_stuff->m + NUMBER_OF_PROCESSORS - 1) / NUMBER_OF_PROCESSORS;
if (processingPackSize <= 0)
{
processingPackSize = 1;
}
/* Preparing processes
int sem_init(sem_t *sem, int pshared, unsigned int value);
*/
if (sem_init(&mutex, 1, 1) < 0)
{
perror("semaphore initilization");
exit(0);
}
pid_t pids[NUMBER_OF_PROCESSORS];
printf("Memory 1 attached at %X\n", (int)shared_memory);
printf("Memory 2 attached at %X\n", (int)shared_memory2);
for (size_t i = 0; i < sizeof(pids) / sizeof(pids[0]); i++)
{
int startIndex = i * processingPackSize;
int endIndex = min(startIndex + processingPackSize, shared_stuff->m);
if ((pids[i] = fork()) < 0)
{
perror("fork(2) failed");
exit(EXIT_FAILURE);
}
if (pids[i] == 0)
{
struct process_info *tinfo;
/* Allocate memory for pthread_create() arguments */
tinfo = calloc(1, sizeof(struct process_info));
if (tinfo == NULL)
handle_error("calloc");
tinfo->process_num = i;
tinfo->m = shared_stuff->m;
tinfo->n = shared_stuff->n;
tinfo->number_of_elements = shared_stuff->n;
tinfo->processingPackSize = tinfo->endIndex - tinfo->startIndex;
tinfo->startIndex = startIndex;
tinfo->endIndex = endIndex;
printf("\n[son] pid %d from [parent] pid %d and value ou i=%ld, startIndex %d endIndex %d.\n",
getpid(), getppid(), i, startIndex, endIndex);
// printf("\n\n%d\n\n", shared_stuff->unfinished_tasks_data[0][0]);
// printf("@@@@@@@@@ %d\n", tinfo->process_num);
// print_matrix2((int *)shared_memory2, shared_stuff->m, shared_stuff->n);
// printf("@@@@@@@@@ %d\n", tinfo->process_num);
// Critical section
// printf("Reducing task is counter %d.\n", shared_stuff->unfinished_tasks_counter);
start_bubble(tinfo, (int *)shared_memory2);
sem_wait(&mutex);
shared_stuff->unfinished_tasks_counter -= (tinfo->endIndex - tinfo->startIndex);
sem_post(&mutex);
if (shmdt(shared_memory2) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid2, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
free(tinfo);
exit(EXIT_SUCCESS);
}
}
for (size_t i = 0; i < sizeof(pids) / sizeof(pids[0]); i++)
if (waitpid(pids[i], NULL, 0) < 0)
perror("waitpid(2) failed");
if (shmdt(shared_memory2) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid2, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}