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
| public class sstfAlg{
int num;
int[][] position;
int size;
int initPos;
int[] sequenceOfProcess ;//存储访问序列
int[] sequenceOfNumber;
Scanner sc = new Scanner(System.in);
public sstfAlg(int a,int b,int c){
//a means the amount of process
//b means the inital of position
//c means the size of disk
num = a;
position = new int[a][2];
sequenceOfProcess = new int[a];
sequenceOfNumber = new int[a];
initPos = b;
size = c;
}
public void input(){
System.out.println("input the number of process:");
for(int i=0;i<num;i++){
position[0] = sc.nextInt();
position[1] = i+1;
}
}
public void myAlg(){
int nearest = 10000;
int index = 0 ;
int initPos1 = initPos;
//复制一个数组用来寻找访问序列
int[][] position1 = new int[num][2];
for(int i=0 ;i<num;i++){
position1[0] = position[0];
position1[1] = i+1;
}
//寻找磁头访问的序列
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
if(position1[j][0]!=-1){
if(Math.abs(initPos1 - nearest)>Math.abs(position1[j][0]-initPos1)){
nearest = position1[j][0];
index = j;
}
}
}
sequenceOfProcess = nearest;
sequenceOfNumber = index+1;
position1[index][0] = -1;//-1表示此位置的进程已经放在了访问序列,不在进行查询
initPos1 = nearest;
nearest = 10000;
}
for(int i=0;i<num;i++){
System.out.println("进程"+sequenceOfNumber+"在磁道"+sequenceOfProcess+"完成");
}
}
|