1// Aim: Program to perform Disk Scheduling operations (Disk Scheduling).
2#include<stdio.h>
3#include<stdlib.h>
4
5void fcfs(int request[], int n, int head) {
6 int seek_count = 0;
7 int distance, cur_track;
8 for(int i = 0; i < n; i++) {
9 cur_track = request[i];
10 distance = abs(cur_track - head);
11 seek_count += distance;
12 head = cur_track;
13 }
14 printf("fcfs seek count: %d\n", seek_count);
15}
16
17void sstf(int request[], int n, int head) {
18 int seek_count = 0;
19 int distance, cur_track;
20 int completed[n];
21 for(int i = 0; i < n; i++) {
22 completed[i] = 0;
23 }
24 for(int i = 0; i < n; i++) {
25 int min_distance = 10000, index = -1;
26 for(int j = 0; j < n; j++) {
27 if(completed[j] == 0) {
28 distance = abs(request[j] - head);
29 if(distance < min_distance) {
30 min_distance = distance;
31 index = j;
32 }
33 }
34 }
35 completed[index] = 1;
36 cur_track = request[index];
37 seek_count += min_distance;
38 head = cur_track;
39 }
40 printf("sstf seek count: %d\n", seek_count);
41}
42
43void scan(int request[], int n, int head , int disk_size) {
44 int seek_count = 0;
45 int distance, cur_track;
46 int sorted_request[n];
47 for(int i = 0; i < n; i++) {
48 sorted_request[i] = request[i];
49 }
50 for(int i = 0; i < n - 1; i++) {
51 for(int j = 0; j < n - i - 1; j++) {
52 if(sorted_request[j] > sorted_request[j + 1]) {
53 int temp = sorted_request[j];
54 sorted_request[j] = sorted_request[j + 1];
55 sorted_request[j + 1] = temp;
56 }
57 }
58 }
59 int index;
60 for(index = 0; index < n; index++) {
61 if(sorted_request[index] >= head) {
62 break;
63 }
64 }
65 for(int i = index; i < n; i++) {
66 cur_track = sorted_request[i];
67 distance = abs(cur_track - head);
68 seek_count += distance;
69 head = cur_track;
70 }
71 seek_count += abs(disk_size - 1 - head);
72 head = disk_size - 1;
73 for(int i = index - 1; i >= 0; i--) {
74 cur_track = sorted_request[i];
75 distance = abs(cur_track - head);
76 seek_count += distance;
77 head = cur_track;
78 }
79 printf("scan seek count: %d\n", seek_count);
80}
81
82void cscan(int request[], int n, int head , int disk_size) {
83 int seek_count = 0;
84 int distance, cur_track;
85 int sorted_request[n];
86 for(int i = 0; i < n; i++) {
87 sorted_request[i] = request[i];
88 }
89 for(int i = 0; i < n - 1; i++) {
90 for(int j = 0; j < n - i - 1; j++) {
91 if(sorted_request[j] > sorted_request[j + 1]) {
92 int temp = sorted_request[j];
93 sorted_request[j] = sorted_request[j + 1];
94 sorted_request[j + 1] = temp;
95 }
96 }
97 }
98 int index;
99 for(index = 0; index < n; index++) {
100 if(sorted_request[index] > head) {
101 break;
102 }
103 }
104 for(int i = index; i < n; i++) {
105 cur_track = sorted_request[i];
106 distance = abs(cur_track - head);
107 seek_count += distance;
108 head = cur_track;
109 }
110 seek_count += abs(disk_size - 1 - head);
111 head = 0;
112 seek_count += abs(disk_size - 1);
113 for(int i = 0; i < index; i++) {
114 cur_track = sorted_request[i];
115 distance = abs(cur_track - head);
116 seek_count += distance;
117 head = cur_track;
118 }
119 printf("cscan seek count: %d\n", seek_count);
120}
121
122void look(int request[], int n, int head) {
123 int seek_count = 0;
124 int distance, cur_track;
125 int sorted_request[n];
126 for(int i = 0; i < n; i++) {
127 sorted_request[i] = request[i];
128 }
129 for(int i = 0; i < n - 1; i++) {
130 for(int j = 0; j < n - i - 1; j++) {
131 if(sorted_request[j] > sorted_request[j + 1]) {
132 int temp = sorted_request[j];
133 sorted_request[j] = sorted_request[j + 1];
134 sorted_request[j + 1] = temp;
135 }
136 }
137 }
138 int index;
139 for(index = 0; index < n; index++) {
140 if(sorted_request[index] > head) {
141 break;
142 }
143 }
144 for(int i = index; i < n; i++) {
145 cur_track = sorted_request[i];
146 distance = abs(cur_track - head);
147 seek_count += distance;
148 head = cur_track;
149 }
150 for(int i = index - 1; i >= 0; i--) {
151 cur_track = sorted_request[i];
152 distance = abs(cur_track - head);
153 seek_count += distance;
154 head = cur_track;
155 }
156 printf("look seek count: %d\n", seek_count);
157}
158
159void clook(int request[], int n, int head) {
160 int seek_count = 0;
161 int distance, cur_track;
162 int sorted_request[n];
163 for(int i = 0; i < n; i++) {
164 sorted_request[i] = request[i];
165 }
166 for(int i = 0; i < n - 1; i++) {
167 for(int j = 0; j < n - i - 1; j++) {
168 if(sorted_request[j] > sorted_request[j + 1]) {
169 int temp = sorted_request[j];
170 sorted_request[j] = sorted_request[j + 1];
171 sorted_request[j + 1] = temp;
172 }
173 }
174 }
175 int index;
176 for(index = 0; index < n; index++) {
177 if(sorted_request[index] > head) {
178 break;
179 }
180 }
181 for(int i = index; i < n; i++) {
182 cur_track = sorted_request[i];
183 distance = abs(cur_track - head);
184 seek_count += distance;
185 head = cur_track;
186 }
187 head = sorted_request[0];
188 seek_count += abs(sorted_request[n - 1] - head);
189 for(int i = 0; i < index; i++) {
190 cur_track = sorted_request[i];
191 distance = abs(cur_track - head);
192 seek_count += distance;
193 head = cur_track;
194 }
195 printf("clook seek count: %d\n", seek_count);
196}
197
198int main(){
199 int n , head, disk_size;
200 printf("Enter number of requests: ");
201 scanf("%d", &n);
202 int request[n];
203 printf("Enter requests: ");
204 for(int i = 0; i < n; i++) {
205 scanf("%d", &request[i]);
206 }
207 printf("Enter initial head position: ");
208 scanf("%d", &head);
209 printf("Enter disk size: ");
210 scanf("%d", &disk_size);
211 fcfs(request, n, head);
212 sstf(request, n, head);
213 scan(request, n, head, disk_size);
214 cscan(request, n, head, disk_size);
215 look(request, n, head);
216 clook(request, n, head);
217 return 0;
218}