/
main.c
649 lines (544 loc) · 17.1 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "routing.h"
pthread_t random_t, child_t;
pthread_mutex_t crit_m = PTHREAD_MUTEX_INITIALIZER;
int read_random();
int initialize();
int start_alarm();
int finish_alarm();
/* Μεταβλητές που αφορούν τα παιδιά.
* stat_c - Επιστρεφόμενη τιμή απο τα παιδιά
* pid_c[x] - Χώρος αποθήκευσης του pid των παιδιών
* flag_c - flag που δηλώνει αν πρόκειται για παιδί */
int stat_c, pid_c[10], flag_c;
int EndTime;
/* Το power είναι μία σημαντική μεταβλητή στο πρόγραμμα μας.
* Συμβολίζει το ότι το λειτουργικό είναι ανοιχτό και στέλνει
* αιτήσεις. Έτσι ο αλγόριθμος καταλαβαίνει ότι όσο είναι
* σε κατάσταση ON θα εξυπηρετεί αιτήσεις
* if (Power) { sleep(1); } */
int Power;
int main(void) {
short int i;
srand(time(NULL));
initialize();
puts("To πρόγραμμα εξυπηρετεί Χ αιτήσεις όπου Χ ένας τυχαίος αριθμός");
puts("αιτήσεων ο οποίος δημιουργήθηκε σε n δευτερόλεπτα.");
puts("\nΣημείωση:");
puts("Ο χρόνος που θα δημιουργεί αιτήσεις δεν εχει σχέση με τον χρόνο");
puts("που θα εκτελείται το πρόγραμμα");
puts("\nΕισήγαγε τα n δευτερόλεπτα που το πρόγραμμα θα δημιουργεί");
puts("αιτήσεις:");
/* Εισήγαγε μη μηδενική τιμή */
do {
scanf("%d", &EndTime);
} while (EndTime < 0);
for (i = 0; i < 10; i++) {
pid_c[i] = fork();
switch (pid_c[i]) {
case 0:
childs(i);
flag_c = TRUE;
break; //case break
case -1:
perror("Fork SSTF");
return Error;
}
//Αν είσαι παιδί κάνε break απ'το for
if (flag_c == TRUE) {
break;
}
}
if (flag_c == FALSE) {
father();
}
return (EXIT_SUCCESS);
}
/*********************************************************************************
/******************* Δημιουργία των τυχαίων αριθμών ****************************
/*********************************************************************************/
int father() {
int x, i, tmp;
char FileName[40] = "random.txt";
FILE *fp = fopen(FileName, "w");
for (i = 0; i < 10000; i++) {
x = rand() % MaxBlockRange;
fprintf(fp, "%d ", x);
}
fclose(fp);
//Στείλε σήμα στα παιδιά ότι δημιούργησα τυχαίους αριθμούς
for (i = 0; i < 10; i++) {
kill(pid_c[i], SIGALRM);
}
for (i = 0; i < 10; i++) {
waitpid(pid_c[i], &stat_c, 0);
tmp = stat_c >> 8;
if (tmp == TRUE) {
printf("Success");
} else {
printf("Error");
}
switch (i) {
case 0:
printf(" at SSTF \n");
break;
case 1:
printf(" at FCFS \n");
break;
case 2:
printf(" at SCAN down \n");
break;
case 3:
printf(" at SCAN up \n");
break;
case 4:
printf(" at C-SCAN down \n");
break;
case 5:
printf(" at C-SCAN up \n");
break;
case 6:
printf(" at LOOK down \n");
break;
case 7:
printf(" at LOOK up \n");
break;
case 8:
printf(" at C-LOOK down \n");
break;
case 9:
printf(" at C-LOOK up \n");
break;
}
}
return;
}
/*********************************************************************************
/********************* Δημιουργία των αλγόριθμων *******************************
/*********************************************************************************/
int childs(int i) {
/* Αυτή τη περιοχή θα την διαχειριστούμε ως μία δεύτερη submain για τα παιδιά.
* ότι θέλουμε να ισχύει για ολα τα παιδιά θα το δηλώνουμε πριν από τα νήματα.
* Με το i θα ξεχωρίζουμε για ποιο παιδί μιλάμε. */
//Περίμενε τον πατέρα να στείλει σήμα ότι δημιούργησε τους τυχαίους αριθμούς.
signal(SIGALRM, start_alarm);
pause();
//Το πρόγραμμα μας θα εκτελείται όσο χρόνο εμείς του εισάγουμε.
signal(SIGALRM, finish_alarm);
alarm(EndTime);
//Συγκεκριμένος κώδικας για κάθε παιδί ξεχωριστά.
switch (i) {
case 0:
printf("SSTF \n");
if (pthread_create(&child_t, NULL, (void *) &SSTF, NULL) != 0) {
puts("Error creating SSTF thread");
exit(Error);
}
break;
case 1:
printf("FCFS \n");
if (pthread_create(&child_t, NULL, (void *) &FCFS, NULL) != 0) {
puts("Error creating FCFS thread");
exit(Error);
}
break;
case 2:
printf("SCAN down \n");
if (pthread_create(&child_t, NULL, (void *) &SCAN, NULL) != 0) {
puts("Error creating SCAN thread");
exit(Error);
}
break;
case 3:
printf("SCAN up \n");
App.direction = UP;
if (pthread_create(&child_t, NULL, (void *) &SCAN, NULL) != 0) {
puts("Error creating SCAN thread");
exit(Error);
}
break;
case 4:
printf("C-SCAN down \n");
if (pthread_create(&child_t, NULL, (void *) &C_SCAN, NULL) != 0) {
puts("Error creating C-SCAN thread");
exit(Error);
}
break;
case 5:
printf("C-SCAN up \n");
App.direction = UP;
if (pthread_create(&child_t, NULL, (void *) &C_SCAN, NULL) != 0) {
puts("Error creating C-SCAN thread");
exit(Error);
}
break;
case 6:
printf("LOOK down \n");
if (pthread_create(&child_t, NULL, (void *) &LOOK, NULL) != 0) {
puts("Error creating LOOK thread");
exit(Error);
}
break;
case 7:
printf("LOOK up \n");
App.direction = UP;
if (pthread_create(&child_t, NULL, (void *) &LOOK, NULL) != 0) {
puts("Error creating LOOK thread");
exit(Error);
}
break;
case 8:
printf("C-LOOK down \n");
if (pthread_create(&child_t, NULL, (void *) &C_LOOK, NULL) != 0) {
puts("Error creating LOOK thread");
exit(Error);
}
break;
case 9:
printf("C-LOOK up \n");
App.direction = UP;
if (pthread_create(&child_t, NULL, (void *) &C_LOOK, NULL) != 0) {
puts("Error creating LOOK thread");
exit(Error);
}
break;
}
if (pthread_create(&random_t, NULL, (void *) &read_random, NULL) != 0) {
puts("Error creating Random thread");
exit(Error);
}
pthread_join(random_t, NULL);
pthread_join(child_t, NULL);
exit(TRUE);
}
/* Λίγα λόγια για τους αλγόριθμους.
* 1. Όλοι δημιουργούν ένα αρχείο με το όνομα τους. Τα αρχείο που το όνομα τους
* ξεκινάει από 0 σημαίνει ότι ο αλγόριθμος λειτουργεί με κατεύθυνση UP ενώ τα
* αρχεία που το όνομα τους ξεκινάει από 1 σημαίνει ότι ο αλγόριθμος λειτουργεί με
* κατεύθυνση down.
*
* 2. if(Power) { sleep(1); }
* Η συγκεκριμένη εντολή είναι κοινή σε όλους τους αλγόριθμους.
* Όσο το νήμα που συμβολίζει το Λ.Σ. είναι ανοιχτό και στέλνει αιτήσεις
* στο σκληρό τότε κοιμήσου για 1sec αλλιώς εξυπηρέτησε όσες αιτήσεις σου έμειναν.
* Την sleep την εισαγάγαμε προκειμένου να έχουμε πιο εμφανή αποτελέσματα. */
/*********************************************************************************
/******************************** FCFS *****************************************
/*********************************************************************************/
int FCFS() {
short int i;
char FileName[40] = "FCFS.txt";
FILE *fp = fopen(FileName, "w");
while (Power) {
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
// Εξυπηρέτησε σειριακά όσες αιτήσεις δημιουργήθηκαν έως τώρα.
for (i = 0; i < App.QueueSize; i++) {
App.sum += abs(App.CurrentApp - App.Queue[i]);
fprintf(fp, "%d %d %d %d\n", App.CurrentApp, App.Queue[i], App.rndLoop[i], App.sum);
App.CurrentApp = App.Queue[i];
}
App.QueueSize = 0;
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
return TRUE;
}
/**
* SSTF algorithm
*
* @return
*/
int SSTF() {
unsigned int x;
int POS, MIN, tmp;
char FileName[40] = "SSTF.txt";
FILE *fp = fopen(FileName, "w");
fprintf(fp, "Start POS: %d\n", App.CurrentApp);
fprintf(fp, "\nCurrent rndLoop sum\n\n");
while (Power) {
while (App.QueueSize) {
MIN = MaxBlockRange;
pthread_mutex_lock(&crit_m);
for (x = 0; x < App.QueueSize; x++) {
tmp = abs(App.Queue[x] - App.CurrentApp);
if (tmp < MIN) {
MIN = tmp;
POS = x;
}
}
App.sum += MIN;
App.CurrentApp = App.Queue[POS];
App.CurrentRND = App.rndLoop[POS];
remove_app(POS);
fprintf(fp, "%d %d %d \n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
fclose(fp);
return TRUE;
}
/**
* SCAN algorithm
* @return
*/
int SCAN() {
char FileName[40];
sprintf(FileName, "%d SCAN.txt", App.direction);
FILE *fp = fopen(FileName, "w");
fprintf(fp, "Start POS: %d\n", App.CurrentApp);
fprintf(fp, "\nCurrent rndLoop sum\n\n");
while (Power) {
while (App.QueueSize) {
switch (App.direction) {
case UP:
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_up() == FALSE) {
App.sum += abs(App.CurrentApp - MaxBlockRange);
App.CurrentApp = MaxBlockRange - 1;
App.direction = DOWN;
fprintf(fp, "direction changed \n%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
// Κάνουμε Break από το while γιατί θέλουμε να αλλάξουμε κατεύθυνση.
break;
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
pthread_mutex_unlock(&crit_m);
break; //case break
default:
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_down() == FALSE) {
App.sum += abs(App.CurrentApp);
App.CurrentApp = 0;
App.direction = UP;
fprintf(fp, "direction changed \n%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
// Κάνουμε Break από το for γιατί θέλουμε να αλλάξουμε κατεύθυνση.
break;
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
pthread_mutex_unlock(&crit_m);
break; //case break
}
}
}
fclose(fp);
return TRUE;
}
/*********************************************************************************
/****************************** C-SCAN *****************************************
/*********************************************************************************/
int C_SCAN() {
char FileName[40];
sprintf(FileName, "%d C-SCAN.txt", App.direction);
FILE *fp = fopen(FileName, "w");
fprintf(fp, "Start POS: %d\n", App.CurrentApp);
fprintf(fp, "\nCurrent rndLoop sum\n\n");
switch (App.direction) {
case UP:
while (Power) {
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_up() == FALSE) {
App.sum += abs(App.CurrentApp - MaxBlockRange);
App.CurrentApp = 0;
fprintf(fp, "Cycle\n");
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
break; //case break
default:
while (Power) {
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_down() == FALSE) {
App.sum += abs(App.CurrentApp + 1);
App.CurrentApp = MaxBlockRange - 1;
fprintf(fp, "Cycle\n");
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
break; //case break
}
fclose(fp);
return TRUE;
}
/*********************************************************************************
/******************************** LOOK *****************************************
/*********************************************************************************/
int LOOK() {
char FileName[40];
sprintf(FileName, "%d LOOK.txt", App.direction);
FILE *fp = fopen(FileName, "w");
fprintf(fp, "Start POS: %d\n", App.CurrentApp);
fprintf(fp, "\nCurrent rndLoop sum\n\n");
while (Power) {
while (App.QueueSize) {
switch (App.direction) {
case UP:
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_up() == FALSE) {
App.direction = DOWN;
fprintf(fp, "direction changed \n");
break;
// Κάνουμε Break από το for γιατί θέλουμε να αλλάξουμε κατεύθυνση.
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
pthread_mutex_unlock(&crit_m);
break; //case break
default:
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_down() == FALSE) {
App.direction = UP;
fprintf(fp, "direction changed \n");
break;
// Κάνουμε Break από το for γιατί θέλουμε να αλλάξουμε κατεύθυνση.
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
pthread_mutex_unlock(&crit_m);
break; //case break
}
}
}
fclose(fp);
return TRUE;
}
/*********************************************************************************
/******************************** C-LOOK ***************************************
/*********************************************************************************/
int C_LOOK() {
char FileName[40];
sprintf(FileName, "%d C-LOOK.txt", App.direction);
FILE *fp = fopen(FileName, "w");
fprintf(fp, "Start POS: %d\n", App.CurrentApp);
fprintf(fp, "\nCurrent rndLoop sum\n\n");
switch (App.direction) {
case UP:
while (Power) {
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_up() == FALSE) {
/* Μετακινήσου στην μικρότερη */
min_app();
fprintf(fp, "cycle\n");
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
break; //case break
default:
while (Power) {
while (App.QueueSize) {
pthread_mutex_lock(&crit_m);
if (move_down() == FALSE) {
/* Μετακινήσου στην μεγαλύτερη */
max_app();
fprintf(fp, "cycle\n");
}
fprintf(fp, "%d %d %d\n", App.CurrentApp, App.CurrentRND, App.sum);
pthread_mutex_unlock(&crit_m);
if (Power) {
sleep(1);
}
}
}
break; //case break
}
return TRUE;
}
int read_random() {
int count, tmp, loop = 0;
FILE *fp;
char read[40] = "random.txt";
if ((fp = fopen(read, "r")) == NULL) {
printf("Error opening file at pid %d \n", getpid());
return Error;
}
while (Power) {
count = defineCount();
pthread_mutex_lock(&crit_m); //Κρίσιμη περιοχή.
loop++;
while (count--) {
fscanf(fp, "%d", &tmp);
add_app(tmp, loop);
}
pthread_mutex_unlock(&crit_m); //Τέλος Κρίσιμης περιοχή.
//Αφού διάβασες μερικούς αριθμούς πέσε για ύπνο.
sleep(1);
}
//Κλείνουμε τα αρχεία.
fclose(fp);
return TRUE;
}
int initialize() {
//Αρχικοποιούμε το πρόγραμμα μας.
flag_c = FALSE;
Power = ON;
App.QueueSize = 0;
App.sum = 0;
App.QueueCapacity = new_capacity();
App.direction = DOWN;
App.CurrentApp = rand() % MaxBlockRange;
return TRUE;
}
int start_alarm(int sig) {
printf("Είμαι το παιδί με pid %d και ξεκίνησα ", getpid());
return TRUE;
}
int finish_alarm(int sig) {
Power = OFF;
//Σταμάτα το νήμα που διαβάζει αριθμούς.
pthread_cancel(random_t);
//Περίμενε να εξυπηρετήσει και τις υπόλοιπες αιτήσεις.
pthread_join(child_t, NULL);
return TRUE;
}