本文主要是介绍Java 实现 磁盘调度算法的实现与分析(计算机操作系统课程设计)-----扫描磁道移动路径可视化实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近期末,计算机操作系统课程设计需要实现----磁盘调度算法。就使用Java写个图形界面的磁盘调度算法。
实现磁盘调度的四种算法:
先来先服务算法(FCFS)
最短寻道时间优先算法(SSTF)
扫描算法(SCAN)
循环扫描算法(C-SCAN)
实现原理:数组的排序,画笔的绘制
运行截图:
代码如下:复制即用
package com.huang;import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import javax.swing.*;
/*** @author HuangAn* @date 2021/12/28*/public class Demo extends JFrame implements ActionListener{ //主面板JButton btnFCFS; //先来先服务算法(FCFS)JButton btnSSTF; //最短寻道时间优先算法(SSTF)JButton btnSCAN; //扫描算法(SCAN)JButton btnCSCAN; //循环扫描算法(C-SCAN)JPanel jMainPanel = new JPanel(); //按钮面板JPanel buttons = new JPanel(); //绘图面板JLabel jLabel1;JLabel jLabel2;StringBuffer s;int start;int[] arr;int fcfsNUM=0;int sstfNUM=0;int scanNUM=0;int cscanNUM=0;public Demo(){}public Demo(int start, int[] arr){super("绘制形状");this.start=start;this.arr=arr;StringBuffer stringBuffer=new StringBuffer();for (int tt=0;tt<arr.length;tt++){stringBuffer.append(arr[tt]);stringBuffer.append(" ");}this.s=stringBuffer;int fcfsNUM=0; //先来先服务算法(FCFS)int temp=start;for (int i=0;i<arr.length;i++){fcfsNUM+= Math.abs(arr[i] - temp);temp=arr[i];}this.fcfsNUM=fcfsNUM;List<Object> resultList = Arrays.stream(arr).boxed().collect(Collectors.toList());//最短寻道时间优先算法(SSTF)int min=300;int temp1 = 0;int jiLuID=0;int start1=start;int[] newArr=new int[10];for (int i=0;i<arr.length;i++){for (int j=0;j<resultList.size();j++){if (min>=Math.abs((int)(resultList.get(j))-start1)){min=Math.abs((int)(resultList.get(j))-start1);temp1=(int)resultList.get(j);jiLuID=j;}}newArr[i]=temp1;min=300;resultList.remove(jiLuID);start1=temp1;}int sstfNUM=0;int temp2=start;for (int i=0;i<newArr.length;i++){sstfNUM+= Math.abs(newArr[i] - temp2);temp2=newArr[i];}this.sstfNUM=sstfNUM;int[] scanArr=new int[10]; //扫描算法(SCAN)for (int u=0;u<arr.length;u++){scanArr[u]=arr[u];}Arrays.sort(scanArr);int[] newArrscan=new int[11];int pp=0;while (scanArr[pp]<start){pp++;}for (int j=pp;j>0;j--){newArrscan[pp-j]=scanArr[j-1];}newArrscan[pp]=0;for (int o=pp+1,h=pp;h<scanArr.length;h++){newArrscan[o]=scanArr[h];o++;}int scanNUM=0;int temp3=start;for (int i1=0;i1<newArrscan.length;i1++){scanNUM+= Math.abs(newArrscan[i1] - temp3);temp3=newArrscan[i1];}this.scanNUM=scanNUM;int[] cscan=new int[10]; //循环扫描算法(C-SCAN)for (int u=0;u<arr.length;u++){cscan[u]=arr[u];}Arrays.sort(cscan);int[] newArrcscan=new int[12];int zz=0;while (cscan[zz]<start){zz++;}for(int h=0;h<10-zz;h++){newArrcscan[h]=cscan[zz];zz++;}newArrcscan[10-zz]=200;newArrcscan[11-zz]=0;for (int j=zz,d=0;j>0;j--){newArrcscan[12-j]=cscan[d];d++;}int cscanNUM=0;int temp4=start;for (int rr=0;rr<newArrcscan.length;rr++){cscanNUM+= Math.abs(newArrcscan[rr] - temp4);temp4=newArrcscan[rr];}this.cscanNUM=cscanNUM;btnFCFS = new JButton("FCFS");btnSSTF = new JButton("SSTF");btnSCAN = new JButton("SCAN");btnCSCAN = new JButton("CSCAN");btnFCFS.addActionListener(this);btnSSTF.addActionListener(this);btnSCAN.addActionListener(this);btnCSCAN.addActionListener(this);this.setLayout(null);buttons.add(btnFCFS);buttons.add(btnSSTF);buttons.add(btnSCAN);buttons.add(btnCSCAN);jMainPanel.setLayout(new BorderLayout()); //绘图面板设置布局buttons.setBounds(20,0,1000,100);jMainPanel.setBounds(20,100,1000,300);jLabel1=new JLabel("磁盘队列为:");jLabel1.setBounds(20,0,200,20);buttons.add(jLabel1);this.getContentPane().add(buttons);this.getContentPane().add(jMainPanel);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocation(100,50);setSize(1200,400);setVisible(true);}public void actionPerformed(ActionEvent ae){if(ae.getActionCommand().equals("FCFS")){jLabel1.setText(" 先来先服务算法总共移动磁道数:"+fcfsNUM+" 。 随机磁道序列:"+s);jMainPanel.removeAll();jMainPanel.add(new FCFS(start,arr));jMainPanel.repaint();jMainPanel.validate();}if(ae.getActionCommand().equals("SSTF")){jLabel1.setText("最短寻道时间优先算法总共移动磁道数:"+sstfNUM+" 。 随机磁道序列:"+s);jMainPanel.removeAll();jMainPanel.add(new SSTF(start,arr));jMainPanel.repaint();jMainPanel.validate();}if(ae.getActionCommand().equals("SCAN")){jLabel1.setText(" 扫描算法总共移动磁道数:"+scanNUM+" 。 随机磁道序列:"+s);jMainPanel.removeAll();jMainPanel.add(new SCAN(start,arr));jMainPanel.repaint();jMainPanel.validate();}if(ae.getActionCommand().equals("CSCAN")){jLabel1.setText(" 循环扫描算法总共移动磁道数:"+cscanNUM+" 。 随机磁道序列:"+s);jMainPanel.removeAll();jMainPanel.add(new CSCAN(start,arr));jMainPanel.repaint();jMainPanel.validate();}}public static void main(String args[]){int hh=0;int[] a=new int[10];for (int i=0;i<10;i++){int n=(int)(Math.random()*200)+1;for (int j=0;j<10;j++){if (a[j]==n){hh++;}}if (hh==0&&n!=0&&n!=200){a[i]=n;}else {hh=0;i--;}}for (int i=0;i<a.length;i++){System.out.println(a[i]);}Scanner scanner=new Scanner(System.in);System.out.println("请输入磁头的初始位置(0-200之间):");int start=scanner.nextInt();new Demo(start,a);}
}class FCFS extends JPanel{ //先来先服务算法(FCFS)public int start;public int[] x;public FCFS(int start,int[] x) {this.start=start;int[] newArr;newArr=x;this.x = newArr;}public void paintComponent(Graphics g){g.drawString(String.valueOf(start),start*4+2,10);g.drawLine(start*4, 0,x[0]*4,20);g.drawLine(x[0]*4,20 ,x[1]*4,40 );g.drawLine(x[1]*4,40 ,x[2]*4,60 );g.drawLine(x[2]*4,60 ,x[3]*4,80 );g.drawLine(x[3]*4,80 ,x[4]*4,100);g.drawLine(x[4]*4,100,x[5]*4,120);g.drawLine(x[5]*4,120,x[6]*4,140);g.drawLine(x[6]*4,140,x[7]*4,160);g.drawLine(x[7]*4,160,x[8]*4,180);g.drawLine(x[8]*4,180,x[9]*4,200);for (int i=0;i<x.length;i++){g.drawString(String.valueOf(x[i]),x[i]*4+2,20*(i+1));}}
}class SSTF extends JPanel{ //最短寻道时间优先算法(SSTF)public int start;public int[] x;public SSTF(int start,int[] x) {this.start=start;List<Object> resultList = Arrays.stream(x).boxed().collect(Collectors.toList());int min=300;int temp = 0;int jiLuID=0;int[] newArr=new int[10];for (int i=0;i<x.length;i++){for (int j=0;j<resultList.size();j++){if (min>=Math.abs((int)(resultList.get(j))-start)){min=Math.abs((int)(resultList.get(j))-start);temp=(int)resultList.get(j);jiLuID=j;}}newArr[i]=temp;min=300;resultList.remove(jiLuID);start=temp;}this.x = newArr;}public void paintComponent(Graphics g){g.drawString(String.valueOf(start),start*4+2,10);g.drawLine(start*4, 0,x[0]*4,20);g.drawLine(x[0]*4,20 ,x[1]*4,40 );g.drawLine(x[1]*4,40 ,x[2]*4,60 );g.drawLine(x[2]*4,60 ,x[3]*4,80 );g.drawLine(x[3]*4,80 ,x[4]*4,100);g.drawLine(x[4]*4,100,x[5]*4,120);g.drawLine(x[5]*4,120,x[6]*4,140);g.drawLine(x[6]*4,140,x[7]*4,160);g.drawLine(x[7]*4,160,x[8]*4,180);g.drawLine(x[8]*4,180,x[9]*4,200);for (int i=0;i<x.length;i++){g.drawString(String.valueOf(x[i]),x[i]*4+2,20*(i+1));}}
}class SCAN extends JPanel{ //扫描算法(SCAN)public int start;public int[] x;public SCAN(int start,int[] y) {this.start=start;int[] x=new int[10];for (int u=0;u<y.length;u++){x[u]=y[u];}Arrays.sort(x);int[] newArr=new int[11];int i=0;while (x[i]<start){i++;}for (int j=i;j>0;j--){newArr[i-j]=x[j-1];}newArr[i]=0;for (int o=i+1,h=i;h<x.length;h++){newArr[o]=x[h];o++;}this.x =newArr;}public void paintComponent(Graphics g){g.drawString(String.valueOf(start),start*4+2,10);g.drawLine(start*4, 0,x[0]*4,20);g.drawLine(x[0]*4,20 ,x[1]*4,40 );g.drawLine(x[1]*4,40 ,x[2]*4,60 );g.drawLine(x[2]*4,60 ,x[3]*4,80 );g.drawLine(x[3]*4,80 ,x[4]*4,100);g.drawLine(x[4]*4,100,x[5]*4,120);g.drawLine(x[5]*4,120,x[6]*4,140);g.drawLine(x[6]*4,140,x[7]*4,160);g.drawLine(x[7]*4,160,x[8]*4,180);g.drawLine(x[8]*4,180,x[9]*4,200);g.drawLine(x[9]*4,200,x[10]*4,220);for (int i=0;i<x.length;i++){g.drawString(String.valueOf(x[i]),x[i]*4+2,20*(i+1));}}
}class CSCAN extends JPanel{ //循环扫描算法(C-SCAN)public int start;public int[] x;public CSCAN(int start,int[] y) {this.start=start;int[] x=new int[10];for (int u=0;u<y.length;u++){x[u]=y[u];}Arrays.sort(x);int[] newArr=new int[12];int i=0;while (x[i]<start){i++;}for(int h=0;h<10-i;h++){newArr[h]=x[i];i++;}newArr[10-i]=200;newArr[11-i]=0;for (int j=i,d=0;j>0;j--){newArr[12-j]=x[d];d++;}this.x =newArr;}public void paintComponent(Graphics g){g.drawString(String.valueOf(start),start*4+2,10);g.drawLine(start*4, 0,x[0]*4,20);g.drawLine(x[0]*4,20 ,x[1]*4,40 );g.drawLine(x[1]*4,40 ,x[2]*4,60 );g.drawLine(x[2]*4,60 ,x[3]*4,80 );g.drawLine(x[3]*4,80 ,x[4]*4,100);g.drawLine(x[4]*4,100,x[5]*4,120);g.drawLine(x[5]*4,120,x[6]*4,140);g.drawLine(x[6]*4,140,x[7]*4,160);g.drawLine(x[7]*4,160,x[8]*4,180);g.drawLine(x[8]*4,180,x[9]*4,200);g.drawLine(x[9]*4,200,x[10]*4,220);g.drawLine(x[10]*4,220,x[11]*4,240);for (int i=0;i<x.length;i++){g.drawString(String.valueOf(x[i]),x[i]*4+2,20*(i+1));}}
}
这篇关于Java 实现 磁盘调度算法的实现与分析(计算机操作系统课程设计)-----扫描磁道移动路径可视化实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!