-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_new.c
158 lines (150 loc) · 3.2 KB
/
disk_new.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
#include<stdio.h>
#include<stdlib.h>
int headpos;
int disk[100];
int n;
int curpos;
int total_head=0;
int cdisk[100];
int abss(int x)
{
if(x>=0)
return x;
else
return -x;
}
void swapp(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void sortt(int arr[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swapp(&cdisk[j],&cdisk[j+1]);
}
}
}
}
void copy()
{
for(int i=0;i<n;i++)
{
cdisk[i]=disk[i];
}
}
int main()
{
printf("Enter the head position\n");
scanf("%d",&headpos);
printf("Enter number of requests\n");
scanf("%d",&n);
printf("Enter the disk requests\n");
for(int i=0;i<n;i++)
{
scanf("%d",&disk[i]);
}
printf("\n__________________________________________________\n");
printf("FCFS\n");
curpos=headpos;
for(int i=0;i<n;i++)
{
printf("Head moves from cyclinder position %d to %d\n",curpos,disk[i]);
total_head+=abss(curpos-disk[i]);
curpos=disk[i];
}
printf("Total head movement: %d\n",total_head);
printf("\n__________________________________________________\n");
printf("SSTF\n");
curpos=headpos;
int nextpos;
copy();
total_head=0;
for(int i=0;i<n;i++)
{
nextpos=-1;
int d=9999;
for(int j=0;j<n;j++)
{
if(cdisk[j]!=-1 && abss(cdisk[j]-curpos)<d)
{
d=abss(cdisk[j]-curpos);
nextpos=j;
}
}
printf("Disk head moves from cylinder position %d to %d\n",curpos,cdisk[nextpos]);
total_head+=abss(curpos-cdisk[nextpos]);
curpos=cdisk[nextpos];
cdisk[nextpos]=-1;
}
printf("Total head movement: %d\n",total_head);
printf("\n__________________________________________________\n");
printf("SCAN\n");
copy();
cdisk[n]=0;
cdisk[n+1]=200;
sortt(cdisk,n+2);
curpos=headpos;
int a=0;
for(int i=0;cdisk[i]<=curpos;i++)
{
a++;
}
a--;
total_head=0;
for(int i=a;i>=0;i--)
{
printf("Disk head moves from cylinder position %d to %d\n",curpos,cdisk[i]);
total_head+=abss(curpos-cdisk[i]);
curpos=cdisk[i];
cdisk[i]=-1;
}
for(int i=0;i<n+2;i++)
{
if(cdisk[i]!=-1)
{
printf("Disk head moves from cylinder position %d to %d\n",curpos,cdisk[i]);
total_head+=abss(curpos-cdisk[i]);
curpos=cdisk[i];
cdisk[i]=-1;
}
}
printf("Total head movement: %d\n",total_head);
printf("\n__________________________________________________\n");
printf("C-SCAN\n");
copy();
sortt(cdisk,n);
a=0;
total_head=0;
curpos=headpos;
for(int i=0;cdisk[i]<=curpos;i++)
{
a++;
}
a--;
for(int i=a;i>=0;i--)
{
printf("Disk head moves from cylinder position %d to %d\n",curpos,cdisk[i]);
total_head+=abss(curpos-cdisk[i]);
curpos=cdisk[i];
cdisk[i]=-1;
}
for(int i=0;i<n;i++)
{
if(cdisk[i]!=-1)
{
printf("Disk head moves from cylinder position %d to %d\n",curpos,cdisk[i]);
total_head+=abss(curpos-cdisk[i]);
curpos=cdisk[i];
cdisk[i]=-1;
}
}
printf("Total head movement: %d",total_head);
return 0;
}