2 篇文章 0 订阅
2 篇文章 0 订阅
7 篇文章 0 订阅

山东建筑大学操作系统实验三、磁盘调度算法的设计



#ifndef SCAN_H_
#define SCAN_H_

#include<iostream>

#include <iterator>
#include <algorithm>

#include<sstream>
#include<string>
#include<math.h>

using namespace std;

struct Listnode{//结构项列表
int data;
Listnode *pre,*next;

Listnode(){pre = NULL;next = NULL;} // @suppress("Class members should be properly initialized")
Listnode(int data){
this->data = data;
pre = NULL;next = NULL;
}
Listnode(int data,Listnode *pre,Listnode *next){
this->data = data;
this->pre = pre;
this->next = next;
}
};

class TrackList{
public:
TrackList(int initialCapacity=10);
~TrackList();
void insertList(int n);
void sortList(int n);
void putList();
void SCANvisitList(int n,int data);
void SSTFvisitList(int n,int data);

protected:

};

int temp;
Listnode *q,*p;
q = p->next;
if((p->data) > (q->data)){
temp = p->data;
p->data = q->data;
q->data = temp;
}
q = q->next;
}
p = p->next;
}
cout<<"排序完成！"<<endl;
}

cout<<"链表顺序为："<<endl;
cout<< currentNode->data<<" ";
currentNode = currentNode->next;
}
cout<<endl;
}
//扫描（SCAN）算法
void TrackList::SCANvisitList(int n,int data){
Listnode *p,*q;
float distance = 0;
float sum = 0;
float ave = 0;
while(p->data <= data){//找到data所在的范围，p指向比data大的那一节点
p = p->next;
break;
}
int a = fabs(data - p->pre->data); //与前面节点的绝对值
int b = fabs(data - p->data); //与后面节点的绝对值
if (a < b)
p = p->pre;
}
int i;
cout<<"请输入磁道移动方向（向外1，向内0）"<<endl;
cin>>i;
if (i == 1) {
cout << "先向外后向内访问：" << endl;
cout << "将要被访问的磁道号\t本次移动的距离\n";
q = p->pre;
cout << p->data << "  ";
distance = (float) fabs(data - (p->data));
data = p->data;
sum += distance;
cout << distance << endl;
p = p->next;
}
cout << q->data << "  ";
distance = (float) fabs((q->data) - data);
data = q->data;
sum += distance;
cout << distance << endl;
q = q->pre;
}
ave = sum / n;
cout << "平均寻道长度为：" << ave << endl;
}
if(i == 0){
cout<<"先向内后向外访问："<<endl;
cout<<"将要被访问的磁道号\t本次移动的距离\n"<<endl;
q = p;
p = p->next;
cout<<q->data<<"  ";
distance = fabs((q->data) - data);
cout<<distance<<endl;
data = q->data;
sum += distance;
q = q->pre;
}
cout<<p->data<<"  ";
distance = (float)fabs( data - (p->data) );
cout<<distance<<endl;
data = p->data;
sum += distance;
p = p->next;
}
ave = sum / n;
cout << "平均寻道长度为：" << ave << endl;
}
}
//最短寻道时间优先（SSTF）
void TrackList::SSTFvisitList(int n,int data){
Listnode *p;
float distance = 0;
float sum = 0;
float ave = 0;
int a,b,i;
while (p->data <= data) { //找到data所在的范围，p指向比data大的那一节点
p = p->next;
break;
}
p = p->pre;
else {
a = fabs(data - p->pre->data); //与前面节点的绝对值
b = fabs(data - p->data); //与后面节点的绝对值
if (a < b) //比较得到离磁头最近的磁道号
p = p->pre;
}
cout<<"将要被访问的磁道号\t本次移动的距离"<<endl;
for(i = 0;i < n;++i){
cout<<p->data<<"  ";
distance = fabs(data - p->data);
cout<<distance<<endl;
data = p->data;
sum += distance;
Listnode *q;
q = p;
//比较绝对值，判断哪个是离当前磁头最近的磁道
if(fabs(data - p->pre->data) < fabs(data - p->next->data)){
p = p->pre;
p->next = q->next;
q->next->pre = p;
}
else if(fabs(data - p->pre->data) >= fabs(data - p->next->data)){
p = p->next;
p->pre = q->pre;
q->pre->next = p;
}
while (p->data <= data) { //找到data所在的范围，p指向比data大的那一节点
p = p->next;
}
a = fabs(data - p->pre->data); //与前面节点的绝对值
b = fabs(data - p->data); //与后面节点的绝对值
if (a < b) //比较得到离磁头最近的磁道号
p = p->pre;
}
delete q;//删除访问过的节点
}
ave = sum/n;
cout << "平均寻道长度为：" << ave << endl;
}

#endif /* SCAN_H_ */


test.cpp:

/*
* test.cpp
*
*  Created on: 2018年12月16日
*      Author: 小伙
*/

#include "SCAN.h"

int main()
{
int n;
cout<<"请输入进程要访问的磁道总数："<<endl;
cin>>n;
TrackList y(n);
y.insertList(n);
//y.putList();
y.sortList(n);
//y.putList();
int m;
cout<<"请输入磁头的起始位置："<<endl;
cin>>m;
int choose;
cout<<"请选择磁盘调度算法（SSTF：0，SCAN：1）"<<endl;
cin>>choose;
if(choose == 0)
y.SSTFvisitList(n,m);
if(choose == 1)
y.SCANvisitList(n,m);
return 0;
}



• 1
点赞
• 0
评论
• 9
收藏
• 一键三连
• 扫一扫，分享海报

01-27
12-01 692
12-24 741
09-17 7034
06-10 3724
01-21 7491
01-30
10-19 3万+
02-15 308
08-27 3503
06-21 1万+
09-30 3056