????????
import java.util.Arrays;
import java.util.Scanner;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
public class DiskSchedulingAlgorithm {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
System.out.println("请输入磁盘柱头起始位置:");
int start = in.nextInt();
System.out.println("输入请求队列的个数:");
int num = in.nextInt();
int[] S = new int[num];
System.out.println("请输入各请求的磁道位置:");
for(int i = 0;i<num;i++){
S[i] = in.nextInt();
}
boolean T = true;
int C;
int choose ;
while(T==true){
System.out.println("请选择对应的磁盘调度:"+" "+"1.FCFS 2.SSTF 3.SCAN");
choose =in.nextInt();
switch(choose){
case 1:
FCFS(S,start,num);
break;
case 2:
SSTF(S,start,num);
break;
case 3:
SCAN(S,start,num);
break;
}
System.out.println(" ");
System.out.println("是否继续: 1/2");
C = in.nextInt();
T = false;
if(C==1){
T = true;
}
else{
System.out.printf("您已退出!");
}
}
}
public static void FCFS(int S[],int start,int num){
int result = 0;
System.out.print("采用FCFS算法调度时,磁头移动顺序为:");
System.out.print(start);
for(int i = 0;i<num-1;i++){
System.out.print("->"+S[i]);
result += Math.abs(S[i+1]-S[i]);
}
double avarage = 1.0*result/num;
System.out.println(" ");
System.out.print("磁头移动总量为:"+result+" "+"磁盘的平均移动量为:"+String.format("%.2f",avarage));
}
public static void SSTF(int S[],int start,int num){
int[] T = new int[num];
for(int i = 0;i<num;i++){
T[i]=S[i];
}
int sum = 0;
int key=num;
System.out.print("采用SSTF算法调度时,磁头移动顺序为:");
System.out.print(start);
while(num>=1){
int[] DISTANCE = new int[num];
for(int i = 0;i<num;i++){
DISTANCE[i] = Math.abs(start-T[i]); //存入各磁道和磁头的距离
}
int min = DISTANCE[0];
int corners= 0;
for(int i = 1;i<num;i++){
if(min>DISTANCE[i]){
min = DISTANCE[i];
corners = i;
}
}
System.out.print("->"+S[corners]);
start = T[corners]; //输入距离最短的值
sum +=DISTANCE[corners];
num --;
int k=0;
for(int i = 0;i<=num;i++){
if(T[i]!=T[corners]){
T[k] = T[i];
k++;
}
}
}
System.out.println(" ");
System.out.println("磁头移动量为:"+sum+" "+"平均磁头移动量为:"+(double)(sum*1.0/key));
}
public static void SCAN(int S[],int start,int num){
Arrays.sort(S);
int key =num;
System.out.println("请选择第一个要访问的磁道号:");
int First = in.nextInt();
int corners = 0;
System.out.print(start);
int sum = Math.abs(start-First);
for(int i = 0;i<num;i++){
if(S[i]==First){
corners = i;
}
}
if(start>First){
for(int i = corners;i>=0;i--){
System.out.print("->"+S[i]);
}
for(int i = corners+1;i<num;i++){
System.out.print("->"+S[i]);
}
for(int i = corners;i>0;i--){
sum+=Math.abs(S[i]-S[i-1]);
}
sum +=Math.abs(S[0]-S[corners+1]);
for(int i = corners+2;i<num;i++){
sum+=Math.abs(S[i]-S[i-1]);
}
double average = sum*1.0/key;
System.out.println(" ");
System.out.println("采用SCAN算法调度时,磁头移动顺序为:");
System.out.println("磁头移动量为:"+sum+" "+"平均磁头移动量为:"+String.format("%.2f", average));
}
else{
for(int i = corners;i<num;i++){
System.out.print("->"+S[i]);
}
for(int i = corners-1;i>=0;i--){
System.out.print("->"+S[i]);
}
for(int i = corners+1;i<num;i++){
sum+=Math.abs(S[i]-S[i-1]);
}
sum +=Math.abs(S[num-1]-S[corners-1]);
for(int i = corners-1;i>0;i--){
sum+=Math.abs(S[i]-S[i-1]);
}
double average = sum*1.0/key;
System.out.println(" ");
System.out.println("采用SCAN算法调度时,磁头移动顺序为:");
System.out.println("磁头移动量为:"+sum+" "+"平均磁头移动量为:"+String.format("%.2f", average));
}
}
}
运行效果:
?
|