-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathABecker2Proj4.cpp
More file actions
458 lines (404 loc) · 12.1 KB
/
Copy pathABecker2Proj4.cpp
File metadata and controls
458 lines (404 loc) · 12.1 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
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
/******************************************************************************
//
// @file ABecker2Proj4.cpp
// @author Aidan Becker
// @islandID ABecker2
// @professor Dulal Kar
// @class Operating Systems
// @version 1.0
// Project #4: Process Synchronization Using Pthreads:
// The Producer / Consumer Problem With Prime Number Detector
//
******************************************************************************/
/******************************************************************************
//
// Includes and Defines
//
******************************************************************************/
#include <string>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <vector>
#ifndef _BUFFER_H_DEFINED_
#define _BUFFER_H_DEFINED_
typedef int buffer_item;
#define BUFFER_SIZE 5
bool buffer_insert_item( buffer_item item );
bool buffer_remove_item( buffer_item *item );
#endif //_BUFFER_H_DEFINED_
using namespace std;
/******************************************************************************
//
// Global variables
//
******************************************************************************/
//Where the buffer_insert_item() function will place an item
int buffer_in;
//Where the buffer_remove_item() function will get an item
int buffer_out;
//The buffer itself
buffer_item buff[BUFFER_SIZE];
//seed for random numbers
unsigned int seed;
//flag to tell threads when to stop running
bool sim = true;
//flag to output runtime data
bool verbose_output;
//number of times the buffer was full/empty
long int timesEmpty;
long int timesFull;
//amount of time threads will wait if buffer was empty/full (milliseconds)
int thread_wait_time;
//Holds the number of items produced/consumed by a thread
int *produced;
int *consumed;
pthread_attr_t attr;
//mutual exclusion lock
pthread_mutex_t mutex;
//semaphores
sem_t full;
sem_t empty;
/******************************************************************************
// isPrime function
// Returns true if an integer is prime
// @param int num
// @return bool
******************************************************************************/
bool isPrime(int num)
{
bool flag = true;
for(int i = 2; i <= num/2; i++)
{
if(num% i == 0)
{
flag = false;
break;
}
}
return flag;
}
/******************************************************************************
// initialize function
// Initializes buffer, timesEmpty and timesFull, along with pthread_attribute,
// mutex lock, and semaphores.
// @param none
// @return none
******************************************************************************/
void initialize()
{
buffer_in = 0;
buffer_out = 0;
timesEmpty = 0;
timesFull = 0;
pthread_attr_init(&attr);
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
}
/******************************************************************************
// buffer_insert_item function
// Inserts an item into the buffer at location buffer_in, if the space is free.
// Returns true if the operation was successful, false otherwise
// Buffer logic referenced from http://www.cs.fsu.edu/~baker/realtime/restricted/notes/prodcons.html
// @param buffer_item(integer) item
// @return bool
******************************************************************************/
bool buffer_insert_item( buffer_item item)
{
if( (buffer_in + 1) % BUFFER_SIZE == buffer_out )
{
timesFull++;
return false;
}
buff[buffer_in] = item;
buffer_in = (buffer_in + 1) % BUFFER_SIZE;
return true;
}
/******************************************************************************
// buffer_remove_item function
// Removes an item from the buffer at location buffer_in, if the space is free.
// Returns true if the operation was successful, false otherwise
// Buffer logic referenced from http://www.cs.fsu.edu/~baker/realtime/restricted/notes/prodcons.html
// @param *buffer_item(integer) item
// @return bool
******************************************************************************/
bool buffer_remove_item( buffer_item *item)
{
if( buffer_in == buffer_out )
{
timesEmpty++;
return false;
}
*item = buff[buffer_out];
buffer_out = (buffer_out + 1) % BUFFER_SIZE;
return true;
}
/******************************************************************************
// buffers_occupied function
// Returns the number of items in use in the buffer
// @param none
// @return int
******************************************************************************/
int buffers_occupied()
{
if(buffer_in >= buffer_out)
{
return buffer_in - buffer_out;
}
return buffer_out - buffer_in;
}
/******************************************************************************
// print_buffer_stats function
// Prints stats about items produced/consumed
// @param none
// @return none
******************************************************************************/
void print_buffer_stats()
{
cout << "(buffers occupied: " << buffers_occupied() << ")" << endl;
cout << "buffers: ";
for(int i = 0; i < BUFFER_SIZE; i++)
{
cout << buff[i] << " ";
}
cout << endl << " ---- ---- ---- ---- ----" << endl << " ";
for(int i = 0; i < BUFFER_SIZE; i++)
{
if(buffer_in == buffer_out)
{
if(i+1 % BUFFER_SIZE == buffer_out)
{
cout << "RW ";
}
else if(i+1 % BUFFER_SIZE == buffer_in)
{
cout << "WR ";
}
}
else if((i+1 % BUFFER_SIZE) == buffer_in)
{
cout << "W ";
}
else if((i+1 % BUFFER_SIZE) == buffer_out)
{
cout << "R ";
}
else
{
cout << " ";
}
}
cout << endl << endl;
}
/******************************************************************************
// producer function
// Alternates between sleeping for a random period of time no longer than
// thread_wait_time milliseconds and generating a random number into the buffer.
// @param void *param (unused)
// @return none
******************************************************************************/
void *producer(void *param)
{
//initial timespec declaration for nanosleep
struct timespec sleepTime = {0};
sleepTime.tv_sec = 0;
//specific identity of this consumer thread
int threadNo = param;
//the number of times the buffer was full and the thread attempted to insert an item
int prodCount = 0;
//loop until main tells us to stop
while(sim)
{
//sleep for a random period of time no longer than thread_wait_time milliseconds
sleepTime.tv_nsec = (rand_r(&seed) % thread_wait_time) *1000L;
nanosleep(&sleepTime, (struct timespec *)NULL);
//produce a random integer
buffer_item next_produced = rand_r(&seed) % 100;
//wait on empty
sem_wait(&empty);
//get mutex lock
pthread_mutex_lock(&mutex);
//enter critical section
if( buffer_insert_item(next_produced) )
{
if(verbose_output)
{
cout << "Producer " << pthread_self() << " writes " << next_produced << endl;
print_buffer_stats();
}
produced[threadNo]++;
}
//unlock mutex
pthread_mutex_unlock(&mutex);
//signal full
sem_post(&full);
//leave the critical section
}
sem_post(&full);
sem_post(&empty);
}
/******************************************************************************
// producer function
// Alternates between sleeping for a random period of time no longer than
// thread_wait_time milliseconds and pulling a random number from the buffer.
// determines if the buffer item was prime or not.
// @param void *param (unused)
// @return none
******************************************************************************/
void *consumer(void *param)
{
//variable to hold buffer item
buffer_item next_consumed;
//specific identity of this consumer thread
int threadNo = param;
//sleep for a random period of time no longer than thread_wait_time milliseconds
struct timespec sleepTime = {0};
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = (rand_r(&seed) % thread_wait_time) *1000L;
//the number of times the buffer was empty and the thread attempted to remove an item
int consCount = 0;
//loop until main tells us to stop
while(sim)
{
//sleep for a random period of time no longer than thread_wait_time milliseconds
nanosleep(&sleepTime, (struct timespec *)NULL);
//wait on full
sem_wait(&full);
//get mutex lock
pthread_mutex_lock(&mutex);
//enter critical section
if( buffer_remove_item(&next_consumed) )
{
if(verbose_output)
{
cout << "Consumer " << pthread_self() << " reads " << next_consumed;
if(isPrime(next_consumed))
{
cout << " * * * PRIME * * *" << endl;
}
else
{
cout << endl;
}
print_buffer_stats();
}
consumed[threadNo]++;
}
//unlock mutex
pthread_mutex_unlock(&mutex);
//signal empty
sem_post(&empty);
//leave the critical section
}
sem_post(&empty);
sem_post(&full);
}
int main( int argc, char *argv[] )
{
//Get command line arguments
if( argc != 6 )
{
fprintf(stderr, "Incorrect number of arguments. Requires 4 integer arguments.\n");
return -1;
}
//Argv[0] will be ./proj4
int sim_time = atoi(argv[1]);
thread_wait_time = atoi(argv[2]);
int producer_thread_count = atoi(argv[3]);
int consumer_thread_count = atoi(argv[4]);
verbose_output = false;
//total number of items produced
int totalProduced = 0;
int totalConsumed = 0;
if( !strcmp(argv[5], "yes") )
{
verbose_output = true;
}
//initialize semaphores, mutext, and pthread attribute variable
initialize();
pthread_t *producer_tid = new pthread_t[producer_thread_count];
pthread_t *consumer_tid = new pthread_t[consumer_thread_count];
produced = new int[producer_thread_count];
consumed = new int[consumer_thread_count];
cout << "Starting Threads..." << endl;
//Create producer thread(s)
bool exit = false;
int counter = 0;
for(int i = 0; i < producer_thread_count; i++)
{
if(pthread_create(&producer_tid[i], &attr, producer, i) != 0)
{
fprintf(stderr, "couldn't created the requested number of threads, exiting");
return -1;
}
}
exit = false;
counter = 0;
//Create consumer thread(s)
for(int i = 0; i < consumer_thread_count; i++)
{
//need to guarantee a thread was created
if(pthread_create(&consumer_tid[i], &attr, consumer, i) != 0)
{
fprintf(stderr, "couldn't created the requested number of threads, exiting");
return -1;
}
}
//Sleep until threads are done
struct timespec sleepTime = {0};
sleepTime.tv_sec = sim_time;
nanosleep(&sleepTime, (struct timespec *)NULL);
//after sleeping the correct amount of time, tell threads to stop
sim = false;
//Join Threads
for(int i = 0; i < producer_thread_count; i++)
{
cout << "joining producer" << endl;
pthread_join(producer_tid[i], NULL);
}
for(int i = 0; i < consumer_thread_count; i++)
{
cout << "joining consumer" << endl;
pthread_join(consumer_tid[i], NULL);
}
sleepTime.tv_sec = 1;
nanosleep(&sleepTime, (struct timespec *)NULL);
//output runtime stats
//get number of items produced/consumed
for(int i = 0; i < producer_thread_count; i++)
{
totalProduced += produced[i];
}
for(int i = 0; i < consumer_thread_count; i++)
{
totalConsumed += consumed[i];
}
//Display Statistics
cout << "PRODUCER / CONSUMER SIMULATION COMPLETE" << endl
<< "=======================================" << endl
<< "Simulation Time: " << sim_time << endl
<< "Maximum Thread Sleep Time: " << thread_wait_time << endl
<< "Number of Producer Threads: " << producer_thread_count << endl
<< "Number of Consumer Threads: " << consumer_thread_count << endl
<< "Size of Buffer: " << BUFFER_SIZE << endl
<< "Total Number of Items Produced: " << totalProduced << endl;
for(int i = 0; i < producer_thread_count; i++)
{
cout
<< " Thread " << i << ": " << produced[i] << endl;
}
cout << "Total Number of Items Consumed: " << totalConsumed << endl;
for(int i = 0; i < consumer_thread_count; i++)
{
cout
<< " Thread " << i << ": " << consumed[i] << endl;
}
cout << "Number Of Items Remaining in Buffer: " << totalProduced - totalConsumed << endl
<< "Number of Times Buffer Was Full: " << timesFull << endl
<< "Number Of Times Buffer Was Empty: " << timesEmpty << endl;
return(0);
}