/
hw2.cpp
775 lines (677 loc) · 22.4 KB
/
hw2.cpp
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#include <iostream>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <values.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <semaphore.h>
#include <cstring>
#define _UNIX03_THREADS
#define _OPEN_THREADS
using namespace std;
#define NUM_THREADS 3
pthread_mutex_t M;//for fixed size buffer
pthread_mutex_t M1;//for tool1
pthread_mutex_t M2;//for tool2
pthread_mutex_t M3;//for tool3
pthread_mutex_t mutex_pause; //for pause and resume
pthread_mutexattr_t mattr;
pthread_cond_t C;//for generator
pthread_condattr_t cattr;
pthread_cond_t C1;//for operator
pthread_cond_t C2;
pthread_cond_t cond_pause;
int op_num;
int tool_num;
int material1_count=0;
int material2_count=0;
int material3_count=0;
int dead_count=0;//deadlock count
int product1_count=0;
int product2_count=0;
int product3_count=0;
int isPaused = 0;
class queue
{
public:
queue(); // constructor - constructs a new empty queue.
void enqueue( int item ); // enqueues <item>.
int dequeue(); // dequeues the front item.
int front(); // returns the front item without dequeuing it.
bool empty(); // true iff the queue contains no items.
int size(); // the current number of items in the queue.
bool check( long pid); // show products
bool recent( int product);//check for two same products cannot be next to each other
bool check_entire(int product);//check the difference between any two kind should be less than 10
void show();
bool check_buffer(int product);
int check_p();
//int get_1st_tool(int tid);
//int get_2nd_tool(int tool1,int tid);
private:
class node // node type for the linked list
{
public:
node(int new_data, node * next_node ){
data = new_data ;
next = next_node ;
}
int data ;
node * next ;
};
node * front_p ; // pointer to the (node containing the) next item
// which will be dequeud, or NULL if the queue is empty.
node * back_p ; // pointer to the (node containing the) last item
// which was enqueued, or NULL if the queue is empty.
int current_size ; // current number of elements in the queue.
};
queue fix_buffer;//input buffer with size10
queue output_Q;//output queue(unlimited)
queue::queue()
{
front_p = NULL;
back_p = NULL;
current_size=0;
}
void queue::enqueue(int item)
{
node *new_node= new node(item,NULL);
if (front_p==NULL && back_p==NULL)//empty queue
{
back_p=new_node;
front_p=new_node;
}
else
{
back_p->next=new_node;
back_p=new_node;
}
current_size++;
}
int queue::dequeue()
{
/*if (front_p==NULL && back_p==NULL)
{
cout<<"queue is alrady empty";
break;
}*/
node * old_front = front_p;
int temp=old_front->data;
if( front_p==back_p)
{
front_p = NULL;
back_p = NULL;
}
else
{
front_p=front_p->next;
}
delete old_front;
current_size--;
return temp;
}
int queue::size()
{
return current_size;
}
bool queue::empty()
{
return (front_p==NULL && back_p==NULL);
}
int queue::front()
{
return front_p->data;
}
bool queue::check(long pid)
{
node * temp = front_p;
int track=0;
for (int i=0; i<size();i++)
{
if (temp->data==pid)
{
track++;
}
temp=temp->next;
}
if (track>=4)
{
return false;
}
else
{
return true;
}
}
bool queue::recent(int product)
{
if (back_p->data==product)
{
return false;
}
return true;
}
bool queue::check_buffer(int material)
{
int count1=0;
int count2=0;
int count3=0;
node * temp = front_p;
for (int i=0; i<size();i++)
{
if (temp->data==1)
{
count1++;
}
else if (temp->data == 2)
{
count2++;
}
else
{
count3++;
}
temp=temp->next;
}
if (((count1-count2>3)||(count1-count3)>3) && (material==1))
{
return false;
}
else if (((count2-count1>3)||(count2-count3)>3) && (material==2))
{
return false;
}
else if (((count3-count2>3)||(count3-count1)>3) && (material==3))
{
return false;
}
else
{
return true;
}
}
bool queue::check_entire(int product)
{
int count1=0;
int count2=0;
int count3=0;
node * temp = front_p;
for (int i=0; i<size();i++)
{
if (temp->data==3)
{
count1++;
}
else if (temp->data == 4)
{
count2++;
}
else
{
count3++;
}
temp=temp->next;
}
if (((count1-count2>10)||(count1-count3)>10) && (product==3))
{
return false;
}
else if (((count2-count1>10)||(count2-count3)>10) && (product==4))
{
return false;
}
else if (((count3-count2>10)||(count3-count1)>10) && (product==5))
{
return false;
}
else
{
return true;
}
}
void queue::show()
{
node *temp=front_p;
cout<<"show: "<<endl;
for (int i=0; i<this->size();i++)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int queue::check_p()
{
node *t=front_p;
int first = t->data;
t=t->next;
int second = t->data;
int check_product = first+second;
return check_product;
}
/*int queue::get_1st_tool(int tid)
{
int random_tool1=rand() % 3 + 1;
if (random_tool1==1)
{
if(pthread_mutex_trylock(&M1)==0)
{
//tools.enqueue(1);
cout<<"get tools 1 by operator "<<tid<<endl;
//tools.show();
return 1;
}
}
else if (random_tool1==2)
{
if(pthread_mutex_trylock(&M2)==0)
{
//tools.enqueue(2);
cout<<"get tools 2 by operator "<<tid<<endl;
//tools.show();
return 2;
}
}
else if (random_tool1==3)
{
if(pthread_mutex_trylock(&M3)==0)
{
//tools.enqueue(3);
cout<<"get tools 3 by operator "<<tid<<endl;
//tools.show();
return 3;
}
}
}*/
/*int queue::get_2nd_tool(int tool1, int tid)
{
int flag=true;
while (flag)
{
int random_tool2=rand() % 3 + 1;
if (random_tool2!=tool1)
{
if (random_tool2==1)
{
if(pthread_mutex_trylock(&M1)==0)
{
//tools.enqueue(1);
cout<<"get tools 1 by operator "<<tid<<endl;
//tools.show();
flag=false;
return 1;
}
}
else if (random_tool2==2)
{
if(pthread_mutex_trylock(&M2)==0)
{
//tools.enqueue(2);
cout<<"get tools 2 by operator "<<tid<<endl;
//tools.show();
flag=false;
return 2;
}
}
else if (random_tool2==3)
{
if(pthread_mutex_trylock(&M3)==0)
{
//tools.enqueue(3);
cout<<"get tools 3 by operator "<<tid<<endl;
//tools.show();
flag=false;
return 3;
}
}
}
}
}*/
void * generators(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, generator_thread #%ld!\n", tid);
while(true)//for (int i=0; i<10; i++)//change to while(true)
{
pthread_mutex_lock(&mutex_pause);
while (isPaused == 1)
{
pthread_cond_wait(&cond_pause, &mutex_pause);
}
//else
//{
pthread_mutex_unlock(&mutex_pause);
//int rand_time=rand() % 10000;
//usleep(rand_time);
pthread_mutex_lock(&M);//lock mutex
//critical section
int material=tid;
if ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) )
{
dead_count++;
cout<<"Deadlock due to full or too many of the material in the buffer!!!"<<endl;
cout<<"Number of deadlock happened is "<<dead_count<<endl;
}
while ((!fix_buffer.check(tid)) || (!fix_buffer.check_buffer(material)) || (fix_buffer.size() >=10) )//check if there are many of those products
if (pthread_cond_wait (&C, &M))//producer of the material goes to wait
{
fprintf (stdout, "pthread_cond_wait: producer\n");
exit (-1);
}
/*int material=tid;
while(!fix_buffer.check_buffer(material))// wait if the difference are 10
if (pthread_cond_wait (&C, &M))
{
fprintf (stdout, "pthread_cond_wait: consumer\n");
exit (-1);
}*/
printf(" thread #%ld produces %ld\n", tid, material);
if (fix_buffer.size() >= 10)
{
cout<<"Full Wait"<<endl;
}
/*while (fix_buffer.size() >=10)//if its full wait
if (pthread_cond_wait ( &C1 , &M))
{
fprintf (stdout, "pthread_cond_wait: producer\n");
exit (-1);
}*/
fix_buffer.enqueue(material);
//pthread_cond_signal (&C1);
if (material==1)
material1_count++;
else if (material==2)
material2_count++;
else
material3_count++;
cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl;
fix_buffer.show();
cout<<material1_count<<" of material 1 are generated\n";
cout<<material2_count<<" of material 2 are generated\n";
cout<<material3_count<<" of material 3 are generated\n";
pthread_mutex_unlock (&M);
// noncritical section
pthread_cond_signal (&C1);
}
//}
pthread_exit(NULL);
}
void *operators(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, operator_thread #%ld!\n", tid);
queue require;
queue tools;
while(true)//for (int i=0; i<10; i++)//change to while(true)
{
pthread_mutex_lock(&mutex_pause);
while (isPaused == 1)
{
pthread_cond_wait(&cond_pause, &mutex_pause);
}
//else
//{
pthread_mutex_unlock(&mutex_pause);
int rand_time=rand() % 10000;
usleep(rand_time);
pthread_mutex_lock(&M);//lock mutex
//critical section
if (fix_buffer.size()==0)
{
cout<<"Empty"<<endl;
dead_count++;
cout<<"Deadlock due to Empty!!!"<<endl;
cout<<"Number of deadlock happened is "<<dead_count<<endl;
}
while(fix_buffer.size()==0)//wait if fixed size buffer is 0
if (pthread_cond_wait (&C1, &M))
{
fprintf (stdout, "pthread_cond_wait: consumer\n");
exit (-1);
}
if (require.size()==0)//get 1st material
{
int temp=fix_buffer.dequeue();
pthread_cond_signal (&C);
require.enqueue(temp);
cout<<"get "<<temp<<"by "<<tid<<endl;
cout<<"require: "<<endl;
require.show();
}
else if (require.size()==1)//get 2nd material
{
int temp1=fix_buffer.dequeue();
pthread_cond_signal(&C);
if (temp1 != require.front())
{
require.enqueue(temp1);
cout<<"get "<<temp1<<"by "<<tid<<endl;
cout<<"require: "<<endl;
require.show();
int check_product=require.check_p();
if (output_Q.size()==0) //starting queue no need to check for previous product
{
int random_tool1;
int random_tool2;
if(pthread_mutex_trylock(&M1)==0)
{
while(tools.size()<2)
{
if (tools.size()==0)
{
random_tool1=rand() % tool_num + 1;
tools.enqueue(random_tool1);
cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl;
tools.show();
}
else if (tools.size()==1)
{
random_tool1=rand() % tool_num + 1;
if (!(random_tool1==random_tool2))
{
tools.enqueue(random_tool1);
cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl;
tools.show();
}
}
}
}
int product=require.dequeue();
product=product+require.dequeue();
output_Q.enqueue(product);
cout<<"output_Q: "<<endl;
output_Q.show();
if (product==3)
product1_count++;
else if (product==4)
product2_count++;
else
product3_count++;
cout<<product1_count<<" of product 3 are generated\n";
cout<<product2_count<<" of product 4 are generated\n";
cout<<product3_count<<" of product 5 are generated\n";
}
else// if it is not 1st product
{
if ((!output_Q.recent(check_product)) || (!output_Q.check_entire(check_product)))
{
dead_count++;
cout<<"Deadlock due to recent product is same as new product or different number of products are more than 10 !!!"<<endl;
cout<<"Number of deadlock happened is "<<dead_count<<endl;
while(fix_buffer.size() >=10) //check if there are many of those products
if (pthread_cond_wait (&C1, &M))//producer of the material goes to wait
{
fprintf (stdout, "pthread_cond_wait: producer\n");
exit (-1);
}
//for (int i=0;i<2;i++)
//{
int put_back=require.dequeue();
fix_buffer.enqueue(put_back);
pthread_cond_signal (&C1);
cout<<"Prevent Deadlock put : "<<put_back<<" to buffer"<<endl;
fix_buffer.show();
cout<<"current size of fix_buffer is "<<fix_buffer.size()<<endl;
//}
}
/*while(!output_Q.recent(product))//if recent is of same kind then wait
if (pthread_cond_wait (&C2, &M))
{
fprintf (stdout, "pthread_cond_wait: consumer\n");
exit (-1);
}
while(!output_Q.check_entire(product))// wait if the difference are 10
if (pthread_cond_wait (&C2, &M))
{
fprintf (stdout, "pthread_cond_wait: consumer\n");
exit (-1);
}*/
else
{
int random_tool1;
int random_tool2;
if(pthread_mutex_trylock(&M1)==0)
{
while(tools.size()<2)
{
if (tools.size()==0)
{
random_tool1=rand() % tool_num + 1;
tools.enqueue(random_tool1);
cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl;
tools.show();
}
else if (tools.size()==1)
{
random_tool1=rand() % tool_num + 1;
if (!(random_tool1==random_tool2))
{
tools.enqueue(random_tool1);
cout<<"get tools"<<random_tool1<<" by operator "<<tid<<endl;
tools.show();
}
}
}
int product=require.dequeue();
product=product+require.dequeue();
cout<<"product is "<<product<<" by tools"<<tools.dequeue()<<" and "<<tools.dequeue()<<endl;
output_Q.enqueue(product);
cout<<"output_Q: "<<endl;
output_Q.show();
cout<<"current size of output queue is "<<output_Q.size()<<endl;
if (product==3)
product1_count++;
else if (product==4)
product2_count++;
else
product3_count++;
cout<<product1_count<<" of product 3 are generated\n";
cout<<product2_count<<" of product 4 are generated\n";
cout<<product3_count<<" of product 5 are generated\n";
}
}
}//end of not 1st output
}// end of if (temp1 ! = require.front())
else if ((temp1 == require.front()))
{
dead_count++;
cout<<"Deadlock due to both materials are same!!!"<<endl;
cout<<"Number of deadlock happened is "<<dead_count<<endl;
fix_buffer.enqueue(temp1);
pthread_cond_signal (&C1);
}
}
pthread_mutex_unlock ( &M);
pthread_mutex_unlock ( &M1);
pthread_mutex_unlock ( &M2);
pthread_mutex_unlock ( &M3);
//non critical section
pthread_cond_signal (&C);
//}//if pause
}
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_mutex_init(&M, NULL);
pthread_mutex_init(&M1, NULL);
pthread_mutex_init (&M2, NULL);
pthread_mutex_init (&M3, NULL);
pthread_mutex_init (&mutex_pause, NULL);
pthread_cond_init (&C, NULL);
pthread_cond_init (&C1, NULL);
pthread_cond_init (&C2, NULL);
pthread_cond_init (&cond_pause, NULL);
pthread_t producer[NUM_THREADS];
cout<<"Enter number of operators"<<endl;
cin>>op_num;
cout<<"Enter number of tools"<<endl;
cin>>tool_num;
pthread_t consumer[op_num];
int result;
long t;
//creating generators threads
for(t=1; t<=NUM_THREADS; t++)
{
printf("In main: creating generators_thread %ld\n", t);
result = pthread_create(&producer[t], NULL, generators, (void *)t);
if (result)
{
printf("ERROR; return code from pthread_create() is %d\n", result);
exit(-1);
}
}
//creating operators threads
for(t=1; t<=op_num; t++)
{
printf("In main: creating operators_thread %ld\n", t);
result = pthread_create(&consumer[t], NULL, operators, (void *)t);
if (result)
{
printf("ERROR; return code from pthread_create() is %d\n", result);
exit(-1);
}
}
while(true)
{
//press p and enter to pause
// press r and enter to resume
// press q and enter to quit the program
char ch;
//cin.get(ch);
if(cin.get(ch))
{
//char ch;
//cin.get(ch);
if (ch=='p') // press p and enter to pause
{
pthread_mutex_lock(&mutex_pause);
isPaused = 1;
pthread_mutex_unlock(&mutex_pause);
}
else if (ch=='r')//(cin.get()=='r')//press r and enter to resume
{
pthread_mutex_lock(&mutex_pause);
isPaused = 0;
pthread_mutex_unlock(&mutex_pause);
pthread_cond_signal (&cond_pause);
pthread_cond_signal (&cond_pause);
pthread_cond_signal (&cond_pause);
pthread_cond_signal (&cond_pause);
pthread_cond_signal (&cond_pause);
pthread_cond_signal (&cond_pause);
//pthread_mutex_unlock(&mutex_pause);
}
else if (ch == 'q')//(cin.get()=='q')//press q and enter to quit
{
pthread_mutex_lock(&mutex_pause);
return false;
}
}
}
pthread_exit(NULL);
}