-
Notifications
You must be signed in to change notification settings - Fork 0
/
12c.c
76 lines (60 loc) · 1.5 KB
/
12c.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
// Disk Scheduling Algorithms
// Circular Scan
// by Simply
#include <stdio.h>
void cscan(int req[], int n, int head, int max)
{
int seek = 0, c = 0, temp, i, j, seq[20];
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (req[j] > req[j + 1])
{
temp = req[j];
req[j] = req[j + 1];
req[j + 1] = temp;
}
}
}
j = 0;
while (req[j] <= head)
j++;
for (i = j; i < n; i++) {
seek += req[i] - head;
head = req[i];
seq[c] = req[i];
c++;
}
seek += (max - 1) - req[n - 1];
head = max - 1;
for (i = 0; i < j; i++) {
seek += head - req[i];
head = req[i];
seq[c] = req[i];
c++;
}
printf("TIME TO SEEK = %d\n", seek);
printf("SEEK SEQUENCE: ");
for (i = 0; i < c; i++)
printf("%d->", seq[i]);
}
void main()
{
int head, req[20], n, i, max, flag = 0;
printf("NUMBER OF CYLINDERS: ");
scanf("%d", &max);
printf("NUMBER OF REQUESTS: ");
scanf("%d", &n);
printf("ENTER REQUESTS: ");
for (i = 0; i < n; i++)
scanf("%d", &req[i]);
printf("POSITION OF READER / WRITER: ");
scanf("%d", &head);
if (req[0] > max - 1)
flag = 1;
if (flag == 1)
printf("CANNOT BE COMPLETED");
else
cscan(req, n, head, max);
}