博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(七)磁盘调度
阅读量:5056 次
发布时间:2019-06-12

本文共 4407 字,大约阅读时间需要 14 分钟。

要求:

1。实现三种算法:
1。先来先服务
2。最短寻道优先(老师会给当前磁头的位置)
3。电梯算法
2。磁道服务顺序从指定的文本文件(TXT文件)中取出
3。输出:
第一行:磁道的服务顺序
第二行:显示移动总道数
本程序包括:FIFO,最短寻道优先调度算法,电梯算法

 

磁盘调度
 
#include
<
stdio.h
>
#include
<
iostream.h
>
#include
<
string
.h
>
#include
<
math.h
>
const
int
MAXQUEUE
=
200
;
//
定义队列最大数
//
结构体定义
typedef
struct
node{
int
go;
int
visited;
}qu;
qu queue[MAXQUEUE];
int
quantity;
int
start;
//
定义开始时磁头所在位置
//
初始化函数
void
initial()
{
int
i;
for
(i
=
0
;i
<
MAXQUEUE;i
++
){
queue[i].go
=-
1
;
queue[i].visited
=
0
;
}
start
=
53
;
//
磁头的初始位置
}
//
读入磁道号流
void
readData()
{
FILE
*
fp;
char
fname[
20
];
int
temp,i;
cout
<<
"
请输入磁道号流文件名:
"
;
strcpy(fname,
"
7hard.txt
"
);
cin
>>
fname;
if
((fp
=
fopen(fname,
"
r
"
))
==
NULL){
cout
<<
"
错误,文件打不开,请检查文件名:)
"
<<
endl;
}
else
{
while
(
!
feof(fp)){
fscanf(fp,
"
%d
"
,
&
temp);
queue[quantity].go
=
temp;
quantity
++
;
}
cout
<<
endl
<<
"
---------------------------------------------
"
<<
endl;
cout
<<
"
所读入的磁道号流:
"
;
for
(i
=
0
;i
<
quantity;i
++
){
cout
<<
queue[i].go
<<
"
"
;
}
cout
<<
endl
<<
"
请求数为:
"
<<
quantity
<<
endl;
}
}
//
FIFO算法
void
FIFO()
{
int
i;
int
total
=
0
;
int
current;
cout
<<
endl
<<
"
---------------------------------------------
"
<<
endl;
cout
<<
"
FIFO算法的访问磁道号顺序流:
"
;
current
=
start;
for
(i
=
0
;i
<
quantity;i
++
){
cout
<<
queue[i].go
<<
"
"
;
total
+=
abs(queue[i].go
-
current);
current
=
queue[i].go;
}
cout
<<
endl
<<
"
磁头移过的柱面数:
"
<<
total;
}
//
最短寻道优先调度算法
void
shortest()
{
int
i,j,p;
int
total
=
0
;
int
current;
cout
<<
endl
<<
"
---------------------------------------------
"
<<
endl;
cout
<<
"
最短寻道优先调度算法的访问磁道号顺序流:
"
;
current
=
start;
for
(i
=
0
;i
<
quantity;i
++
){
p
=
0
;
while
(queue[p].visited
!=
0
){
p
++
;
}
for
(j
=
p;j
<
quantity;j
++
){
if
((queue[j].visited
==
0
)
&&
(abs(current
-
queue[p].go)
>
abs(current
-
queue[j].go))){
p
=
j;
}
}
cout
<<
queue[p].go
<<
"
"
;
total
+=
abs(queue[p].go
-
current);
queue[p].visited
=
1
;
current
=
queue[p].go;
}
cout
<<
endl
<<
"
磁头移过的柱面数:
"
<<
total;
}
//
电梯算法
void
elevator()
{
int
i,j,p,flag;
int
total
=
0
;
int
current;
cout
<<
endl
<<
"
---------------------------------------------
"
<<
endl;
cout
<<
"
电梯调度算法
"
<<
endl;
//
磁头初始向里
cout
<<
"
磁头初始向里的访问磁道号顺序流:
"
;
current
=
start;
for
(i
=
0
;i
<
quantity;i
++
){
flag
=
1000
;
p
=-
1
;
for
(j
=
0
;j
<
quantity;j
++
){
if
((queue[j].visited
==
0
)
&&
(queue[j].go
>=
current)){
if
(abs(queue[j].go
-
current)
<
flag){
p
=
j;
flag
=
abs(queue[j].go
-
current);
}
}
}
if
(p
!=-
1
){
cout
<<
queue[p].go
<<
"
"
;
total
+=
abs(queue[p].go
-
current);
current
=
queue[p].go;
queue[p].visited
=
1
;
}
else
{
for
(j
=
0
;j
<
quantity;j
++
){
if
((queue[j].visited
==
0
)
&&
(queue[j].go
<
current)){
if
(abs(queue[j].go
-
current)
<
flag){
p
=
j;
flag
=
abs(queue[j].go
-
current);
}
}
}
cout
<<
queue[p].go
<<
"
"
;
total
+=
abs(queue[p].go
-
current);
current
=
queue[p].go;
queue[p].visited
=
1
;
}
}
cout
<<
endl
<<
"
磁头移过的柱面数:
"
<<
total
<<
endl;
//
磁头初始向外
for
(i
=
0
;i
<
quantity;i
++
){
queue[i].visited
=
0
;
}
total
=
0
;
cout
<<
"
磁头初始向外的访问磁道号顺序流:
"
;
current
=
start;
for
(i
=
0
;i
<
quantity;i
++
){
flag
=
1000
;
p
=-
1
;
for
(j
=
0
;j
<
quantity;j
++
){
if
((queue[j].visited
==
0
)
&&
(queue[j].go
<=
current)){
if
(abs(queue[j].go
-
current)
<
flag){
p
=
j;
flag
=
abs(queue[j].go
-
current);
}
}
}
if
(p
!=-
1
){
cout
<<
queue[p].go
<<
"
"
;
total
+=
abs(queue[p].go
-
current);
current
=
queue[p].go;
queue[p].visited
=
1
;
}
else
{
for
(j
=
0
;j
<
quantity;j
++
){
if
((queue[j].visited
==
0
)
&&
(queue[j].go
>
current)){
if
(abs(queue[j].go
-
current)
<
flag){
p
=
j;
flag
=
abs(queue[j].go
-
current);
}
}
}
cout
<<
queue[p].go
<<
"
"
;
total
+=
abs(queue[p].go
-
current);
current
=
queue[p].go;
queue[p].visited
=
1
;
}
}
cout
<<
endl
<<
"
磁头移过的柱面数:
"
<<
total;
}
//
显示版权信息函数
void
version()
{
cout
<<
endl
<<
endl;
cout
<<
"
┏━━━━━━━━━━━━━━━━━━━━━━━┓
"
<<
endl;
cout
<<
"
┃     磁盘调度算法系统        ┃
"
<<
endl;
cout
<<
"
┠───────────────────────┨
"
<<
endl;
cout
<<
"
┃   (c)All Right Reserved Neo       ┃
"
<<
endl;
cout
<<
"
┃      sony006@163.com          ┃
"
<<
endl;
cout
<<
"
┃     version 2004 build 1122      ┃
"
<<
endl;
cout
<<
"
┗━━━━━━━━━━━━━━━━━━━━━━━┛
"
<<
endl;
cout
<<
endl
<<
endl;
}
void
main()
{
int
i;
version();
initial();
readData();
FIFO();
shortest();
for
(i
=
0
;i
<
quantity;i
++
){
queue[i].visited
=
0
;
}
elevator();
}

 

转载于:https://www.cnblogs.com/FCWORLD/archive/2010/12/04/1896361.html

你可能感兴趣的文章
一个关于vue+mysql+express的全栈项目(六)------ 聊天模型的设计
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
代码整洁
查看>>
蓝桥杯-分小组-java
查看>>
Java基础--面向对象编程1(类与对象)
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>
javascript:二叉搜索树 实现
查看>>
网络爬虫Heritrix源码分析(一) 包介绍
查看>>