-
Notifications
You must be signed in to change notification settings - Fork 17
/
disk_scheduling_algorithms.c
169 lines (147 loc) · 3.85 KB
/
disk_scheduling_algorithms.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
#include <stdio.h> //for printf and scanf
#include <stdlib.h> //for malloc
#define LOW 0
#define HIGH 199
#define START 53
//compare function for qsort
//you might have to sort the request array
//use the qsort function
// an argument to qsort function is a function that compares 2 quantities
//use this there.
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
//function to swap 2 integers
void swap(int *a, int *b)
{
if (*a != *b)
{
*a = (*a ^ *b);
*b = (*a ^ *b);
*a = (*a ^ *b);
return;
}
}
//Prints the sequence and the performance metric
void printSeqNPerformance(int *request, int numRequest)
{
int i, last, acc = 0;
last = START;
printf("\n");
printf("%d", START);
for (i = 0; i < numRequest; i++)
{
printf(" -> %d", request[i]);
acc += abs(last - request[i]);
last = request[i];
}
printf("\nPerformance : %d\n", acc);
return;
}
//access the disk location in FCFS
void accessFCFS(int *request, int numRequest)
{
//simplest part of assignment
printf("\n----------------\n");
printf("FCFS :");
printSeqNPerformance(request, numRequest);
printf("----------------\n");
return;
}
//access the disk location in SSTF
void accessSSTF(int *request, int numRequest)
{
//write your logic here
printf("\n----------------\n");
printf("SSTF :");
printSeqNPerformance(request, numRequest);
printf("----------------\n");
return;
}
//access the disk location in SCAN
void accessSCAN(int *request, int numRequest)
{
//write your logic here
printf("\n----------------\n");
printf("SCAN :");
printSeqNPerformance(newRequest, newCnt);
printf("----------------\n");
return;
}
//access the disk location in CSCAN
void accessCSCAN(int *request, int numRequest)
{
//write your logic here
printf("\n----------------\n");
printf("CSCAN :");
printSeqNPerformance(newRequest, newCnt);
printf("----------------\n");
return;
}
//access the disk location in LOOK
void accessLOOK(int *request, int numRequest)
{
//write your logic here
printf("\n----------------\n");
printf("LOOK :");
printSeqNPerformance(newRequest, newCnt);
printf("----------------\n");
return;
}
//access the disk location in CLOOK
void accessCLOOK(int *request, int numRequest)
{
//write your logic here
printf("\n----------------\n");
printf("CLOOK :");
printSeqNPerformance(newRequest,newCnt);
printf("----------------\n");
return;
}
int main()
{
int *request, numRequest, i,ans;
//allocate memory to store requests
printf("Enter the number of disk access requests : ");
scanf("%d", &numRequest);
request = malloc(numRequest * sizeof(int));
printf("Enter the requests ranging between %d and %d\n", LOW, HIGH);
for (i = 0; i < numRequest; i++)
{
scanf("%d", &request[i]);
}
printf("\nSelect the policy : \n");
printf("----------------\n");
printf("1\t FCFS\n");
printf("2\t SSTF\n");
printf("3\t SCAN\n");
printf("4\t CSCAN\n");
printf("5\t LOOK\n");
printf("6\t CLOOK\n");
printf("----------------\n");
scanf("%d",&ans);
switch (ans)
{
//access the disk location in FCFS
case 1: accessFCFS(request, numRequest);
break;
//access the disk location in SSTF
case 2: accessSSTF(request, numRequest);
break;
//access the disk location in SCAN
case 3: accessSCAN(request, numRequest);
break;
//access the disk location in CSCAN
case 4: accessCSCAN(request,numRequest);
break;
//access the disk location in LOOK
case 5: accessLOOK(request,numRequest);
break;
//access the disk location in CLOOK
case 6: accessCLOOK(request,numRequest);
break;
default:
break;
}
return 0;
}