Repository navigation
Expand file tree
/
Copy pathsolution.cpp
More file actions
409 lines (385 loc) · 14.8 KB
/
Copy pathsolution.cpp
File metadata and controls
409 lines (385 loc) · 14.8 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
#include <assert.h>
#include "solution.h"
#include <unistd.h>
void *producerFunction(void *_arg)
{
// Parse the _arg passed to the function.
// Enqueue `n` items into the `production_buffer`. The items produced should
// be 0, 1, 2,..., (n-1).
// Keep track of the number of items produced and the value produced by the
// thread.
// The producer that was last active should ensure that all the consumers have
// finished. NOTE: Each thread will enqueue `n` items.
// Use mutex variables and conditional variables as necessary.
// *********** MY CODE *********************
// *****************************************
// 1. _arg should contain pointers to:
// production_buffer,
// a mutex,
// two conditional variables,
// an id (source No.) to identify this producer thread,
// the total number of the producer threads
// 2. this function is responsible for enqueuing items
// so, its code body should be wrapped by a lock
// ******************************************
// Reference:
// - https://www.cse.iitd.ac.in/~sbansal/os/lec/l24.html
// - https://www.cse.iitd.ac.in/~sbansal/os/lec/l25.html
// ******************************************
// char queue[MAX]; //global
// int head = 0, tail = 0; //global
// struct cv not_full, not_empty;
// struct lock qlock;
// void produce(char data) {
// acquire(&qlock);
// while ((head + 1) % MAX == tail) {
// wait(¬_full, &qlock);
// }
// queue[head] = data;
// head = (head + 1) % MAX;
// notify(¬_full);
// release(&qlock);
// }
// char consume(void) {
// acquire(&qlock);
// while (tail == head) {
// wait(¬_empty, &qlock);
// }
// e = queue[tail];
// tail = (tail + 1) % MAX;
// notify(¬_empty);
// release(&qlock);
// return e;
// }
// ******************************************
// parse arguments
timer t;
t.start();
CircularQueue *production_buffer = ((params_producer *)_arg)->production_buffer;
pthread_mutex_t *mt = ((params_producer *)_arg)->mt;
pthread_cond_t *not_full = ((params_producer *)_arg)->not_full;
pthread_cond_t *not_empty = ((params_producer *)_arg)->not_empty;
long *n_items = ((params_producer *)_arg)->n_items;
int producer_id = ((params_producer *)_arg)->producer_id; // used to mark source property of every entry (product)
long product_value = producer_id; // incrementd by the total number of producer threads
int producer_number = ((params_producer *)_arg)->producer_number;
long *items_produced = ((params_producer *)_arg)->items_produced;
long *value_produced = ((params_producer *)_arg)->value_produced;
int *active_producer_count = ((params_producer *)_arg)->active_producer_count;
int *active_consumer_count = ((params_producer *)_arg)->active_consumer_count;
double *time_taken_by_producer = ((params_producer *)_arg)->time_taken_by_producer;
long upper_bound = (*n_items) / producer_number;
if (producer_id == 0)
upper_bound += ((*n_items) % producer_number);
while ((*items_produced) < upper_bound)
{
pthread_mutex_lock(mt);
while ((*production_buffer).isFull())
pthread_cond_wait(not_full, mt);
assert((*production_buffer).enqueue(product_value, producer_id));
(*value_produced) += product_value;
(*items_produced)++;
product_value += producer_number;
// if (product_value + producer_number < *n_items)
// {
// product_value += producer_number;
// }
// else
// {
// product_value++;
// }
pthread_cond_signal(not_empty);
pthread_mutex_unlock(mt);
}
if (--(*active_producer_count) == 0)
{
while (*active_consumer_count > 0)
{
pthread_mutex_lock(mt);
pthread_cond_signal(not_empty);
pthread_mutex_unlock(mt);
}
}
*time_taken_by_producer = t.stop();
pthread_exit(_arg);
// *****************************************
}
void *consumerFunction(void *_arg)
{
// Parse the _arg passed to the function.
// The consumer thread will consume items by dequeueing the items from the
// `production_buffer`.
// Keep track of the number of items consumed and the value consumed by the
// thread.
// Once the productions is complete and the queue is also empty, the thread
// will exit. NOTE: The number of items consumed by each thread need not be
// same.
// Use mutex variables and conditional variables as necessary.
// *********** MY CODE *********************
// parse arguments
timer t;
t.start();
CircularQueue *production_buffer = ((params_consumer *)_arg)->production_buffer;
pthread_mutex_t *mt = ((params_consumer *)_arg)->mt;
pthread_cond_t *not_full = ((params_consumer *)_arg)->not_full;
pthread_cond_t *not_empty = ((params_consumer *)_arg)->not_empty;
//int consumer_id = ((params_consumer *)_arg)->consumer_id;
long *items_consumed = ((params_consumer *)_arg)->items_consumed;
long *value_consumed = ((params_consumer *)_arg)->value_consumed;
long *value_from_producers = ((params_consumer *)_arg)->value_from_producers;
// create two variables to recieve the entry info
int producer_id; // used to contain item producer id
long product_value; // used to contain item value
int *active_producer_count = ((params_consumer *)_arg)->active_producer_count;
int *active_consumer_count = ((params_consumer *)_arg)->active_consumer_count;
double *time_taken_by_consumer = ((params_consumer *)_arg)->time_taken_by_consumer;
while (true)
{
pthread_mutex_lock(mt);
while ((*production_buffer).isEmpty())
{
if (*active_producer_count == 0)
{
(*active_consumer_count)--;
pthread_mutex_unlock(mt);
*time_taken_by_consumer = t.stop();
pthread_exit(_arg);
}
pthread_cond_wait(not_empty, mt);
if (*active_producer_count == 0 && (*production_buffer).isEmpty())
{
(*active_consumer_count)--;
pthread_mutex_unlock(mt);
*time_taken_by_consumer = t.stop();
pthread_exit(_arg);
}
}
assert((*production_buffer).dequeue(&product_value, &producer_id));
(*items_consumed)++;
(*value_consumed) += product_value;
value_from_producers[producer_id] += product_value;
pthread_cond_signal(not_full);
pthread_mutex_unlock(mt);
}
// std::cout << "CONSUMER" << consumer_id << " HAS BEEN OUT OF WORK!\n";
// (*active_consumer_count)--;
// *time_taken = t.stop();
pthread_exit(_arg);
// *****************************************
}
ProducerConsumerProblem::ProducerConsumerProblem(long _n_items,
int _n_producers,
int _n_consumers,
long _queue_size)
: n_items(_n_items), n_producers(_n_producers), n_consumers(_n_consumers),
production_buffer(_queue_size)
{
std::cout << "Constructor\n";
//std::cout << "Number of items: " << n_items << "\n";
std::cout << "Number of producers: " << n_producers << "\n";
std::cout << "Number of consumers: " << n_consumers << "\n";
std::cout << "Queue size: " << _queue_size << "\n";
producer_threads = new pthread_t[n_producers];
consumer_threads = new pthread_t[n_consumers];
// Initialize all mutex and conditional variables here.
// *********** MY CODE *********************
producer_params = new params_producer[n_producers];
consumer_params = new params_consumer[n_consumers];
mt = PTHREAD_MUTEX_INITIALIZER;
not_full = PTHREAD_COND_INITIALIZER;
not_empty = PTHREAD_COND_INITIALIZER;
items_produced = new long[n_producers];
value_produced = new long[n_producers];
time_taken_by_producers = new double[n_producers];
items_consumed = new long[n_consumers];
value_consumed = new long[n_consumers];
value_from_producers = new long *[n_consumers];
for (int i = 0; i < n_consumers; i++)
{
value_from_producers[i] = new long[n_producers];
}
time_taken_by_consumers = new double[n_consumers];
// *****************************************
}
ProducerConsumerProblem::~ProducerConsumerProblem()
{
std::cout << "Destructor\n";
delete[] producer_threads;
delete[] consumer_threads;
// Destroy all mutex and conditional variables here.
// *********** MY CODE *********************
delete[] producer_params;
delete[] consumer_params;
pthread_mutex_destroy(&mt);
pthread_cond_destroy(¬_full);
pthread_cond_destroy(¬_empty);
delete[] items_produced;
delete[] value_produced;
delete[] time_taken_by_producers;
delete[] items_consumed;
delete[] value_consumed;
for (int i = 0; i < n_consumers; i++)
{
delete[] value_from_producers[i];
}
delete[] value_from_producers;
delete[] time_taken_by_consumers;
// *****************************************
}
void ProducerConsumerProblem::startProducers()
{
std::cout << "Starting Producers\n";
active_producer_count = n_producers;
// Create producer threads P1, P2, P3,.. using pthread_create.
// *********** MY CODE *********************
for (int i = 0; i < n_producers; i++)
{
(&producer_params[i])->production_buffer = &production_buffer;
(&producer_params[i])->mt = &mt;
(&producer_params[i])->not_full = ¬_full;
(&producer_params[i])->not_empty = ¬_empty;
(&producer_params[i])->n_items = &n_items;
(&producer_params[i])->producer_id = i;
(&producer_params[i])->producer_number = n_producers;
(&producer_params[i])->active_producer_count = &active_producer_count;
(&producer_params[i])->active_consumer_count = &active_consumer_count;
(&producer_params[i])->time_taken_by_producer = &time_taken_by_producers[i];
items_produced[i] = 0;
(&producer_params[i])->items_produced = &items_produced[i];
value_produced[i] = 0;
(&producer_params[i])->value_produced = &value_produced[i];
int err = pthread_create(&producer_threads[i], NULL, producerFunction, &producer_params[i]);
if (err)
std::cout << "Producer thread creation failed : " << strerror(err);
// else
// std::cout << "Producer thread Created with ID : " << i << std::endl;
}
// *****************************************
}
void ProducerConsumerProblem::startConsumers()
{
std::cout << "Starting Consumers\n";
active_consumer_count = n_consumers;
// Create consumer threads C1, C2, C3,.. using pthread_create.
// *********** MY CODE *********************
for (int i = 0; i < n_consumers; i++)
{
(&consumer_params[i])->production_buffer = &production_buffer;
(&consumer_params[i])->mt = &mt;
(&consumer_params[i])->not_full = ¬_full;
(&consumer_params[i])->not_empty = ¬_empty;
(&consumer_params[i])->consumer_id = i;
(&consumer_params[i])->active_producer_count = &active_producer_count;
(&consumer_params[i])->active_consumer_count = &active_consumer_count;
items_consumed[i] = 0;
(&consumer_params[i])->items_consumed = &items_consumed[i];
value_consumed[i] = 0;
(&consumer_params[i])->value_consumed = &value_consumed[i];
(&consumer_params[i])->value_from_producers = value_from_producers[i];
(&consumer_params[i])->time_taken_by_consumer = &time_taken_by_consumers[i];
for (int j = 0; j < n_producers; j++)
{
((&consumer_params[i])->value_from_producers)[j] = 0;
}
int err = pthread_create(&consumer_threads[i], NULL, consumerFunction, &consumer_params[i]);
if (err)
std::cout << "Consumer thread creation failed : " << strerror(err);
// else
// std::cout << "Consumer thread Created with ID : " << i << std::endl;
}
// *****************************************
}
void ProducerConsumerProblem::joinProducers()
{
std::cout << "Joining Producers\n";
// Join the producer threads with the main thread using pthread_join
// *********** MY CODE *********************
for (int i = 0; i < n_producers; i++)
{
// std::cout << "Joining Producer Thread " << i << "\n";
void *anything;
int err = pthread_join(producer_threads[i], &anything);
if (err)
std::cout << "Producer thread join failed : " << strerror(err);
// else
// {
// std::cout << "Producer thread joined : " << i << std::endl;
// }
}
// *****************************************
}
void ProducerConsumerProblem::joinConsumers()
{
std::cout << "Joining Consumers\n";
// Join the consumer threads with the main thread using pthread_join
// *********** MY CODE *********************
for (int i = 0; i < n_consumers; i++)
{
// std::cout << "Joining Consumer Thread " << i << "\n";
void *anything;
int err = pthread_join(consumer_threads[i], &anything);
if (err)
std::cout << "Consumer thread join failed : " << strerror(err);
// else
// {
// std::cout << "Consumer thread joined : " << i << std::endl;
// }
}
// *****************************************
}
void ProducerConsumerProblem::printStats()
{
std::cout << "Producer stats\n";
std::cout << "producer_id, items_produced, value_produced, time_taken \n";
// Make sure you print the producer stats in the following manner
// 0, 1000, 499500, 0.00123596
// 1, 1000, 499500, 0.00154686
// 2, 1000, 499500, 0.00122881
long total_produced = 0;
long total_value_produced = 0;
for (int i = 0; i < n_producers; i++)
{
// *********** MY CODE *********************
std::cout << i << ", " << items_produced[i] << ", " << value_produced[i] << ", "
<< time_taken_by_producers[i]
<< "\n";
total_produced += items_produced[i];
total_value_produced += value_produced[i];
// *****************************************
}
std::cout << "Total produced = " << total_produced << "\n";
std::cout << "Total value produced = " << total_value_produced << "\n";
std::cout << "Consumer stats\n";
std::cout << "consumer_id, items_consumed, value_consumed, time_taken, ";
for (int i = 0; i < n_producers; i++)
{
std::cout << "value_from_producer_" << i;
if (i < n_producers - 1)
std::cout << ", ";
}
std::cout << "\n";
// Make sure you print the consumer stats in the following manner
// 0, 677, 302674, 0.00147414, 120000, 130000, 140000
// 1, 648, 323301, 0.00142694, 130000, 140000, 120000
// 2, 866, 493382, 0.00139689, 140000, 120000, 130000
// 3, 809, 379143, 0.00134516, 109500, 109500, 109500
long total_consumed = 0;
long total_value_consumed = 0;
for (int i = 0; i < n_consumers; i++)
{
// *********** MY CODE *********************
std::cout << i << ", " << items_consumed[i] << ", " << value_consumed[i]
<< ", "
<< time_taken_by_consumers[i];
for (int j = 0; j < n_producers; j++)
{
std::cout << ", " << value_from_producers[i][j];
}
std::cout << "\n";
total_consumed += items_consumed[i];
total_value_consumed += value_consumed[i];
// *****************************************
}
std::cout << "Total consumed = " << total_consumed << "\n";
std::cout << "Total value consumed = " << total_value_consumed << "\n";
}