-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskschedule.c
434 lines (360 loc) · 9.88 KB
/
diskschedule.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
//
// main.c
// os_project4
//
// Created by Bayram Muradov on 25/12/2018.
// Copyright © 2018 Bayram Muradov. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/shm.h>
//global variables
int cylinders[5000];
int headpos;
int fd_output;
//scheduling algorithms
void start_fcfs(int in_count) {
int mv_count=0;
int difference;
int fcfs_queue [5010];
fcfs_queue[0]=headpos;
for(int i=1; i<=in_count; i++) {
fcfs_queue[i]=cylinders[i];
}
for(int i=0;i<in_count; i++)
{
difference=abs(fcfs_queue[i+1]-fcfs_queue[i]);
mv_count+=difference;
}
//output resls
printf("%s %d \n", "SSTF:", mv_count);
}
void start_sstf(int in_count) {
int sstf_dist[5000]; //array keeps distances from headpos
int sstf_elem[5000]; //array keeps sorted dataset
//distance from head to each element
for(int i=0; i<in_count; i++){
sstf_dist[i] = abs(headpos-cylinders[i]);
}
for(int i=0; i<in_count; i++){
sstf_elem[i] = cylinders[i];
}
//sort the dataset elements according to their distance
//fill the result to local array
int temp;
for(int i=0; i<in_count; i++){
for(int j=i+1; j<in_count;j++){
if(sstf_dist[i]>sstf_elem[j]){
temp = sstf_dist[i];
sstf_dist[i]=sstf_elem[j];
sstf_dist[j]=temp;
temp=sstf_elem[i];
sstf_elem[i]=sstf_elem[j];
sstf_elem[j]=temp;
}
}
}
//calculate movecounts
int mov_count=0;
int sstf_head=headpos;
for(int i=1; i<in_count; i++){
mov_count = mov_count+abs(sstf_head-sstf_elem[i]);
sstf_head = sstf_elem[i];
}
//output resls
printf("%s %d \n", "SSTF:", mov_count);
}
void start_scan(int in_count) {
int queue [5010];
int head, max, q_size, temp, sum;
int dloc; //location of disk (head) arr
q_size=in_count;
head=headpos;
//fill elements to local queue
for(int i=0; i<q_size; i++){
queue[i]=cylinders[i];
}
queue[q_size] = head; //add RW head into queue
q_size++;
//sort the array
for(int i=0; i<q_size;i++){
for(int j=i; j<q_size; j++){
if(queue[i]>queue[j]){
temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
max = queue[q_size-1];
//locate head in the queue
for(int i=0; i<q_size; i++){
if(head == queue[i]){
dloc = i;
break;
}
}
sum = head + max;
printf("\nmovement of total cylinders %d", sum);
}
void start_cscan(int in_count) {
int LOW=0;
int HIGH=5000;
int queue[5010], q_size, head, i,j, seek=0, diff, temp, queue1[5010], queue2[5010], temp1=0, temp2=0;
float avg;
int datas[5010];
q_size=in_count;
head=headpos;
for(int k=0; k<in_count;k++) {
datas[k]=cylinders[k];
}
for(i=0; i<q_size; i++) {
temp=datas[i];
if(temp >= head){
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
}
}
//sort both arrays
for(i=0; i<temp1-1; i++){
for(j=i+1; j<temp1; j++){
if(queue1[i] > queue1[j]){
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
for(i=0; i<temp2-1; i++){
for(j=i+1; j<temp2; j++){
if(queue2[i]>queue2[j]){
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
//calculate closest edge
if(abs(head-LOW) >= abs(head-HIGH)){
for(i=1,j=0; j<temp1; i++,j++){
queue[i] = queue1[j];
}
queue[i] = HIGH;
queue[i+1] = 0;
for(i=temp1+3, j=0; j<temp2; i++, j++){
queue[i] = queue2[j];
}
} else {
for(i=1,j=temp2-1; j>=0; i++,j--){
queue[i] = queue2[j];
}
queue[i] = LOW;
queue[i+1] = HIGH;
for(i=temp2+3, j=temp1-1; j>=0; i++, j--){
queue[i] = queue1[j];
}
}
queue[0] = head;
for(j=0; j<=q_size+1; j++){
diff=abs(queue[j+1] - queue[j]);
seek += diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
//seek = seek - max; //subtract seek time back to zero
printf("Total seek time is %d\n", seek);
avg = seek/(float)q_size;
printf("Average seek time is %f\n", avg);
}
void start_look(int in_count) {
int LOW=0;
int HIGH=5000;
int queue[5010], head, q_size, i,j, seek=0, temp, queue1[5010], queue2[5010], temp1=0, temp2=0;
float avg;
q_size=in_count;
head=headpos;
int datas[5010];
for(int i=0; i<in_count; i++) {
datas[i]=cylinders[i];
}
for(i=0; i<q_size; i++){
temp=datas[i];
//queue1 - elems greater than head
if(temp >= head){
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
}
}
//sort queue1 - increasing order
for(i=0; i<temp1-1; i++){
for(j=i+1; j<temp1; j++){
if(queue1[i] > queue1[j]){
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
//sort queue2 - decreasing order
for(i=0; i<temp2-1; i++){
for(j=i+1; j<temp2; j++){
if(queue2[i] < queue2[j]){
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
if(abs(head-LOW) >= abs(head-HIGH)){
for(i=1,j=0; j<temp1; i++,j++){
queue[i] = queue1[j];
}
for(i=temp1+1, j=0; j<temp2; i++, j++){
queue[i] = queue2[j];
}
} else {
for(i=1,j=0; j<temp2; i++,j++){
queue[i] = queue2[j];
}
for(i=temp2+1, j=0; j<temp1; i++, j++){
queue[i] = queue1[j];
}
}
queue[0] = head;
printf("Total seek time is %d\n", seek);
avg = seek/(float)q_size;
printf("Average seek time is %f\n", avg);
}
void start_clook(int in_count) {
int LOW=0;
int HIGH=5000;
int queue[5010], head, q_size, i,j, seek=0, temp, queue1[5010], queue2[5010], temp1=0, temp2=0;
int datas[5010];
q_size=in_count;
head=headpos;
for(int i=0; i<in_count; i++) {
datas[i]=cylinders[i];
}
for(i=0; i<q_size; i++){
temp=datas[i];
//queue1 - elems greater than head
if(temp >= head){
queue1[temp1] = temp;
temp1++;
} else {
queue2[temp2] = temp;
temp2++;
}
}
//sort queue1 - increasing order
for(i=0; i<temp1-1; i++){
for(j=i+1; j<temp1; j++){
if(queue1[i] > queue1[j]){
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
//sort queue2
for(i=0; i<temp2-1; i++){
for(j=i+1; j<temp2; j++){
if(queue2[i] > queue2[j]){
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
if(abs(head-LOW) <= abs(head-HIGH)){
for(i=1,j=temp2-1; j>=0; i++,j--){
queue[i] = queue2[j];
}
queue[i] = LOW;
queue[i+1] = HIGH;
for(i=temp2+3,j=temp1-1; j>=0; i++,j--){
queue[i] = queue1[j];
}
} else {
for(i=1,j=0; j<temp1; i++,j++){
queue[i] = queue1[j];
}
queue[i] = HIGH;
queue[i+1] = LOW;
for(i=temp1+3,j=0; j<temp2; i++,j++){
queue[i] = queue2[j];
}
}
queue[0] = head;
//range = max - min;
//printf("Range is %d", range);
//seek = seek - (max - min);
printf("CLOOK: %d\n", seek);
}
//generates output from specified input set
void generateFromDataset(int inp_count) {
int in_count=inp_count;
start_fcfs(in_count);
start_sstf(in_count);
start_scan(in_count);
start_cscan(in_count);
start_look(in_count);
start_clook(in_count);
}
//generates output randomly
void generateRandomly() {
//fill cylinders array randomly
}
int createFile(char* filename) {
int fd_results;
fd_results=open(filename, O_CREAT | O_RDWR, 0600);
return fd_results;
}
//main method
int main(int argc, const char * argv[]) {
if(argc<1) {
printf("# of args should be @least 1\n");
return 0;
}
char*outname="initialhead";
strcat(outname, iota(argv[1]));
fd_output=createFile(outname);
//terminal args.
char* FILENAME=NULL;
//assign values
headpos = atoi(argv[1]);
if(argc==1) {
generateRandomly();
}
else {
FILENAME = argv[2];
//read files and fill global cylinders array
FILE *fp;
fp=fopen(FILENAME, "r");
if(fp==NULL) {
printf("error while opening file\n");
exit(1);
}
char* temp_buff[100];
int tracker_inputno=0;
int temp;
while(fscanf(fp, "%s", &temp_buff)!= EOF) {
temp=atoi(temp_buff);
cylinders[tracker_inputno]=temp;
tracker_inputno++;
}
fclose(fp);
generateFromDataset(tracker_inputno);
}
}