-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_primes.c
551 lines (491 loc) · 10.7 KB
/
proc_primes.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <strings.h>
#include <semaphore.h>
#include <signal.h>
#include "listbag.h"
#define BYTES 536870912
#define TOTAL BYTES*8-1
#define MAX_PROC 8
struct compFinder {
unsigned char *nlist;
unsigned char mask[8];
uint32_t prime;
uint32_t procNo;
uint32_t nproc;
uint32_t total;
int done;
sem_t find[MAX_PROC];
sem_t complete[MAX_PROC];
int happy;
int sad;
int phappy;
int psad;
};
void printBitMap(struct compFinder *, uint32_t, uint32_t );
int getNumPrimes(struct compFinder *cfinder);
uint32_t calcInterm(char *buf);
static struct compFinder *cfinder = NULL;
static sem_t *find = NULL;
static sem_t *complete = NULL;
static sem_t *getid = NULL;
static void *pstruct = NULL;
static char *path;
static uint32_t nproc = 1;
static void mhandler(int sig)
{
int j;
for (j = 0; j < cfinder->nproc; j++)
{
sem_destroy(&find[j]);
sem_destroy(&complete[j]);
}
sem_destroy(getid);
for (j = 0; j < nproc; j++)
wait(NULL);
if (shm_unlink(path) == -1)
{
perror("unlink shared memory\n");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
static void chandler(int sig)
{
exit(EXIT_FAILURE);
}
/* taken from Kevin Mcgrath's lecture on shared memory */
void *mount_shmem(char *path, uint64_t object_size)
{
int shmem_fd;
void *addr;
shmem_fd = shm_open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (shmem_fd == -1)
{
printf("failed to open shared memory object.\n");
exit(EXIT_FAILURE);
}
if (ftruncate(shmem_fd, object_size) == -1)
{
printf("failed to resize shared memory object.\n");
exit(EXIT_FAILURE);
}
addr = mmap(NULL, object_size, PROT_READ | PROT_WRITE, MAP_SHARED,
shmem_fd, 0);
if (addr == MAP_FAILED)
{
printf("failed to map shared memory object.\n");
exit(EXIT_FAILURE);
}
return addr;
}
int main(int argc, char *argv[])
{
uint32_t i, j, k;
uint32_t m;
uint32_t byte, bit;
struct sigaction msa;
struct sigaction csa;
uint64_t object_size = BYTES + 1024*1024;
int opt;
/* child variables */
uint32_t nmult;
uint32_t mpert;
uint32_t temp;
uint32_t a;
uint32_t cbyte, cbit;
uint32_t start, end;
uint32_t prime;
if (argc < 2)
{
printf("Indicate the number of threads.\n");
exit(EXIT_FAILURE);
}
/* map shared memory object, allocate into it */
nproc = atoi(argv[1]);
if (nproc > MAX_PROC)
{
printf("The process limit is 8.\n");
exit(EXIT_FAILURE);
}
path = "/hari_primes";
pstruct = mount_shmem(path, object_size);
cfinder = (struct compFinder *) pstruct;
bzero(cfinder, sizeof(struct compFinder));
cfinder->nlist = (unsigned char *) (&cfinder[1]);
find = &cfinder->find[0];
complete = &cfinder->complete[0];
getid = (sem_t *) (&cfinder->nlist[BYTES]);
/* initialize composite finder struct and number list */
bzero(cfinder->nlist, BYTES);
cfinder->nproc = nproc;
/* initialize semaphores */
for (i = 0; i < nproc; i++)
{
sem_init(&find[i], 1, 0);
sem_init(&complete[i], 1, 0);
}
sem_init(getid, 1, 1);
/* install signal handler */
sigemptyset(&msa.sa_mask);
msa.sa_flags = 0;
msa.sa_handler = mhandler;
if (sigaction(SIGINT, &msa, NULL) == -1)
{
int j;
for (j = 0; j < cfinder->nproc; j++)
{
sem_destroy(&find[j]);
sem_destroy(&complete[j]);
}
sem_destroy(getid);
for (j = 0; j < nproc; j++)
wait(NULL);
if (shm_unlink(path) == -1)
{
perror("unlink shared memory\n");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
/* initialize mask */
for (i = 0; i < 8; i++)
{
cfinder->mask[i] = 1 << (8-i-1);
}
cfinder->total = (uint32_t) ((unsigned long)(BYTES) * 8 - 1);
/* mark the number 1 as special */
cfinder->nlist[0] = cfinder->nlist[0] | cfinder->mask[1];
cfinder->done = 0;
/* create composite marking processes */
for (i = 0; i < nproc; i++)
{
switch(fork())
{
case -1:
for (j = 0; j < nproc; j++)
{
sem_destroy(&find[j]);
sem_destroy(&complete[j]);
}
sem_destroy(getid);
if (shm_unlink(path) == -1)
{
perror("unlink pstruct\n");
exit(EXIT_FAILURE);
perror("forking");
}
exit(EXIT_FAILURE);
break;
case 0: /* child */
/* install child signal handler */
sigemptyset(&csa.sa_mask);
csa.sa_flags = 0;
csa.sa_handler = chandler;
if (sigaction(SIGINT, &csa, NULL) == -1)
{
exit(EXIT_FAILURE);
}
/* get process number */
sem_wait(getid);
temp = cfinder->procNo;
cfinder->procNo++;
sem_post(getid);
/* wait for signal and get prime */
while(1)
{
sem_wait(&cfinder->find[temp]);
if (cfinder->done)
break;
prime = cfinder->prime;
nmult = cfinder->total / prime;
mpert = nmult / cfinder->nproc;
if (!mpert)
{
/* multiples per thread greater
than the number of threads */
for (a = 0; a < nmult - 1; a++)
{
if (temp == a)
{
start = (a + 2) * prime;
end = (a + 2) * prime;
}
}
for (a = nmult - 1; a < cfinder->nproc; a++)
{
/* for inactive threads
just make start
arbitrarily greater
than end */
if (temp == a)
{
start = 4;
end = 2;
}
}
}
else
{
/* set start composite number */
if (!temp)
start = 2 * prime;
else
{
start = (temp * mpert + 2) * prime;
/* set start to first
multiple in byte */
while (1)
{
if ( (start / 8) != ((start - prime) / 8))
{
/* first
multiple
in byte
*/
break;
}
start += prime;
}
}
/* set end composite number */
if (temp == (cfinder->nproc - 1))
{
end = nmult * prime;
}
else
{
end = ((temp + 1) * mpert + 1) * prime;
while(1)
{
/* set end to
last multiple
of byte */
if ( ((end + prime) / 8) != (end / 8))
{
/* last
multiple
of byte
*/
break;
}
end += prime;
}
}
}
for (a = start; a <= end; a += prime)
{
if (!(a % 2) && prime != 2)
{
if ( (cfinder->total - a) < prime)
break;
continue;
}
cbyte = a / 8;
cbit = a % 8;
cfinder->nlist[cbyte] = cfinder->nlist[cbyte] | cfinder->mask[cbit];
if ( (cfinder->total - a) < prime)
break;
}
sem_post(&cfinder->complete[temp]);
}
sem_post(&cfinder->complete[temp]);
/* happy/sad determination */
sem_wait(&cfinder->find[temp]);
if (cfinder->happy | cfinder->sad)
{
/* divide up list of numbers */
struct bag *hcheck = (struct bag *) malloc(sizeof(struct bag));
char buf[33];
if (!temp)
start = 0;
else
start = temp * (BYTES / cfinder->nproc);
if (temp == (cfinder->nproc - 1))
end = BYTES - 1;
else
end = (temp + 1) * (BYTES / cfinder->nproc) - 1;
/* iterate through prime numbers in region */
for (cbyte = start; cbyte <= end; cbyte++)
{
for (cbit = 0; cbit < 8; cbit++)
{
i = 8 * cbyte + cbit;
/* algorithm similar to that shown in
wikipedia page on happy/sad primes */
if (!(cfinder->nlist[cbyte] & cfinder->mask[cbit]))
{
prime = i;
initBag(hcheck);
addToBag(hcheck, 0);
while ((i > 1) && (!bagContains(hcheck,i)))
{
addToBag(hcheck, i);
sprintf(&buf[0], "%u", i);
i = calcInterm(&buf[0]);
}
if (i == 1 && cfinder->phappy)
{
printf("happy: %u\n", prime);
}
else if (i != 1 && cfinder->psad)
{
if (i) /* i != 0 */
printf("sad: %u\n", prime);
}
else {
}
freeBag(hcheck);
}
}
}
free(hcheck);
}
exit(EXIT_SUCCESS);
break;
default:
break;
}
}
/* perform sieve, mark off composites */
for (i = 2; i <= sqrt(cfinder->total);)
{
/* find first unmarked number */
j = i;
while(j < cfinder->total)
{
byte = j / 8;
bit = j % 8;
if (!(cfinder->nlist[byte] & cfinder->mask[bit]))
{
break;
}
j++;
}
if (j >= cfinder->total)
break;
cfinder->prime = j;
/* allowing threads now to cross off composites */
for(k = 0; k < nproc; k++)
sem_post(&find[k]);
/* waiting for all threads to finish before getting next prime
*/
for (k = 0; k < nproc; k++)
{
if (sem_wait(&complete[k])==-1)
perror("error\n");
}
i = ++j;
}
cfinder->done = 1;
/* telling processes to terminate */
for(k = 0; k < nproc; k++)
sem_post(&find[k]);
for (k = 0; k < nproc; k++)
sem_wait(&complete[k]);
/* check what to do next */
while ((opt = getopt(argc, argv, "phs")) == 'p')
{
printBitMap(cfinder, 1, cfinder->total);
}
/* set the proper flags */
while (opt != -1)
{
if (opt == 'h')
{
printf("happy primes will be found\n");
cfinder->happy = 1;
if (getopt(argc, argv, "phs") == 'p')
{
printf("happy primes will be printed \n");
cfinder->phappy = 1;
}
}
else if (opt == 's')
{
printf("sad primes will be found\n");
cfinder->sad = 1;
if (getopt(argc, argv, "phs") == 'p')
{
printf("sad primes will be printed\n");
cfinder->psad = 1;
}
}
else
{
printf("we're done \n");
}
opt = getopt(argc, argv, "phs");
}
/* start up processes again after setting flags */
for(k = 0; k < nproc; k++)
sem_post(&find[k]);
for (k = 0; k < nproc; k++)
wait(NULL);
/* destroy semaphores, delete shared memory object */
for (j = 0; j < nproc; j++)
{
sem_destroy(&find[j]);
sem_destroy(&complete[j]);
}
sem_destroy(getid);
if (shm_unlink(path) == -1)
{
perror("unlink pstruct\n");
exit(EXIT_FAILURE);
}
return 0;
}
void printBitMap(struct compFinder *cfinder, uint32_t start, uint32_t end)
{
uint32_t i;
uint32_t byte;
uint32_t bit;
for (i = start; i <= end; i++)
{
byte = i / 8;
bit = i % 8;
if (!(cfinder->nlist[byte] & cfinder->mask[bit]))
printf("%d\n", i);
}
}
int getNumPrimes(struct compFinder *cfinder)
{
uint32_t i;
uint32_t byte;
uint32_t bit;
uint32_t pcount = 0;
for (i = 0; i <= cfinder->total; i++)
{
byte = i / 8;
bit = i % 8;
if (!(cfinder->nlist[byte] & cfinder->mask[bit]))
pcount++;
}
return pcount;
}
uint32_t calcInterm(char *buf)
{
int i;
int len;
char tempbuf[2];
uint32_t count = 0;
uint32_t temp = 0;
tempbuf[1] = '\0';
len = strlen(buf);
for (i = 0; i < len; i++)
{
tempbuf[0] = buf[i];
temp = (uint32_t)atol(tempbuf);
temp = temp * temp;
count += temp;
}
return count;
}