-
Notifications
You must be signed in to change notification settings - Fork 0
/
backUp_try_failed
1951 lines (1671 loc) · 44.8 KB
/
backUp_try_failed
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string>
#include <math.h>
#include <sys/wait.h> /* Wait for Process Termination */
#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#define RAND_DIVISOR 1000000000
#define TIME_LIMIT 100000000
#define TRUE 1
#define FALSE 0
typedef int Material;
typedef int Product;
#define BUFFER_SIZE 10 // in fact in this particular problem BUFFER_SIZE is a constant 10
#define MATERIAL_1 1
#define MATERIAL_2 2
#define MATERIAL_3 3
#define PRODUCT_1 1 // produced form MATERIAL_1 and MATERIAL_2
#define PRODUCT_2 2 // produced from MATERIAL_1 and MATERIAL_3
#define PRODUCT_3 3 // produced form MATERIAL_2 and MATERIAL_3
bool g_pause = FALSE;
//m how many material have been generated
//b the state of the input buffer
//r for each kind of product, how many are produced
//q the state of outputQueue
//d how many times deadlock occurs
//p for pause
int g_material_1_count = 0;
int g_material_2_count = 0;
int g_material_3_count = 0;
int g_inputBufferDeadlockCounter;
int g_outputQueueDeadlockCounter;
/* the semaphores */
sem_t full, empty;
sem_t tools;
/* The mutex lock */
pthread_mutex_t inputBuffer_mutex;
pthread_mutex_t outputQueue_mutex;
pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes
void changemode(int);
int kbhit(void);
void show_material_total()
{
printf("There are %d material_1 have been generated\n",g_material_1_count);
printf("There are %d material_2 have been generated\n",g_material_2_count);
printf("There are %d material_3 have been generated\n",g_material_3_count);
}
void show_deadlock()
{
printf("There occurs %d times deadlock because of inputBuffer\n",g_inputBufferDeadlockCounter);
printf("There occurs %d times deadlock because of outputQueue\n",g_outputQueueDeadlockCounter);
}
class InputBuffer //a stack buffer to store the Material
{
private:
int size;
int current;
Material* ary;
int m1_counter;
int m2_counter;
int m3_counter;
public:
InputBuffer(int s):size(s),current(0),m1_counter(0),m2_counter(0),m3_counter(0)
{
ary = new int[size];
for(int i = 0; i != size; i++)
{
ary[i] = -1; // means no material at that index
}
}
~InputBuffer(){delete ary;}
bool check(Material item) // when there is only one empty slot remains, with the latest insert one, there must be three kind of material in input buffer, otherwise we won't push it in the ary.
{
if(current < size - 1)
return false;
else
{
int detect[3] = {0,0,0}; // to detect whether there is material_1 or 2 or 3 in the ary
if(item == MATERIAL_1)
detect[0] = 1;
else if (item == MATERIAL_2)
detect[1] = 1;
else if (item == MATERIAL_3)
detect[2] = 1;
for(int i = 0; i != size; i++)
{
if(ary[i] == MATERIAL_1)
detect[0] = 1;
else if (ary[i] == MATERIAL_2)
detect[1] = 1;
else if (ary[i] == MATERIAL_3)
detect[2] = 1;
}
// if the ary is full and there is only 2 or 1 kind of material, deadlock may occur, so discard all the material
if((detect[0] + detect[1] + detect[2]) < 3)
{
printf("\n\n\n********error occurs in check\n\n\n");
g_inputBufferDeadlockCounter++;
return true;
}
else
return false;
}
}
int push(Material item)
{
if(check(item))
return -2;
if(current < size)
{
if(item == MATERIAL_1)
{
m1_counter++;
++g_material_1_count;
}
else if(item == MATERIAL_2)
{
m2_counter++;
++g_material_2_count;
}
else if(item == MATERIAL_3)
{
m3_counter++;
++g_material_3_count;
}
else
{
printf("********* error: trying to put undefined material into InputBuffer\n \n"); // this line is not necessary can be removed in the future
return -1;
}
for(int i = 0; i != size; i++)
{
if(ary[i] == -1) // is empty
{
ary[i] = item;
current++;
return 0;
}
}
printf("********* push has some errors **************\n \n");
return -1;
}
else
{ /* Error the buffer is full */
printf("*********** error: try to push into a full ary\n\n\n");
return -1;
}
}
// Material try_pop_m1()
// {
// if(current > 0)
// {
// for(int i = 0; i != size; i++)
// {
// if(ary[i] == MATERIAL_1)
// {
// ary[i] = -1;
// current--;
// return MATERIAL_1;
// }
// }
// return -1; // means no MATERIAL_1 is found
// }
// else
// {
// printf("*********** error: try to pop from a empty ary\n \n");
// return -1; // error occurs
// }
// }
//
// Material try_pop_m3()
// {
// if(current > 0)
// {
// for(int i = 0; i != size; i++)
// {
// if(ary[i] == MATERIAL_3)
// {
// ary[i] = -1;
// current--;
// return MATERIAL_3;
// }
// }
// return -1; // means no MATERIAL_1 is found
// }
// else
// {
// printf("*********** error: try to pop from a empty ary\n \n");
// return -1; // error occurs
// }
// }
//
// Material try_pop_m2()
// {
// if(current > 0)
// {
// for(int i = 0; i != size; i++)
// {
// if(ary[i] == MATERIAL_2)
// {
// ary[i] = -1;
// current--;
// return MATERIAL_2;
// }
// }
// return -1; // means no MATERIAL_1 is found
// }
// else
// {
// printf("*********** error: try to pop from a empty ary\n \n");
// return -1; // error occurs
// }
// }
int try_get_materials(Product pd, Material* tempHold)
{
//check_clear();
if(pd == PRODUCT_1)
{
int index1 = -1;
int index2 = -1;
for(int i = 0; i != size; i++)
{
if(ary[i] == MATERIAL_1)
index1 = i;
else if (ary[i] == MATERIAL_2)
index2 = i;
if((index1 != -1)&&(index2 != -1))
break;
}
if(index1 == -1 || index2 == -1)
return false;
else
{
ary[index1] = -1;
ary[index2] = -1;
current -= 2;
return true;
}
}
else if(pd == PRODUCT_2)
{
int index1 = -1;
int index2 = -1;
for(int i = 0; i != size; i++)
{
if(ary[i] == MATERIAL_1)
index1 = i;
else if (ary[i] == MATERIAL_3)
index2 = i;
if((index1 != -1)&&(index2 != -1))
break;
}
if(index1 == -1 || index2 == -1)
return false;
else
{
ary[index1] = -1;
ary[index2] = -1;
current -= 2;
return true;
}
}
else if(pd == PRODUCT_3)
{
int index1 = -1;
int index2 = -1;
for(int i = 0; i != size; i++)
{
if(ary[i] == MATERIAL_3)
index1 = i;
else if (ary[i] == MATERIAL_2)
index2 = i;
if((index1 != -1)&&(index2 != -1))
break;
}
if(index1 == -1 || index2 == -1)
return false;
else
{
ary[index1] = -1;
ary[index2] = -1;
current -= 2;
return true;
}
}
else
{
printf("************* There is undefined product in try_get_material\n\n");
return false;
}
}
// Material pop() // won't use in the future
// {
// Material item = -1;
// if(current > 0)
// {
// for(int i =0; i != size; i++)
// {
// if(ary[i] != -1)
// {
// item = ary[i];
// ary[i] = -1;
// --current;
// break;
// }
// }
// if(item == MATERIAL_1)
// m1_counter--;
// else if(item == MATERIAL_2)
// m2_counter--;
// else if(item == MATERIAL_3)
// m3_counter--;
// return item;
// }
// else
// { /* Error buffer empty */
// return -1;
// }
//
// }
void showState()
{
printf("The state of InputBuffer Now is:\n");
printf("Totoal: %d\n", current);
printf("Material_1: %d\n", m1_counter);
printf("Material_2: %d\n", m2_counter);
printf("Material_3: %d\n", m3_counter);
}
void showAry()
{
//pthread_mutex_lock(&inputBuffer_mutex);
printf("the current Array is: \n");
for(int i = 0; i != size; i++)
{
printf("B: %d\n",ary[i]);
}
//pthread_mutex_unlock(&inputBuffer_mutex);
}
};
class OutputQueue // just an unlimited area to store the output of the operator
{
private:
Product* ary;
int current;
int p1_counter;
int p2_counter;
int p3_counter;
public:
OutputQueue():current(0),p1_counter(0),p2_counter(0),p3_counter(0){ary = new int[1000000];}
~OutputQueue(){delete ary;}
int size()
{
return current;
}
void check_priority_next(Product* priority) // to check produce which product next is best
{
if(current == 0) // if there is no product in the queue now, let the generator generate Product_3 first
{
priority[0] = PRODUCT_3;
priority[1] = PRODUCT_2;
}
else
{
if(ary[current-1] == PRODUCT_1)
{
if(p2_counter < p3_counter)
{
priority[0] = PRODUCT_2;
priority[1] = PRODUCT_3;
}
else
{
priority[0] = PRODUCT_3;
priority[1] = PRODUCT_2;
}
}
else if(ary[current-1] == PRODUCT_2)
{
if(p1_counter < p3_counter)
{
priority[0] = PRODUCT_1;
priority[1] = PRODUCT_3;
}
else
{
priority[0] = PRODUCT_3;
priority[1] = PRODUCT_1;
}
}
else if(ary[current-1] == PRODUCT_3)
{
if(p2_counter < p1_counter)
{
priority[0] = PRODUCT_2;
priority[1] = PRODUCT_1;
}
else
{
priority[0] = PRODUCT_1;
priority[1] = PRODUCT_2;
}
}
else
{
printf("********** error: there is undefined product in outputQueue\n");
}
}
}
void push(Product pd)
{
ary[current++] = pd;
if(pd == PRODUCT_1)
p1_counter++;
else if (pd == PRODUCT_2)
p2_counter++;
else if(pd == PRODUCT_3)
p3_counter++;
else
printf("******* error: in push of outputqueue\n\n\n");
}
bool validate_number(int n1, int n2, int n3)
{
if((n1 - n2 > 10) || (n1 - n2 < -10))
return false;
if((n1 - n3 > 10) || (n1 - n3 < -10))
return false;
if((n2 - n3 > 10) || (n2 - n3 < -10))
return false;
return true;
}
void try_insert_product(Product pd)
{
if(pd == ary[current - 1])
{
printf("********* error: same product next to each other\n\n\n");
g_outputQueueDeadlockCounter++;
}
else
{
if(pd == PRODUCT_1)
{
if(validate_number(p1_counter + 1, p2_counter, p3_counter))
push(pd);
else
{
printf("********** error invalid number constraint, dealLock occurs, discard this product_%d to make the process continue\n\n\n",pd);
g_outputQueueDeadlockCounter++;
}
}
if(pd == PRODUCT_2)
{
if(validate_number(p1_counter, p2_counter + 1, p3_counter))
push(pd);
else
{
printf("***************** error invalid number constraint, dealLock occurs, discard this product_%d to make the process continue\n\n\n",pd);
g_outputQueueDeadlockCounter++;
}
}
if(pd == PRODUCT_3)
{
if(validate_number(p1_counter, p2_counter, p3_counter + 1))
push(pd);
else
{
printf("************ error invalid number constraint, dealLock occurs, discard this product_%d to make the process continue\n\n\n",pd);
g_outputQueueDeadlockCounter++;
}
}
}
}
void showStatus() // remember not to use mutex again around this fuction.
{
printf("There are totally %d product in outputQueue now.\n",current);
printf("product_1: %d \n",p1_counter);
printf("product_2: %d \n",p2_counter);
printf("product_3: %d \n",p3_counter);
printf("show input status over\n");
}
void showOutputQueue()
{
printf("the outputQueue Now are:\n");
for(int i = 0; i != current; i++)
{
printf("Q: %d\n",ary[i]);
}
}
};
/* the inputbuffer */
InputBuffer inputBuffer(BUFFER_SIZE);
// the outputQueue
OutputQueue outputQueue;
void *generator(void *param); /* the generator thread */
void *operators(void *param); /* the operators thread */
int insert_item(Material item);
int remove_item(Material *item);
void initializeData(int nTools)
{
/* Create the inputBuffer_mutex lock */
pthread_mutex_init(&inputBuffer_mutex, NULL);
pthread_mutex_init(&outputQueue_mutex, NULL);
/* Create the full semaphore and initialize to 0 */
sem_init(&full, 0, 0);
/* Create the empty semaphore and initialize to BUFFER_SIZE */
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&tools,0, nTools/2); // in my strategy, the operator will whether fetch two tools or not fetch any tools, so, the last odd one is useless, by dividing the number of tools by 2, each down and up on semaphore means get or put back two tools
//printf("there are %d tools \n\n\n\n",nTools/2);
/* Get the default attributes */
pthread_attr_init(&attr);
}
void test_input()
{
long int timeCounter = 0;
while(++timeCounter != TIME_LIMIT) // ****** just for test need a sleep or counter in fact
{
//printf(".");
char ch;
if(kbhit())
{
ch = getchar();
if(ch == 'p')
{
printf("\n\n\n h ere p is clicked\n\n\n");
g_pause = true;
kill(getpid(),SIGSTOP);
}
else if(ch == 'k')
{
printf("k is clicked\n");
inputBuffer.showState();
}
break;
}
}
}
/* Producer Thread */
int testCounter; // for test, stop the child process and resume
void *generator(void *param)
{
int generatorID = *(int*)param;
Material materialID = generatorID;
// the materialID is equal to generatorID, which means the material generated by generator1 is 1(MATERIAL_1),etc
while(TRUE) {
printf("\n$$$$$$$ generator pid is %d $$$$$$$$$$\n", getpid()); //////////////////
/* sleep for a random period of time */
//printf("Now in generator%d, sleep for a while\n",generatorID);
//int rNum = (rand() / RAND_DIVISOR)%100;
sleep(1);
printf("\n the testCouter now is %d \n", testCounter++);
if(testCounter > 10)
{
printf("\n\n\n here p is clicked\n\n\n");
//g_pause = true;
//kill(getpid(),SIGSTOP);
}
//test_input();
printf("generator%d try to down the semaphore\n",generatorID);
/* acquire the empty lock */
sem_wait(&empty);
printf("generator%d finish to down the semaphore.\n", generatorID);
/* acquire the inputBuffer_mutex lock */
pthread_mutex_lock(&inputBuffer_mutex);
//printf("generator%d enter the critical section.\n", generatorID);
int temp = inputBuffer.push(materialID);
if(temp == -1) {
printf("************* error:generator%d report error condition\n", generatorID);
}
else if (temp == -2)
{
sem_post(&empty);
}
else {
printf("generator%d produced MATERIAL_%d\n", generatorID, materialID);
sem_post(&full);
}
//inputBuffer.showAry();
/* release the inputBuffer_mutex lock */
pthread_mutex_unlock(&inputBuffer_mutex);
//printf("generator%d exits the critical section, and try to up the semaphore\n",generatorID);
/* signal full */
//sem_post(&full);
//printf("generator%d finish up the semaphore\n",generatorID);
}
return NULL;
}
/* Consumer Thread */
void *operators(void *param) {
int operatorsID = *(int*)param;
while(TRUE) {
Material temp_materials[2] = {-1,-1}; // -1 means no material holded
//printf("$$$$$$$ pid is %d $$$$$$$$$$\n", getpid()); /////////////
/* sleep for a random period of time */
//printf("now in operators%d, sleep for a while\n",operatorsID);
int rNum = (rand() / RAND_DIVISOR)%100;
sleep(rNum);
//test_input();
//printf("operator%d finish sleep\n",operatorsID);
printf("operator%d try to fetch the tools\n", operatorsID);
sem_wait(&tools); // aquire two tools
//printf("There are %d tools in process \n\n\n",(int)tools);
printf("operator%d fetched the tools\n", operatorsID);
/* aquire the full lock */
printf("operators%d try to down get the first material semaphore\n", operatorsID);
sem_wait(&full);
printf("operators%d try to down get the second material semaphore \n", operatorsID);
sem_wait(&full);
printf("operators%d finish getting all the materials semaphores \n",operatorsID);
/* aquire the inputBuffer_mutex lock */
pthread_mutex_lock(&inputBuffer_mutex);
if(outputQueue.size() == 0) // if there is no product in ouputque now, produce whatever we can produce
{
if(inputBuffer.try_get_materials(PRODUCT_1,temp_materials)) //$$$$$$$$$$$$ maybe don't need temp_materials
{
pthread_mutex_lock(&outputQueue_mutex);
outputQueue.try_insert_product(PRODUCT_1);
printf("^^^^^^^ operator %d MAY produce the product%d\n",operatorsID,PRODUCT_1);
pthread_mutex_unlock(&outputQueue_mutex);
sem_post(&empty);
sem_post(&empty);
//sem_post(&tools);
}
else if(inputBuffer.try_get_materials(PRODUCT_2,temp_materials))
{
pthread_mutex_lock(&outputQueue_mutex);
outputQueue.try_insert_product(PRODUCT_2);
printf("^^^^^^^ operator %d MAY produce the product%d\n",operatorsID,PRODUCT_2);
pthread_mutex_unlock(&outputQueue_mutex);
sem_post(&empty);
sem_post(&empty);
//sem_post(&tools);
}
else if(inputBuffer.try_get_materials(PRODUCT_3,temp_materials))
{
pthread_mutex_lock(&outputQueue_mutex);
outputQueue.try_insert_product(PRODUCT_3);
printf("^^^^^^^ operator %d MAY produce the product%d\n",operatorsID,PRODUCT_3);
pthread_mutex_unlock(&outputQueue_mutex);
sem_post(&empty);
sem_post(&empty);
}
else
{
sem_post(&full);
sem_post(&full);
printf("^^^^^^^ operator_%d put the materials back to the inputBuffer\n\n",operatorsID);
//sem_post(&tools)
}
}
else{
//printf("operators%d enter the critical section\n",operatorsID);
Product priority[2]; //ask the outputQueue which product to operate first
outputQueue.check_priority_next(priority);
if(inputBuffer.try_get_materials(priority[0],temp_materials)) //$$$$$$$$$$$$ maybe don't need temp_materials
{
pthread_mutex_lock(&outputQueue_mutex);
outputQueue.try_insert_product(priority[0]);
printf("^^^^^^^ operator %d MAY produce the product%d\n",operatorsID,priority[0]);
pthread_mutex_unlock(&outputQueue_mutex);
sem_post(&empty);
sem_post(&empty);
//sem_post(&tools);
}
else if(inputBuffer.try_get_materials(priority[1],temp_materials))
{
pthread_mutex_lock(&outputQueue_mutex);
outputQueue.try_insert_product(priority[1]);
printf("^^^^^^^ operator %d MAY produce the product%d\n",operatorsID,priority[1]);
pthread_mutex_unlock(&outputQueue_mutex);
sem_post(&empty);
sem_post(&empty);
//sem_post(&tools);
}
else
{
sem_post(&full);
sem_post(&full);
printf("^^^^^^^ operator_%d put the materials back to the inputBuffer\n\n",operatorsID);
//sem_post(&tools)
}
}
//inputBuffer.showAry();
pthread_mutex_unlock(&inputBuffer_mutex);
//printf("operators%d exit the critical section\n",operatorsID);
/* signal empty */
printf("operator%d put back the tools\n",operatorsID);
sem_post(&tools); //release the two tools
}
return NULL;
}
/* Add an item to the buffer */
/* Remove an item from the buffer */
void execute_child_process(int* args);
void h_sigcont(int sig)
{
printf("\n\\n $$$$###### sig cont is trigged ###########$$$$$$$$$$\n\n\n");
}
int main(int argc, char *argv[])
{
pid_t child_pid; /* variable to store the child's pid */
int child_status; /* parent process: child's exit status */
/* Verify the correct number of arguments were passed in */
if(argc != 4)
{
fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
}
int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
//int numGeneator = 3; /* Number of generator threads const 3*/
int numTools = atoi(argv[2]); // number of tools
int numOperator = atoi(argv[3]); /* Number of operators threads */
int args[3] = {mainSleepTime, numTools, numOperator};
/* Initialize the app */
initializeData(numTools);
signal(SIGCONT,h_sigcont);
//signal(SIGSTOP,h_sigstop);
//while(TRUE)
//{
child_pid = fork();
if(child_pid == 0) // child process
{
//********* maybe only need to execute once, depend on how fork works and when the child process release
execute_child_process(args);
}
else // parent process
{
//changemode(1);
do
{
printf("############## main pid is %d $$$$$$$$$$\n \n \n \n\n\n\n\n\n\n\n", getpid());
//int ch;
//long long int timeCounter = 0;
// changemode(1);
// while(++timeCounter != TIME_LIMIT) // ****** just for test need a sleep or counter in fact
// {
// //printf(".");
// if(kbhit())
// {
// ch = getchar();
// if(ch == 'p')
// {
// printf("**********************\n\np is clicked\n ****************\n\n");
// g_pause = !g_pause;
// }
// else if(ch == 'k')
// {
// printf("k is clicked\n");
// inputBuffer.showState();
// }
// break;
// }
//
//
// }
// printf("time out\n");
// changemode(0);
// if (!g_pause) {
// kill(child_pid, SIGCONT);
// } else {
// kill(child_pid, SIGSTOP);
// }
sleep(1);
} while (0 == waitpid(child_pid, &child_status, WUNTRACED)); // maybe don't need this line
printf("\n########## main child_pid is %d ######## \n\n",child_pid);
printf("\n\n\n come back to the main\n\n\n");
kill(-1,SIGCONT);
}
//}
}
void execute_child_process(int* args)
{
int mainSleepTime = args[0];/* Time in seconds for main to sleep */
int numGeneator = 3; /* Number of generator threads is a const 3 */
//int numTools = args[1]; // Number of tools
int numOperator = args[2]; /* Number of operators threads */
//printf("----------------- now in main, before create the generator\n\n\n");
printf("$$$$$$$ excute_child pid is %d $$$$$$$$$$\n", getpid());///////////////
/* Create the generator threads */
static int proCounter = 0;
for(int i = 0; i < numGeneator; i++) {
/* Create the thread */
pthread_create(&tid,&attr,generator,&proCounter);
proCounter++;
}
//printf("----------------- now in main, finish create the generator, before create the operators\n");
/* Create the operators threads */
static int conCounter = 0;
for(int i = 0; i < numOperator; i++) {
/* Create the thread */
pthread_create(&tid,&attr,operators,&conCounter);
conCounter++;
}
/* Sleep for the specified amount of time in milliseconds */
//printf("---------------- now in main , finish create operators and before main sleep\n");
sleep(mainSleepTime);
/* Exit the program */
//printf("Exit the program\n");
printf("\n\n\n&&&&&&&& testCounter: %d &&&&&&&\n\n\n", testCounter);
exit(0);
}
void changemode(int dir)
{
static struct termios oldt, newt;
if ( dir == 1 )
{
tcgetattr( STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt);
}
else
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
int kbhit (void)
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET (STDIN_FILENO, &rdfs);
select(STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &rdfs);
}
// this is a back_up before trying to use fork();
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>