Javafx 课程设计之操作系统磁盘调度算法比较
一、课程设计目的
通过磁盘调度算法的模拟设计,了解磁盘调度的特点。
二、课程设计内容
模拟实现 FCFS、SSTF、电梯 LOOK、C-SCAN 算法,并计算及比较磁头移动道数。
三、要求及提示
本题目必须单人完成。
1、首先假设磁盘磁道数为 1500,磁头初始位置可任意设置。
2、用随机数生成函数产生“磁道号”序列(即磁盘请求的位置),共产生 400 个。其中
50%位于 0~499,25%分布在 500~999,25%分布在 1000~1499。具体的产生方法可参考
“题目一 页面置换算法的模拟实现及命中率对比”。
3、计算及比较每种磁盘调度算法下的磁头移动道数。
注:本题目要求给出图形可视化界面,并且能够动态模拟每个算法的调度过程,可采用
从上一个请求到下一个请求进行连线的方式。
运行环境:IDEA
JKD版本: 15
语言:javafx
注:MAIN类没有给出完整代码,在四个算法各自的磁道调度如何计算部分已删除(下面已标注删除位置),仅供参考,避免直接套用
课设总结:
- 可以改变磁针运行的方向,代码内容已囊括,但没有写出接口让使用者选择方向
- 在输入框输入的速率后,不能随意调节速率,只可以按它的整数倍调节,查了挺多资料暂时没有找到
- FCFS,SSTF,LOOK,C-SCAN算法的编写,在此处应该时准确,但未验证在题干之外所任意取数是否还保持准确性
- 界面单一,不美观。
- jj,zuipiaoliang
运行界面
查看结果
MIAN类
package com.codewithmosh;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class Main extends Application {
int t = 0;//X轴坐标
int count = 0;//第几个磁道
int rate = 1;//调节速率
int start = 100;//磁道起始位置
double average_FCFS = 0;
double average_SSTF = 0;
double average_LOOK = 0;
double average_C_SCAN = 0;//记录每种算法所用的平均磁道移动数
int n = 0;//记录实验结果的次数
int[] array = new int[400];
ArrayList<Integer> arrayList = new ArrayList<>(400);
ArrayList<Integer> arrayList1 = new ArrayList<>(400);
ArrayList<Integer> arrayList2 = new ArrayList<>(400);
ArrayList<Integer> arrayList3 = new ArrayList<>(400);
ArrayList<Integer> List1 = new ArrayList<>(400);//在SSTF中使用
ArrayList<Integer> List2 = new ArrayList<>(400);//在LOOK中使用
ArrayList<Integer> List3 = new ArrayList<>(400);//在LOOK中使用
Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
double width = screenRectangle.getWidth();
double height = screenRectangle.getHeight();
public static void main(String[] args) {
launch(args);
}
public void init(){//磁盘磁道数为1500
int[] array0 = new int[200];
int[] array1 = new int[100];
int[] array2 = new int[100];
for(int i=0; i<200; i++){
array0[i] = (int)(Math.random()*500);
}
for(int i=0; i<100; i++){
array1[i] = 500+(int)(Math.random()*500);
}
for(int i=0; i<100; i++){
array2[i] = 1000+(int)(Math.random()*500);
}
for (int i=0; i<400; i++){
if (i<200){
array[i] = array0[i];
}
if (i>=200&&i<300){
array[i] = array1[i-200];
}
if (i>=300){
array[i] = array2[i-300];
}
}
array = broken(array);
for(int i=0; i<400; i++){
arrayList.add(i,array[i]);
List1.add(i,array[i]);
List2.add(i,array[i]);
List3.add(i,array[i]);
}
}
public ArrayList<Integer> getShort_index(ArrayList<Integer> List0, int start){//得到SSTF的磁道数组
//这里代码没有给出
}
public ArrayList<Integer> get_LOOK(ArrayList<Integer> List0,int start,int flag){
//这里代码没有给出
}
public ArrayList<Integer> get_C_SCAN(ArrayList<Integer> List0, int start,int flag){
//这里代码没有给出
}
public int count_number(ArrayList<Integer> List,int index){//获取当前位置的磁道移动数
int sum = 0;
int start0 = start;
for(int i=0; i<=index; i++){
sum += Math.abs(List.get(i)-start0);
start0 = List.get(i);
}
return sum;
}
public int count_sum(ArrayList<Integer> List){//获取该算法总的磁道移动数
int sum = 0;
int start0 = start;
for(int i=0; i<List.size(); i++){
sum += Math.abs(List.get(i)-start0);
start0 = List.get(i);
}
return sum;
}
public void analyze() throws IOException {//读取文本内容分析结果
int c = 0;
String string = new String();
FileReader fr = new FileReader("test.txt");
BufferedReader bfr = new BufferedReader(fr);
if (bfr.readLine()==null){
fr.close();
bfr.close();
new Null_result();
}
else {
FileReader fr1 = new FileReader("test.txt");
BufferedReader bfr1 = new BufferedReader(fr1);
while ((string = bfr1.readLine()) != null) {
if (c % 4 == 0) {
average_FCFS += Integer.parseInt(string);
}
if (c % 4 == 1) {
average_SSTF += Integer.parseInt(string);
}
if (c % 4 == 2) {
average_LOOK += Integer.parseInt(string);
}
if (c % 4 == 3) {
average_C_SCAN += Integer.parseInt(string);
}
c++;
}
average_FCFS = average_FCFS / (c / 4);
average_SSTF = average_SSTF / (c / 4);
average_LOOK = average_LOOK / (c / 4);
average_C_SCAN = average_C_SCAN / (c / 4);
n = c / 4;
}
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
NumberAxis xAxis1 = new NumberAxis();
NumberAxis yAxis1 = new NumberAxis();
NumberAxis xAxis2 = new NumberAxis();
NumberAxis yAxis2 = new NumberAxis();
NumberAxis xAxis3 = new NumberAxis();
NumberAxis yAxis3 = new NumberAxis();
LineChart lineChart = new LineChart(xAxis, yAxis);
LineChart lineChart1 = new LineChart(xAxis1, yAxis1);
LineChart lineChart2 = new LineChart(xAxis2, yAxis2);
LineChart lineChart3 = new LineChart(xAxis3, yAxis3);
int sum = 0;
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
// lineChart.setTitle("FCFS");
// lineChart1.setTitle("SSTF");
// lineChart2.setTitle("LOOK");
VBox vBox = new VBox();
VBox vBox1 = new VBox();
TextField textField = new TextField();
Text text = new Text("FCFS磁头移动道数: "+sum);
Text text1 = new Text("SSTF磁头移动道数: "+sum1);
Text text2 = new Text("LOOK磁头移动道数: "+sum2);
Text text3 = new Text("C-SCAN磁头移动道数: "+sum3);
for(int i=0; i<arrayList.size(); i++){//使用List1替代arrayList,不让arrayList被打乱
List1.add(i,arrayList.get(i));
List2.add(i,arrayList.get(i));
List3.add(i,arrayList.get(i));
}
arrayList1 = getShort_index(List1,start);
arrayList2 = get_LOOK(List2,start,0);
arrayList3 = get_C_SCAN(List3,start,0);
count_number(arrayList,count);
//创建列表存储曲线
ObservableList<XYChart.Series<String, Double>> answer = FXCollections.observableArrayList();
ObservableList<XYChart.Series<String, Double>> answer1 = FXCollections.observableArrayList();
ObservableList<XYChart.Series<String, Double>> answer2 = FXCollections.observableArrayList();
ObservableList<XYChart.Series<String, Double>> answer3 = FXCollections.observableArrayList();
EventHandler<ActionEvent> eventHandler = e -> {
if (answer.size()==0){
XYChart.Series<String, Double> series = new XYChart.Series<String, Double>();
answer.addAll(series);
lineChart.getData().addAll(series);
answer.get(0).getData().add(new XYChart.Data(t,start));
answer.get(0).setName("FCFS");
}
if (answer1.size()==0){
XYChart.Series<String, Double> series = new XYChart.Series<String, Double>();
answer1.addAll(series);
lineChart1.getData().addAll(series);
answer1.get(0).getData().add(new XYChart.Data(t,start));
answer1.get(0).setName("SSTF");
}
if (answer2.size()==0){
XYChart.Series<String, Double> series = new XYChart.Series<String, Double>();
answer2.addAll(series);
lineChart2.getData().addAll(series);
answer2.get(0).getData().add(new XYChart.Data(t,start));
answer2.get(0).setName("LOOK");
}
if (answer3.size()==0){
XYChart.Series<String, Double> series = new XYChart.Series<String, Double>();
answer3.addAll(series);
lineChart3.getData().addAll(series);
answer3.get(0).getData().add(new XYChart.Data(t,start));
answer3.get(0).setName("C-SCAN");
t++;
}
if (count<400) {
answer.get(0).getData().add(new XYChart.Data(t, arrayList.get(count)));
text.setText("FCFS磁头移动道数: "+count_number(arrayList,count));
}
if (count<arrayList1.size()) {
answer1.get(0).getData().add(new XYChart.Data(t, arrayList1.get(count)));
text1.setText("SSTF磁头移动道数: "+count_number(arrayList1,count));
}
if (count<arrayList2.size()) {
answer2.get(0).getData().add(new XYChart.Data(t, arrayList2.get(count)));
text2.setText("LOOK磁头移动道数: "+count_number(arrayList2,count));
}
if (count<arrayList3.size()) {
answer3.get(0).getData().add(new XYChart.Data(t, arrayList3.get(count)));
text3.setText("C-SCAN磁头移动道数: "+count_number(arrayList3,count));
}
t++;
count++;
};
Text text4 = new Text("输入数据变化速度,单位ms");
textField.setPromptText("输入数据变化速度,单位ms");
VBox vBox2 = new VBox();
Button button = new Button("暂停");
Button button1 = new Button("继续");
Button button2 = new Button("倍速加一");
Button button3 = new Button("倍速减一");
Button button4 = new Button("重来一遍");
Button button5 = new Button("超级加倍!!");
Button button6 = new Button("查看分析结果");
button.setPrefSize(70,20);
button1.setPrefSize(70,20);
button2.setPrefSize(70,20);
button3.setPrefSize(70,20);
button4.setPrefSize(70,20);
button5.setPrefSize(80,20);
HBox hBox = new HBox();
HBox hBox1 = new HBox();
hBox.getChildren().addAll(button,button1,button2);
hBox1.getChildren().addAll(button3,button4,button5);
hBox.setSpacing(20);
hBox1.setSpacing(20);
vBox2.setSpacing(9);
vBox2.getChildren().addAll(text4,textField,hBox,hBox1);
textField.setOnAction(e->{
Timeline animation = new Timeline(new KeyFrame(Duration.millis(Integer.parseInt(textField.getText())), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
try {
FileWriter fw = new FileWriter("test.txt",true);
String string = new String();
string = count_sum(arrayList)+"\n";
fw.write(string);
string = count_sum(arrayList1)+"\n";
fw.write(string);
string = count_sum(arrayList2)+"\n";
fw.write(string);
string = count_sum(arrayList3)+"\n";
fw.write(string);
fw.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
button.setOnAction(e0->{
animation.pause();
});
button1.setOnAction(e1->{
animation.play();
});
button2.setOnAction(e3->{
rate = rate+1;
animation.setRate(rate);
// System.out.println(animation.getKeyFrames().get(0).getTime());
// System.out.println(rate);
});
button3.setOnAction(e3->{
if (rate-1==0){
rate = rate;
}
else {
rate = rate - 1;
}
animation.setRate(rate);
// System.out.println(animation.getKeyFrames().get(0).getTime());
// System.out.println(rate);
});
button4.setOnAction(e2->{
answer.get(0).getData().clear();
answer1.get(0).getData().clear();
answer2.get(0).getData().clear();
answer3.get(0).getData().clear();
// lineChart.
arrayList.clear();
arrayList1.clear();
arrayList2.clear();
arrayList3.clear();
List1.clear();
List2.clear();
List3.clear();
t = 0;
count = 0;
init();
arrayList1 = getShort_index(List1,start);
arrayList2 = get_LOOK(List2,start,0);
arrayList3 = get_C_SCAN(List3,start,0);
try {
FileWriter fw = new FileWriter("test.txt",true);
String string = new String();
string = count_sum(arrayList)+"\n";
fw.write(string);
string = count_sum(arrayList1)+"\n";
fw.write(string);
string = count_sum(arrayList2)+"\n";
fw.write(string);
string = count_sum(arrayList3)+"\n";
fw.write(string);
fw.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
animation.setRate(1);
animation.play();
});
button5.setOnAction(e4->{
animation.setRate(1000);
});
});
button6.setOnAction(e5->{
try {
analyze();
} catch (IOException e) {
e.printStackTrace();
}
if (n!=0)
new Result(average_FCFS,average_SSTF,average_LOOK,average_C_SCAN,n);
});
vBox1.getChildren().addAll(vBox2,text,text1,text2,text3,button6);
vBox1.setSpacing(50);
vBox1.setPadding(new Insets(200,100,0,100));
BorderPane borderPane = new BorderPane();
borderPane.setLeft(vBox);
borderPane.setRight(vBox1);
vBox.getChildren().addAll(lineChart,lineChart1,lineChart2,lineChart3);
// pane.setStyle("-fx-background-color:red");
vBox.setPrefSize((2*width)/3,height-60);
vBox1.setPrefSize(width/3,height-60);
primaryStage.setScene(new Scene(borderPane, width, height-60));
primaryStage.show();
}
}
Null_result类
package com.codewithmosh;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Null_result {
Null_result(){
Stage primaryStage = new Stage();
Pane pane = new Pane();
Text text = new Text("还没有进行过数据分析,暂无结果,请先在输入框中输入速率得出结果后再分析");
pane.getChildren().add(text);
text.setLayoutX(25);
text.setLayoutY(25);
Scene scene = new Scene(pane,470,50);
primaryStage.setScene(scene);
primaryStage.show();
EventHandler<ActionEvent> eventHandler = e ->{
primaryStage.close();
};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(5000), eventHandler));
animation.setCycleCount(1);
animation.play();
}
}
Result类
package com.codewithmosh;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Result {
Result(double average_FCFS,double average_SSTF,double average_LOOK,double average_C_SCAN,int n){
Stage primaryStage = new Stage();
String string = new String();
double min = average_FCFS;
string = "FCFS";
if (min>average_SSTF) {
min = average_SSTF;
string = "SSTF";
}
if (min>average_LOOK) {
min = average_LOOK;
string = "LOOK";
}
if (min>average_C_SCAN) {
min = average_C_SCAN;
string = "C_SCAN";
}
Pane pane = new Pane();
Text text = new Text("FCFS算法平均磁盘移动数为: "+average_FCFS);
Text text1 = new Text("SSTF算法平均磁盘移动数为: "+average_SSTF);
Text text2 = new Text("LOOK算法平均磁盘移动数为: "+average_LOOK);
Text text3 = new Text("C-SCAN算法平均磁盘移动数为: "+average_C_SCAN);
Text text4 = new Text("根据 "+n+" 次的数据分析得出目前最佳的算法为: ");
Text text5 = new Text(string);
text5.setFill(Color.RED);
pane.getChildren().addAll(text,text1,text2,text3,text4,text5);
text.setLayoutY(50);
text.setLayoutX(50);
text1.setLayoutY(70);
text1.setLayoutX(50);
text2.setLayoutY(90);
text2.setLayoutX(50);
text3.setLayoutY(110);
text3.setLayoutX(50);
text4.setLayoutY(150);
text4.setLayoutX(50);
text5.setLayoutY(150);
text5.setLayoutX(290);
Scene scene = new Scene(pane,400,200);
primaryStage.setScene(scene);
primaryStage.show();
}
}