forked from charlesukpai/Operating-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCAN.java
115 lines (105 loc) · 1.57 KB
/
SCAN.java
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
import java.util.*;
import java.io.*;
class SCAN
{
public static void main(String args[])
{
int i,n,c,l,m,h,ph,stime=0,t=0,diff,min,v,lcount=0,hcount=0,max,k=1;
int a[]=new int[100];
int f[]=new int[100];
for(i=0;i<100;i++)
f[i]=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter no of disk accesses");
n=s.nextInt();
System.out.println("Enter no of cylinders");
c=s.nextInt();
System.out.println("Enter head position");
h=s.nextInt();
System.out.println("Enter previous head position");
ph=s.nextInt();
diff=h-ph;
l=h;
System.out.println("Enter accesses");
for(i=0;i<n;i++)
{
a[i]=s.nextInt();
if(a[i]<h)
lcount++;
else
hcount++;
}
min=1000;max=0;
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
if(diff<0)
stime=min;
if(diff>0)
stime=c-1-max;
System.out.println("Access Sequence is as follows:");
for(v=0;v<n;v++)
{
min=1000;
if(diff>0)
min=-1000;
for(i=0;i<n;i++)
{
if(diff<0)
{
if(f[i]==0)
{
if(l-a[i]>0 && l-a[i]<min)
{
min=l-a[i];
t=i;
}
}
}
if(diff>0)
{
if(f[i]==0)
{
if(l-a[i]<0 && l-a[i]>min)
{
min=l-a[i];
t=i;
}
}
}
}
f[t]=1;
if(l>a[t])
stime=stime+l-a[t];
else
stime=stime+a[t]-l;
System.out.print(a[t]+" ");
l=a[t];
if(diff>0)
hcount--;
if(diff<0)
lcount--;
if(diff<0 && lcount ==0)
l=0;
if(lcount==0)
{
diff=1;
if(hcount>0 && k==1){
System.out.print("0 ");k=0;}
}
if(diff>0 && hcount ==0)
l=c-1;
if(hcount==0)
{
diff=-1;
if(lcount>0 && k==1){
System.out.print((c-1)+" ");k=0;}
}
}
System.out.println("\nTotal seek time is:"+stime);
}
}