-
Notifications
You must be signed in to change notification settings - Fork 0
/
301241917.cpp
854 lines (728 loc) · 19.6 KB
/
301241917.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
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
// 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>
#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 300000000
#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;
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_t child_tid;
pthread_t generator_tid[3];
pthread_t operator_tid[100];
int operator_counter;
pthread_attr_t attr; //Set of thread attributes
int testCounter = 0;
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:
int current_size()
{
return current;
}
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\nDead lock occurs, please PRESS any button to pause, and procedure will be taken to remove it. \n\n\n");
g_inputBufferDeadlockCounter = 1;
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;
}
}
int try_get_materials(Product pd, Material* tempHold)
{
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;
--m1_counter;
--m2_counter;
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;
--m1_counter;
--m3_counter;
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;
--m2_counter;
--m3_counter;
return true;
}
}
else
{
printf("************* There is undefined product in try_get_material\n\n");
return false;
}
}
void clearBuffer()
{
m1_counter = m2_counter = m3_counter = current = 0;
for(int i = 0; i != size; i++)
{
ary[i] = -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("%d ",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 cancel_all_child_thread()
{
pthread_cancel(child_tid);
for(int i = 0; i != 3; i++)
pthread_cancel(generator_tid[i]);
for(int i = 0; i != operator_counter; i++)
pthread_cancel(operator_tid[i]);
}
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);
/*init the semaphore(counter) according to the current size of inputBuffer*/
int size = inputBuffer.current_size();
for(int i = 0; i != size; i++)
{
sem_wait(&empty);
sem_post(&full);
}
}
void clean_data()
{
sem_destroy(&full);
sem_destroy(&empty);
sem_destroy(&tools);
pthread_mutex_destroy(&inputBuffer_mutex);
pthread_mutex_destroy(&outputQueue_mutex);
}
/* Producer Thread */
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)
{
/* sleep for a random period of time */
//int rNum = (rand() / RAND_DIVISOR)%100;
sleep(1);
// printf("\n the testCouter now is %d \n", testCounter++);
// if(testCounter++ > 20)
// {
// printf("\n\n\n generator try to exit\n\n\n");
// g_pause = true;
// //kill(getpid(),SIGSTOP);
// //pthread_exit(0);
// //pthread_cancel(child_tid);
// //cancel_all_child_thread();
// }
/* acquire the empty lock */
sem_wait(&empty);
/* 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);
}
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
/* sleep for a random period of time */
int rNum = (rand() / RAND_DIVISOR)%100;
sleep(rNum);
sem_wait(&tools); // aquire two tools
printf("operator%d fetched the tools\n", operatorsID);
/* aquire the full lock */
sem_wait(&full);
sem_wait(&full);
printf("operators%d finish getting all the materials\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 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 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 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",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 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 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",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 changemode(int);
int kbhit(void);
void *execute_child_process(void* argv)
{
int* args = (int*)argv;
int mainSleepTime = args[0];/* Time in seconds for main to sleep */
int numGeneator = 3; /* Number of generator threads is a const 3 */
int numOperator = args[2]; /* Number of operators threads */
testCounter = 0;
printf("----------------- now in main, before create the generator\n\n\n");
/* Create the generator threads */
int proCounter = 0;
for(int i = 0; i < numGeneator; i++) {
/* Create the thread */
pthread_create(&generator_tid[i],&attr,generator,&proCounter);
proCounter++;
}
/* Create the operators threads */
int conCounter = 0;
for(int i = 0; i < numOperator; i++) {
/* Create the thread */
pthread_create(&operator_tid[i],&attr,operators,&conCounter);
conCounter++;
}
/* Sleep for the specified amount of time in milliseconds */
sleep(mainSleepTime);
/* Exit the program */
return NULL;
}
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 != 3)
{
fprintf(stderr, "\n error: not enough parameters.\nPlease enter in this form: ./301241917_LiuLiang_HW2.exe number_of_tools number_of_operators \n");
}
int mainSleepTime = 5; /* Time in seconds for main to sleep */
//int numGeneator = 3; /* Number of generator threads const 3*/
int numTools = atoi(argv[1]); // number of tools
int numOperator = atoi(argv[2]); /* Number of operators threads */
operator_counter = numOperator;
int args[3] = {mainSleepTime, numTools, numOperator};
/* Initialize the app */
while(TRUE)
{
initializeData(numTools);
pthread_create(&child_tid,&attr,execute_child_process,args); ////////
changemode(1);
while(1)
{
//printf(".");
int ch;
if(kbhit())
{
ch = getchar();
if(ch == 'p')
;
cancel_all_child_thread();
break;
}
}
changemode(0);
printf("\n\n-----------------------------------------------\n");
show_material_total();
printf("-----------------------------------------------\n");
inputBuffer.showState();
printf("-----------------------------------------------\n");
outputQueue.showStatus();
printf("-----------------------------------------------\n");
show_deadlock();
printf("-----------------------------------------------\n");
outputQueue.showOutputQueue();
printf("\n-----------------------------------------------\n");
inputBuffer.clearBuffer();
clean_data(); // reset the semaphores
printf("\n Process is paused now, press any key to resume process.\n\n\n=");
changemode(1);
while(1)
{
//printf(".");
if(kbhit())
{
int ch;
ch = getchar();
if(ch == 'p')
;
break;
}
}
changemode(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);
}