代码中有大量注释,实现较为简单,相信不需要太多文本,直接看代码更容易理解磁盘调度的过程。
头文件都给了这么贴心的没有谁了吧!!!
import java.util.Scanner;
import java.util.Arrays;
需要传递进算法的数据为,磁道队列,和磁道队列长度。
public static void main(String[] args) {
DiskScheduling A = new DiskScheduling();
int[] cidao = {
55, 58, 39, 18, 90, 160, 150, 38, 184};
int count = cidao.length;
//使用先来先服务算法
System.out.println("**************使用先来先服务算法!************");
A.FCFS(cidao, count);
//使用SSTF算法
System.out.println("**********使用最短寻道时间优先调度算法!********");
A.SSTF(cidao, count);
//使用SCAN算法
System.out.println("***************使用扫描调度算法!*************");
A.SCAN(cidao, count);
//使用CSCAN算法
System.out.println("**************使用循环扫描调度算法!***********");
A.CSCAN(cidao, count);
}
//先进先出*********************************************
void FCFS(int cidao[], int m) //磁道号数组,个数为m
{
int now;//当前磁道号
int sum = 0; //总寻道长度
int j, i;
float ave = 0; //平均寻道长度
System.out.println("磁盘请求序列为:");
for (i = 0; i < m; i++) //按先来先服务的策略输出磁盘请求序列
{
System.out.print(cidao[i] + " ");
}
System.out.println();
System.out.print("请输入当前的磁道号:");
Scanner w = new Scanner(System.in);
now = w.nextInt();
sum += Math.abs(cidao[0] - now);
System.out.println("磁盘扫描序列****移动磁道数");
int before = now;
//用于保存上一个磁道位(开始为输入的磁道数)
for (i = 0; i < m; i++) //输出磁盘扫描序列
{
int movenumber=cidao[i]-before;
before=cidao[i];
//保证移动磁道数为正
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " " + movenumber);
}
for (i = 0, j = 1; j < m; i++, j++) //求平均寻道长度
{
sum += Math.abs(cidao[j] - cidao[i]);
ave = (float) sum / m;
}
System.out.println();
System.out.println("平均寻道长度:" + ave);
}
//短磁道优先*******************************************
void SSTF(int cidao[], int m) {
int k = 1;
int now, l, r;
int i, j, sum = 0;
int a;
float ave;
Arrays.sort(cidao);
System.out.print("请输入当前的磁道号:");
Scanner w = new Scanner(System.in);
now = w.nextInt();
if (cidao[m - 1] <= now) //若当前磁道号大于请求序列中最大者,则直接由外向内依次给予各请求服务
{
System.out.println("磁盘扫描序列****移动磁道数");
int before = now;
for (i = m - 1; i >= 0; i--)
{
int movenumber=cidao[i]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " "+movenumber);
before=cidao[i];
}
sum = now - cidao[0];
}
if (cidao[0] >= now) //若当前磁道号小于请求序列中最小者,则直接由内向外依次给予各请求服务
{
System.out.println("磁盘扫描序列****移动磁道数");
int before = now;
for (i = 0; i < m; i++)
{
int movenumber=cidao[i]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " " + movenumber);
before=cidao[i];
}
sum = cidao[m - 1] - now;
}
if (now > cidao[0] && now < cidao[m - 1]) //若当前磁道号大于请求序列中最小者且小于最大者
{
System.out.println("磁盘扫描序列****移动磁道数");
int before = now;
while (cidao[k] < now) //确定当前磁道在已排的序列中的位置,后面的算法都用到了,可以直接复制后少量修改,节省时间。
{
k++;
}
l = k - 1;
r = k;
while ((l >= 0) && (r < m)) //当前磁道在请求序列范围内
{
if ((now - cidao[l]) <= (cidao[r] - now)) //选择与当前磁道最近的请求给予服务
{
int movenumber=cidao[l]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
before=cidao[l];
System.out.println(cidao[l] + " " + movenumber);
sum += now - cidao[l];
now = cidao[l];
l = l - 1;
} else {
int movenumber=cidao[r]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
before=cidao[r];
System.out.println(cidao[r] + " " + movenumber);
sum += cidao[r] - now;
now = cidao[r];
r = r + 1;
}
}
if (l == -1) //磁头移动到序列的最小号,返回外侧扫描仍未扫描的磁道
{
for (j = r; j < m; j++) {
int movenumber=cidao[j]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
before=cidao[j];
System.out.println(cidao[j] + " " + movenumber);
}
sum += cidao[m - 1] - cidao[0];
} else //磁头移动到序列的最大号,返回内侧扫描仍未扫描的磁道
{
for (j = l; j >= 0; j--) {
int movenumber=cidao[j]-before;
if(movenumber<0){
movenumber=0-movenumber;
}
before=cidao[j];
System.out.println(cidao[j] + " " + movenumber);
}
sum += cidao[m - 1] - cidao[0];
}
}
ave = (float) (sum) / (float) (m);
System.out.println();
System.out.println("平均寻道长度: " + ave);
}
//扫描调度*********************************************
void SCAN(int cidao[], int m) //先要给出当前磁道号和移动臂的移动方向
{
int k = 1;
int now, l, r, d;
int i, j, sum = 0;
int a;
float ave;
Arrays.sort(cidao);
System.out.print("请输入当前的磁道号:");
Scanner w = new Scanner(System.in);
now = w.nextInt();
if (cidao[m - 1] <= now) //若当前磁道号大于请求序列中最大者,则直接由外向内依次给予各请求服务,此情况同最短寻道优先
{
System.out.println("磁盘扫描序列****移动磁道数");
int before=now;
for (i = m - 1; i >= 0; i--)
{
int movenumber=cidao[i]-before;
before=cidao[i];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " "+movenumber);
}
sum = now - cidao[0];
}
if (cidao[0] >= now) //若当前磁道号小于请求序列中最小者,则直接由内向外依次给予各请求服务,此情况同最短寻道优先
{
System.out.println("磁盘扫描序列为:");
int before=now;
for (i = 0; i < m; i++)
{
int movenumber=cidao[i]-before;
before=cidao[i];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " " + movenumber);
}
sum = cidao[m - 1] - now;
}
if (now > cidao[0] && now < cidao[m - 1]) //若当前磁道号大于请求序列中最小者且小于最大者
{
while (cidao[k] < now) {
k++;
}
l = k - 1;
r = k;
System.out.println("请输入当前移动臂的移动的方向 (1 表示向外 ,0表示向内) : ");
Scanner x = new Scanner(System.in);
d = x.nextInt();
int before=now;
if (d == 0) //选择移动臂方向向内,则先向内扫描
{
System.out.println("磁盘扫描序列为:");
for (j = l; j >= 0; j--) {
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
for (j = r; j < m; j++) //磁头移动到最小号,则改变方向向外扫描未扫描的磁道
{
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
sum = now - 2 * cidao[0] + cidao[m - 1];
} else //选择移动臂方向向外,则先向外扫描
{
System.out.println("磁盘扫描序列为:");
for (j = r; j < m; j++) {
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
for (j = l; j >= 0; j--) //磁头移动到最大号,则改变方向向内扫描未扫描的磁道
{
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
sum = -now - cidao[0] + 2 * cidao[m - 1];
}
}
ave = (float) (sum) / (float) (m);
System.out.print("平均寻道长度: " + ave);
System.out.println();
}
//循环扫描*********************************************
void CSCAN(int cidao[], int m) {
int k = 1;
int now, l, r;
int i, j, sum = 0;
int a;
float ave;
Arrays.sort(cidao);
System.out.print("请输入当前的磁道号:");
Scanner w = new Scanner(System.in);
now = w.nextInt();
int before=now;
if (cidao[m - 1] <= now) //若当前磁道号大于请求序列中最大者,则直接将移动臂移动到最小号磁道依次向外给予各请求服务
{
System.out.println("磁盘扫描序列为:");
for (i = 0; i < m; i++)
{
int movenumber=cidao[i]-before;
before=cidao[i];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " " + movenumber);
}
sum = now - 2 * cidao[0] + cidao[m - 1];
}
if (cidao[0] >= now) //若当前磁道号小于请求序列中最小者,则直接由内向外依次给予各请求服务,此情况同最短寻道优先
{
System.out.println("磁盘扫描序列为:");
for (i = 0; i < m; i++)
{
int movenumber=cidao[i]-before;
before=cidao[i];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[i] + " " + movenumber);
}
sum = cidao[m - 1] - now;
}
if (now > cidao[0] && now < cidao[m - 1]) //若当前磁道号大于请求序列中最小者且小于最大者
{
System.out.println("磁盘扫描序列为:");
while (cidao[k] < now) //单向反复地从内向外扫描
{
k++;
}
l = k - 1;
r = k;
for (j = r; j < m; j++) {
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
for (j = 0; j < r; j++) //当扫描完最大号磁道,磁头直接移动到最小号磁道,再向外扫描未扫描的磁道
{
int movenumber=cidao[j]-before;
before=cidao[j];
if(movenumber<0){
movenumber=0-movenumber;
}
System.out.println(cidao[j] + " " + movenumber);
}
sum = 2 * cidao[m - 1] + cidao[l] - now - 2 * cidao[0];
}
ave = (float) (sum) / (float) (m);
System.out.print("平均寻道长度: " + ave);
System.out.println();
}