-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
967 lines (816 loc) · 23 KB
/
simulator.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
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
/* FILE NAME: simulator.c
AUTHOR : Harshi Kasundi Bandaranayake
DATE : 07/05/2022
INCLUDES : fcfs.h, scan.h, cscan.h, look.h, clook.h, sstf.h */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
/*DEFINING CONSTANTS*/
#define BUFFERSIZE 1
#define THREAD_COUNT 6
#define TRUE 1
#define FALSE !TRUE
#define VISITED -1
#define WAITING !VISITED
/*DEFINING DATA SHARED BY PARENT AND CHILD THREADS*/
int count = 0; int check = -1; int emptyspaces = BUFFERSIZE; int hasFile = FALSE; int isEnd = FALSE; int threadcount = 0; int canRead = FALSE; int writercount = FALSE;
int fcfs_write = FALSE; int sstf_write = FALSE; int scan_write = FALSE; int cscan_write = FALSE; int look_write = FALSE; int clook_write = FALSE; int bufferReady = FALSE;
int* buffer1; int buffer2[BUFFERSIZE];
/*INITIALIZING MUTEXES*/
pthread_mutex_t fcfs_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t sstf_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t scan_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cscan_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t look_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t clook_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t start_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t end_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t input_mutex = PTHREAD_MUTEX_INITIALIZER;
/*INITIALIZING CONDITIONAL VARIABLES*/
pthread_cond_t fcfs_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t sstf_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t scan_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cscan_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t look_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t clook_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t readbuffer1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t readbuffer2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t terminate = PTHREAD_COND_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t got_filename = PTHREAD_COND_INITIALIZER;
/*FUNCTION DECLARATION*/
void countData(char* filename);
void *fcfs(void *arg);
void *sstf(void *arg);
void *scan(void *arg);
void *cscan(void *arg);
void *look(void *arg);
void *clook(void *arg);
/*MAIN WILL BE THE PARENT THREAD THAT CREATES THREADS A, B, C, D, E & F*/
int main(int argc, char* argv[])
{
char filename[10]; char str[4096]; int seektime[THREAD_COUNT]; char* token; FILE *fptr = NULL; int i = 0;
/*SETTING UP THE THREAD IDs*/
pthread_t thread_A; pthread_t thread_B; pthread_t thread_C;
pthread_t thread_D; pthread_t thread_E; pthread_t thread_F;
/*CREATES THE CHILD THREADS*/
pthread_create(&thread_A, NULL, fcfs, NULL);
pthread_create(&thread_B, NULL, sstf, NULL);
pthread_create(&thread_C, NULL, scan, NULL);
pthread_create(&thread_D, NULL, cscan, NULL);
pthread_create(&thread_E, NULL, look, NULL);
pthread_create(&thread_F, NULL, clook, NULL);
do
{
/*GETS THE FILE DETAILS AS USER INPUT*/
printf( "\n\nEnter the input file name : ");
scanf("%s", filename);
system("clear");
/*WAKES UP ALL CHILD THREADS WAITING FOR AN INPUT FROM THE PARENT THREAD*/
pthread_mutex_lock(&input_mutex);
/*CHECK IF USER HAS ENTERED QUIT*/
check = strcmp(filename,"QUIT");
hasFile = TRUE;
pthread_cond_broadcast(&got_filename);
hasFile = FALSE;
pthread_mutex_unlock(&input_mutex);
if(check != 0)
{
fptr = fopen(filename, "r");
if(fptr == NULL)
{
perror("Error opening the text file");
}else{
/*FINDS THE NUMBER OF TOTAL DATA PROVIDED IN THE .TXT FILE*/
countData(filename);
/*THERE SHOULD BE MORE AT LEAST ONE DISK REQUEST IN THE .TXT FILE FOR THE ALGORITHMS TO WORK WITH*/
if(count > 3)
{
buffer1 = malloc(count * sizeof(int));
i = 0;
/*TOKENIZE THE STRING AND MAKE AN ARRAY OF INTEGERS*/
if(fgets(str, 4096, fptr) != NULL)
{
token = strtok(str, " ");
while(token != NULL)
{
buffer1[i] = atoi(token);
token = strtok(NULL, " ");
i++;
}
}
/*WAKES UP ALL THREADS WAITING ON THIS CONDITION TO PROCEED*/
pthread_mutex_lock(&start_mutex);
bufferReady = TRUE;
pthread_cond_broadcast(&readbuffer1);
pthread_mutex_unlock(&start_mutex);
/*WAIT TILL BUFFER OF SIZE 1 IS FILLED WITH A SEEK TIME FROM ONE OF THE THREADS*/
pthread_mutex_lock(&writer_mutex);
/*WAKES UP A BLOCKED FCFS(THREAD A) TO WRITE TO THE BUFFER FIRST*/
pthread_mutex_lock(&fcfs_mutex);
fcfs_write = TRUE;
pthread_cond_signal(&fcfs_cond);
pthread_mutex_unlock(&fcfs_mutex);
for(i = 0; i < THREAD_COUNT; i++)
{
/*IF THERE IS AN UNREAD SEEK TIME INSIDE THE BUFFER THREADS ARE BLOCKED FROM WRITING TO THE BUFFER*/
if(emptyspaces == BUFFERSIZE)
{
pthread_cond_wait(&readbuffer2, &writer_mutex);
}
/*STORES EACH SEEK TIME INTO AN ARRAY OF INTEGERS*/
seektime[i] = buffer2[0];
emptyspaces = BUFFERSIZE;
/*WAKES UP ONE OF THE THREADS WAITING ON THE CONDITION*/
pthread_cond_signal(&empty);
}
pthread_mutex_unlock(&writer_mutex);
/*PRINTS OUT THE SEEKTIMES FROM THREAD A TO F*/
printf("\nFor %s :\n", filename);
printf("FCFS : %d\n", seektime[0]);
printf("SSTF : %d\n", seektime[1]);
printf("SCAN : %d\n", seektime[2]);
printf("CSCAN : %d\n", seektime[3]);
printf("LOOK : %d\n", seektime[4]);
printf("CLOOK : %d\n", seektime[5]);
/*THREADS ARE BLOCKED FROM READING BUFFER 1*/
bufferReady = FALSE; free(buffer1);
}else{
printf("Insufficient data provided. There should be more than one disk request in the .txt file");
}
/*CLOSE THE FILE AFTER READING DATA*/
fclose(fptr);
}
}
}while(check != 0);
/*ASKS ALL CHILD THREADS TO TERMINATE*/
pthread_mutex_lock(&end_mutex);
isEnd = TRUE;
pthread_cond_broadcast(&terminate);
pthread_mutex_unlock(&end_mutex);
/*PARENT THREAD WAITS FOR CHILD THREADS TO TERMINATE*/
pthread_join(thread_A,NULL);
pthread_join(thread_B,NULL);
pthread_join(thread_C,NULL);
pthread_join(thread_D,NULL);
pthread_join(thread_E,NULL);
pthread_join(thread_F,NULL);
/*DESTROYS MUTEXES AND CONDITIONAL VARIABLES*/
/*pthread_mutex_destroy();*/
/*PARENT THREAD TERMINATE*/
pthread_exit(NULL);
}
/*METHOD TO COUNT THE NO.OF DATA FIELDS IN THE .TXT FILE PROVIDED BY THE USER*/
void countData(char* filename)
{
FILE *fptr = NULL; char str[4096]; char* token;
/*OPEN FILE TO READ IN DATA*/
fptr = fopen(filename, "r");
if(fptr != NULL)
{
if(fgets(str, 4096, fptr) != NULL)
{
count = 0;
token = strtok(str," ");
while(token != NULL)
{
token = strtok(NULL," ");
count++;
}
}
}
/*CLOSE THE FILE AFTER READING DATA*/
fclose(fptr);
}
void *fcfs(void *arg)
{
int movements = 0; int difference = 0; int i = 0; int currPos = 0; int length = 0; int* array;
do
{
/*CHILD THREAD IS BLOCKED UNTIL CHILD IS ALLOWED TO READ INPUT FROM THE BUFFER*/
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
/*IF BUFFER 1 IS NOT READY FOR READING, CHILD THREAD IS BLOCKED*/
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
/*COUNT - 3 SINCE THE FIRST THREE DATA STORED IN THE BUFFER ARE NOT DISK REQUESTS*/
length = count - 3;
/*CREATING A MALLOC SIZED ARRAY*/
array = malloc((length * sizeof(int)));
currPos = buffer1[1];
/*READING DISK REQUESTS FROM BUFFER INTO AN ARRAY*/
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
/*CALCULATE FCFS SEEK TIME*/
for(i = 0; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
}
pthread_mutex_unlock(&start_mutex);
if(check != 0)
{
/*CHILD THREAD IS BLOCKED UNTIL PARENT THREAD SIGNALS ITS READY TO READ SEEK TIMES*/
pthread_mutex_lock(&fcfs_mutex);
if(fcfs_write == FALSE)
{
pthread_cond_wait(&fcfs_cond,&fcfs_mutex);
}
fcfs_write = FALSE;
pthread_mutex_unlock(&fcfs_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
/*CONDITION WILL NEVER BE REACHED BY THE FCFS THREAD AS IT WILL ALWAYS BE THE FIRST THREAD TO FILL OUT BUFFER 2 IN AN ITERATION*/
pthread_cond_wait(&empty,&writer_mutex);
}
/*WRITE SEEK TIME TO BUFFER 2*/
buffer2[0] = movements;
emptyspaces = 0;
/*WAKES UP PARENT THREAD TO READ THE WRITTEN SEEK TIME*/
pthread_cond_signal(&readbuffer2);
sstf_write = TRUE;
/*AFTER FCFS WRITES TO BUFFER 2, THEN SSTF (THREAD B) IS SIGNALED TO WRITE TO BUFFER 2*/
pthread_cond_signal(&sstf_cond);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0;
/*THREAD IS BLOCKED UNTIL CHILD THREAD GETS A NEW FILENAME FROM THE PARENT THREAD*/
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
/*CHILD THREAD WAITS FOR A SIGNAL FROM PARENT THREAD TO TERMINATE*/
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
/*CHILD THREAD TERMINATES*/
pthread_exit(NULL);
}
void *sstf(void *arg)
{
int movements = 0; int difference = 0; int stepNum = 0; int cylinderNum = 0;
int index = 0; int i = 0; int j = 0; int currPos = 0; int length = 0; int* array;
do
{
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
length = count - 3;
array = malloc((length * sizeof(int)));
cylinderNum = buffer1[0];
currPos = buffer1[1];
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
for(i = 0; i < length; i++)
{
/*DIFFERENCE BETWEEN TWO CYLINDER DISK INDICES CAN NEVER BE GREATER THAN THE NO.OF CYLINDER DISKS*/
difference = cylinderNum;
stepNum = 0;
for(j = 0; j < length; j++)
{
if(array[j] != VISITED)
{
stepNum = abs(array[j] - currPos);
if(stepNum < difference)
{
difference = stepNum;
index = j;
}
}
}
currPos = array[index];
array[index] = VISITED;
movements = movements + difference;
}
}
pthread_mutex_unlock(&start_mutex);
if(check !=0)
{
/*SSTF WAITS UNTIL FCFS FINISHED WRITING SEEKTIME INTO BUFFER 2*/
pthread_mutex_lock(&sstf_mutex);
if(sstf_write == FALSE)
{
pthread_cond_wait(&sstf_cond,&sstf_mutex);
}
sstf_write = FALSE;
pthread_mutex_unlock(&sstf_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
/*IF PARENT THREAD IS STILL READING FCFS SEEK TIME, BLOCK*/
pthread_cond_wait(&empty,&writer_mutex);
}
buffer2[0] = movements;
emptyspaces = 0;
pthread_cond_signal(&readbuffer2);
scan_write = TRUE;
/*WAKES UP SCAN (THREAD C) TO WRITE DATA INTO BUFFER 2*/
pthread_cond_signal(&scan_cond);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0; stepNum = 0; index = 0;
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
pthread_exit(NULL);
}
void *scan(void *arg)
{
int movements = 0; int difference = 0; int temp = 0; int cylinderNum = 0; int currPos = 0;
int prevPos = 0; int index = 0; int i = 0; int j = 0; int length = 0; int* array;
do
{
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
length = count - 3;
array = malloc((length * sizeof(int)));
cylinderNum = buffer1[0];
currPos = buffer1[1];
prevPos = buffer1[2];
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
for(i = 0; i < length; i++)
{
for(j = 0; j < length-i-1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for(i = 0; i < length; i++)
{
if(currPos < array[i])
{
index = i;
break;
}
}
/*HEAD MOVES TOWARDS SMALLER DISK NUMBERS*/
if(prevPos - currPos > 0)
{
for(i = index-1 ; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
difference = array[i+1];
movements = movements + difference;
currPos = 0;
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
/*HEAD MOVES TOWARDS LARGER DISK NUMBERS*/
}else{
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
movements = movements + abs(cylinderNum - array[i-1]-1);
currPos = cylinderNum - 1;
for(i = index - 1; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
}
}
pthread_mutex_unlock(&start_mutex);
if(check != 0)
{
/*SCAN WAITS UNTIL SSTF(THREAD B) FINISHES WRITING TO BUFFER 2*/
pthread_mutex_lock(&scan_mutex);
if(scan_write == FALSE)
{
pthread_cond_wait(&scan_cond,&scan_mutex);
}
scan_write = FALSE;
pthread_mutex_unlock(&scan_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
pthread_cond_wait(&empty,&writer_mutex);
}
buffer2[0] = movements;
emptyspaces = 0;
pthread_cond_signal(&readbuffer2);
cscan_write = TRUE;
/*WAKES UP CSCAN (THREAD D) TO WRITE TO BUFFER*/
pthread_cond_signal(&cscan_cond);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0; temp = 0; index = 0;
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
pthread_exit(NULL);
}
void *cscan(void *arg)
{
int movements = 0; int difference = 0; int temp = 0; int cylinderNum = 0; int currPos = 0;
int prevPos = 0; int index = 0; int i = 0; int j = 0; int length = 0; int* array;
do
{
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
length = count - 3;
array = malloc((length * sizeof(int)));
cylinderNum = buffer1[0];
currPos = buffer1[1];
prevPos = buffer1[2];
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
for(i = 0; i < length; i++)
{
for(j = 0; j < length-i-1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for(i = 0; i < length; i++)
{
if(currPos < array[i])
{
index = i;
break;
}
}
/*HEAD MOVES TOWRDS SMALLER DISK NUMBERS*/
if(prevPos - currPos > 0)
{
for(i = index-1 ; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
difference = array[i+1];
movements = movements + difference;
difference = cylinderNum - 1;
movements = movements + difference;
currPos = cylinderNum - 1;
for(i = length-1; i >= index; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
/*HEAD MOVES TOWARDS LARGER DISK NUMBERS*/
}else{
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
movements = movements + abs(cylinderNum - array[i-1]-1);
difference = cylinderNum - 1;
movements = movements + difference;
currPos = 0;
for(i = 0; i < index; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
}
}
pthread_mutex_unlock(&start_mutex);
if(check != 0)
{
/*CSCAN WAITS UNTIL SCAN(THREAD C) FINISHES WRITING TO BUFFER 2*/
pthread_mutex_lock(&cscan_mutex);
if(cscan_write == FALSE)
{
pthread_cond_wait(&cscan_cond,&cscan_mutex);
}
cscan_write = FALSE;
pthread_mutex_unlock(&cscan_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
pthread_cond_wait(&empty,&writer_mutex);
}
buffer2[0] = movements;
emptyspaces = 0;
pthread_cond_signal(&readbuffer2);
look_write = TRUE;
/*WAKES UP LOOK (THREAD E) TO WRITE TO BUFFER*/
pthread_cond_signal(&look_cond);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0; temp = 0;
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
pthread_exit(NULL);
}
void *look(void *arg)
{
int movements = 0; int difference = 0; int temp = 0; int currPos = 0;
int prevPos = 0; int index = 0; int i = 0; int j = 0; int length = 0; int* array;
do
{
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
length = count - 3;
array = malloc((length * sizeof(int)));
currPos = buffer1[1];
prevPos = buffer1[2];
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
for(i = 0; i < length; i++)
{
for(j = 0; j < length-i-1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for(i = 0; i < length; i++)
{
if(currPos < array[i])
{
index = i;
break;
}
}
/*HEAD MOVES TOWRDS SMALLER DISK NUMBERS*/
if(prevPos - currPos > 0)
{
for(i = index-1 ; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
/*HEAD MOVES TOWARDS LARGER DISK NUMBERS*/
}else{
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
for(i = index - 1; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
}
}
pthread_mutex_unlock(&start_mutex);
if(check != 0)
{
/*LOOK WAITS UNTIL CSCAN(THREAD D) FINISHES WRITING TO BUFFER 2*/
pthread_mutex_lock(&look_mutex);
if(look_write == FALSE)
{
pthread_cond_wait(&look_cond,&look_mutex);
}
look_write = FALSE;
pthread_mutex_unlock(&look_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
pthread_cond_wait(&empty,&writer_mutex);
}
buffer2[0] = movements;
emptyspaces = 0;
pthread_cond_signal(&readbuffer2);
/*WAKES UP CLOOK (THREAD E) TO WRITE TO BUFFER*/
clook_write = TRUE;
pthread_cond_signal(&clook_cond);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0; temp = 0;
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
pthread_exit(NULL);
}
void *clook(void *arg)
{
int movements = 0; int difference = 0; int temp = 0; int currPos = 0;
int prevPos = 0; int index = 0; int i = 0; int j = 0; int length = 0; int* array;
do
{
pthread_mutex_lock(&start_mutex);
if(check != 0)
{
if(bufferReady == FALSE)
{
pthread_cond_wait(&readbuffer1, &start_mutex);
}
length = count - 3;
array = malloc((length * sizeof(int)));
currPos = buffer1[1];
prevPos = buffer1[2];
for(i = 0; i < length; i++)
{
array[i] = buffer1[i+3];
}
for(i = 0; i < length; i++)
{
for(j = 0; j < length-i-1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for(i = 0; i < length; i++)
{
if(currPos < array[i])
{
index = i;
break;
}
}
/*HEAD MOVES TOWRDS SMALLER DISK NUMBERS*/
if(prevPos - currPos > 0)
{
for(i = index-1 ; i >= 0; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
for(i = length-1; i >= index; i--)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
/*HEAD MOVES TOWARDS LARGER DISK NUMBERS*/
}else{
for(i = index; i < length; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
for(i = 0; i < index; i++)
{
difference = abs(array[i] - currPos);
movements = movements + difference;
currPos = array[i];
}
}
}
pthread_mutex_unlock(&start_mutex);
if(check != 0)
{
/*CLOOK WAITS UNTIL LOOK(THREAD E) FINISHES WRITING TO BUFFER 2*/
pthread_mutex_lock(&clook_mutex);
if(clook_write == FALSE)
{
pthread_cond_wait(&clook_cond,&clook_mutex);
}
clook_write = FALSE;
pthread_mutex_unlock(&clook_mutex);
pthread_mutex_lock(&writer_mutex);
if(emptyspaces == 0)
{
pthread_cond_wait(&empty,&writer_mutex);
}
buffer2[0] = movements;
emptyspaces = 0;
pthread_cond_signal(&readbuffer2);
pthread_mutex_unlock(&writer_mutex);
}
free(array); movements = 0; difference = 0; temp = 0;
pthread_mutex_lock(&input_mutex);
if(hasFile == FALSE)
{
pthread_cond_wait(&got_filename, &input_mutex);
}
pthread_mutex_unlock(&input_mutex);
}while(check != 0);
pthread_mutex_lock(&end_mutex);
if(isEnd == FALSE)
{
pthread_cond_wait(&terminate, &end_mutex);
}
printf("%ld has terminated\n",pthread_self());
pthread_mutex_unlock(&end_mutex);
pthread_exit(NULL);
}