实验内容
1,选择一到三种磁盘调度算法,模拟实现磁盘调度
2,能输入当前磁头的位置,磁头移动方向,磁道访问请求序列等。
3,计算磁头移动的总磁道数
4,能够显示磁盘调度结果
实验代码
// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<iostream>
#include"Disk.h"
using namespace std;
//磁盘
Disk disk;
//添加访问序列
void addvist() {
int num = 0;
cout << "要添加的个数" << endl;
cin >> num;
for (int i = 0; i < num; i++)
{
int index = 0;
cout << i + 1 << "个:" << endl;
cout << "磁道位置:";
cin >> index;
if (index>0 && index<disk.DiskLength)
{
disk.AddVist(index);
}
}
cout << endl;
}
//进行磁道访问调度
void vistdisk() {
//磁头初始位置
int start = disk.DiskHead;
int way = 0;
cout << "------1,先来先服务 ------2,最短寻道 -------3,电梯调度" << endl;
cout << "输入调度方法:";
cin >> way;
int* sequence=new int[disk.VisitNum];
int StepNum = 0;
if (1==way)
{//1,先来先服务
StepNum = disk.FCFSDispatch(sequence);
}
else if (2==way)
{//2,最短寻道
StepNum = disk.SSTFDispatch(sequence);
}
else
{//3,电梯调度
StepNum = disk.SCANDispatch(sequence);
}
cout << "磁头走过的总道数:" << StepNum << endl;
cout << "走道序列:" << start;
for (int i = 0; i < disk.VisitNum; i++)
{
cout <<"----->"<< sequence[i];
}
//初始化
disk.InitDisk();
cout << endl;
}
//设置磁头移动方向
void setDirection()
{
int Direction = 0;
cout << "输入移动方向,----0代表向外,----1代表向内";
cin >> Direction;
disk.Direction = Direction;
cout << endl;
}
//设置磁头位置
void setDiskHead() {
int DiskHead = 0;
cout << "输入磁头位置";
cin >> DiskHead;
disk.DiskHead = DiskHead;
cout << endl;
}
int main()
{
int disksize = 0;
cout << "磁盘大小:";
cin >> disksize;
disk.DiskLength = disksize;
while (1)
{
cout << "*******************磁盘调度***********************" << endl;
cout << "-----1:添加磁道访问序列" << endl;
cout << "-----2:进行磁道访问调度" << endl;
cout << "-----3:设置磁头移动方向" << endl;
cout << "-----4:设置磁头位置" << endl;
cout << " 输入选择:";
int choice;
cin >> choice;
switch (choice)
{
case 1:
addvist();
break;
case 2:
vistdisk();
break;
case 3:
setDirection();
break;
case 4:
setDiskHead();
break;
default:
break;
}
}
return 0;
}
#include "Disk.h"
using namespace std;
#include<stdlib.h>
Disk::Disk()
{
//初始化成员变量
InitDisk();
}
//初始化成员变量
void Disk::InitDisk()
{
VisitNum = 0;
DiskHead = 0;
Direction = 0;
for (int i = 0; i < 100; i++)
{
VisitList[i] = -1;
}
}
/*
* 添加访问磁道
* index: 要访问的位置
*/
void Disk::AddVist(int index)
{
VisitList[VisitNum] = index;
VisitNum++;
}
/*
* 设置磁头位置
*/
void Disk::Setdiskhead(int index)
{
DiskHead = index;
}
/*
* 先来先服务调度
* &sequence 访问序列
* return: 磁头走过的总道数
*/
int Disk::FCFSDispatch(int* sequence)
{
//磁头走过的步数
int StepNum = 0;
//因为添加时,即安装顺序添加
for (int i = 0; i < VisitNum; i++)
{
sequence[i] = VisitList[i];
StepNum = StepNum + abs((VisitList[i] - DiskHead));
DiskHead = VisitList[i];
VisitList[i] = -1;
}
return StepNum;
}
/*
* 最短寻道调度
*/
int Disk::SSTFDispatch(int* sequence)
{
//最短寻道的调度,下一个访问的磁道是离当前磁头最近的磁道,即减去当前磁道的绝对值最小
//磁头走过的步数
int StepNum = 0;
//临时变量,用来记录当前访问多少磁道
int num = 0;
while (num<VisitNum)
{
//临时变量,用来寻找要访问的磁道
int tem = DiskLength;
//找到离磁头最进的磁道
int index = 0;
for (int i = 0; i < VisitNum; i++)
{
if (VisitList[i]!=-1 && abs((VisitList[i]-DiskHead))<tem)
{
tem = abs((VisitList[i] - DiskHead));
index = i;
}
}
//访问该磁道,更改磁头,并计算距离
sequence[num] = VisitList[index];
StepNum = StepNum + abs((VisitList[index] - DiskHead));
DiskHead = VisitList[index];
VisitList[index] = -1;
num++;
}
return StepNum;
}
/*
* 电梯调度算法
*/
int Disk::SCANDispatch(int* sequence)
{
//对访问序列遍进行递增排序
AscendingOrder();
//找到磁头在访问序列表中的序号
int index = 0;
//走道数
int stepnum = 0;
//找到磁头下一个应该走的磁道
for (; index < VisitNum; index++)
{
if (DiskHead <= VisitList[index])
{
break;
}
}
//根据磁头移动方向,进行走道
if (1==Direction)
{
stepnum = TowardRight(sequence, index);
}
else
{
stepnum = TowardLeft(sequence, index);
}
return stepnum;
}
/*
* 磁头向左移动
*/
int Disk::TowardLeft(int* sequence, int index)
{
int step = 0;
int stepnum = 0;
if (DiskHead!=VisitList[index])
{
index--;
}
for (int i = index; i >=0; i--)
{
sequence[step] = VisitList[i];
stepnum = stepnum + abs(VisitList[i] - DiskHead);
DiskHead = VisitList[i];
step++;
}
for (int i = index+1; i < VisitNum; i++)
{
sequence[step] = VisitList[i];
stepnum = stepnum + abs(VisitList[i] - DiskHead);
DiskHead = VisitList[i];
step++;
}
return stepnum;
}
/*
* 磁头向右移动
*/
int Disk::TowardRight(int* sequence, int index)
{
int step = 0;
int stepnum = 0;
for (int i = index; i < VisitNum; i++)
{
sequence[step] = VisitList[i];
stepnum = stepnum + abs(VisitList[i] - DiskHead);
DiskHead = VisitList[i];
step++;
}
for (int i = index-1; i >=0; i--)
{
sequence[step] = VisitList[i];
stepnum = stepnum + abs(VisitList[i] - DiskHead);
DiskHead = VisitList[i];
step++;
}
return stepnum;
}
/*
* 对访问序列进行排序
*/
void Disk::AscendingOrder()
{
for (int i = 0; i <VisitNum-1; i++)
{
for (int j = 0; j < VisitNum - i - 1; j++)
{
if (VisitList[j]>VisitList[j+1])
{
change(VisitList[j], VisitList[j + 1]);
}
}
}
}
/*
* 两个数交互
*/
void Disk::change(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
#pragma once
using namespace std;
/*
* 磁盘类
*/
class Disk
{
/*
* 成员变量
*/
public:
//磁盘大小
int DiskLength;
//当前磁头位置
int DiskHead;
//磁头移动方向 0代表向左移动,1代表向右移动
int Direction;
//磁道访问序列
int VisitList[100];
//当前访问磁道数量
int VisitNum;
/*
* 成员函数
*/
public:
//构造函数
Disk();
//初始化成员变量
void InitDisk();
//添加访问磁道
void AddVist(int index);
//设置磁头位置
void Setdiskhead(int index);
//FCFS(先来先服务)调度
int FCFSDispatch(int *sequence);
//SSTF(最短寻道)调度
int SSTFDispatch(int* sequence);
//SCAN(电梯算法)调度
int SCANDispatch(int* sequence);
//磁头向左访问
int TowardLeft(int* sequence, int index);
//磁头向右访问
int TowardRight(int* sequence, int index);
//访问序列排序
void AscendingOrder();
//两个数进行交换
void change(int& a, int& b);
};