-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.c
More file actions
151 lines (131 loc) · 3.57 KB
/
primes.c
File metadata and controls
151 lines (131 loc) · 3.57 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
/*
* primes module
* Primary module providing control flow for the primes program
*
* University of Washington, Tacoma
* TCSS 422 - Operating Systems
* Spring 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <time.h>
#include <stdbool.h>
#include "counter.h"
#include "primes.h"
#define OUTPUT 0
counter_t primessearch;
counter_t primescnt;
int genprimes = 1000;
//
int bdone = 0;
//protects bdone variable
pthread_mutex_t donelock;
//used to signal when prime generation is done
pthread_cond_t donecond;
//ensures order of primes printed to screen in order
pthread_mutex_t orderlock;
//finds a prime number
bool findPrime(int threadid)
{
//acquire order lock
pthread_mutex_lock(&orderlock);
int n = inc_counter(&primessearch);
int halfOfn = n / 2;
for (int i=2; i<=halfOfn; i++)
if (n % i == 0)
{
//release order lock if not prime
pthread_mutex_unlock(&orderlock);
return false;
}
//acquire done lock
pthread_mutex_lock(&donelock);
if (bdone==0)
{
#if OUTPUT
switch (threadid)
{
case 1:
printf("\033[0;31m%d,",n);
break;
case 2:
printf("\033[0;32m%d,",n);
break;
case 3:
printf("\033[0;33m%d,",n);
break;
case 4:
printf("\033[0;34m%d,",n);
break;
case 5:
printf("\033[0;35m%d,",n);
break;
case 6:
printf("\033[0;36m%d,",n);
break;
case 7:
printf("\033[1;32m%d,",n);
break;
case 8:
printf("\033[1;34m%d,",n);
}
#endif
}
//release order lock
pthread_mutex_unlock(&orderlock);
//check if incrementing primescnt will reach genprimes
if (inc_counter(&primescnt) == genprimes)
{
//if so set bdone to 1 and signal donecond
bdone=1;
pthread_cond_signal(&donecond);
}
//release done lock
pthread_mutex_unlock(&donelock);
return true;
}
void * generatePrimes(int threadid)
{
while (get_counter(&primescnt) < genprimes)
{
findPrime(threadid);
}
return NULL;
}
int main (int argc, char * argv[])
{
//initialize order lock
pthread_mutex_init(&orderlock,NULL);
pthread_t p1, p2, p3, p4;
init_counter(&primessearch);
init_counter(&primescnt);
//initialize done lock and done condition variable
pthread_mutex_init(&donelock,NULL);
pthread_cond_init(&donecond,NULL);
if (argc == 2)
genprimes = atoi(argv[1]);
pthread_create(&p1, NULL, (void *) generatePrimes, 1); // CREATE PRIME GENERATOR THREAD 1
pthread_create(&p2, NULL, (void *) generatePrimes, 2); // CREATE PRIME GENERATOR THREAD 2
pthread_create(&p3, NULL, (void *) generatePrimes, 3); // CREATE PRIME GENERATOR THREAD 3
pthread_create(&p4, NULL, (void *) generatePrimes, 4); // CREATE PRIME GENERATOR THREAD 4
// pthread_create(&p5, NULL, (void *) generatePrimes, 5); // CREATE PRIME GENERATOR THREAD 5
// pthread_create(&p6, NULL, (void *) generatePrimes, 6); // CREATE PRIME GENERATOR THREAD 6
// pthread_create(&p7, NULL, (void *) generatePrimes, 7); // CREATE PRIME GENERATOR THREAD 7
// pthread_create(&p8, NULL, (void *) generatePrimes, 8); // CREATE PRIME GENERATOR THREAD 8
// pthread_join(p1, NULL);
// pthread_join(p2, NULL);
// pthread_join(p3, NULL);
// pthread_join(p4, NULL);
// pthread_join(p5, NULL);
// pthread_join(p6, NULL);
// pthread_join(p7, NULL);
// pthread_join(p8, NULL);
//Wait for any threads to signal donecond
pthread_mutex_lock(&donelock);
while (bdone==0)
pthread_cond_wait(&donecond, &donelock);
printf("\b \n");
return 0;
}