操作系统最短寻道时间优先(SSTF)和扫描(SCAN)算法

操作系统 同时被 3 个专栏收录
2 篇文章 0 订阅
2 篇文章 0 订阅
7 篇文章 0 订阅

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

实验目的:

通过对磁盘调度算法的设计,深入理解提高磁盘访问速度的原理。

实验内容:

模拟实现磁盘调度算法:最短寻道时间优先(SSTF)和扫描(SCAN)算法。

实验步骤:

理解各调度算法的工作原理

对给出的任意的磁盘请求序列、计算平均寻道长度;要求可定制磁盘请求序列长度、磁头起始位置、磁头移动方向。

测试:假设磁盘访问序列:98,183,37,122,14,124,65,67;读写头起始位置:53,方向:磁道增加的方向。输入此类数据后,程序按照选定的算法,自动给出访问序列,并且算出经过的磁道总数。

详细代码请看:https://download.csdn.net/download/qq_38971487/10887445

头文件:



#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:
	Listnode *head;


};

void TrackList::sortList(int n){ // @suppress("Member declaration not found")
	int temp;
	Listnode *q,*p;
	p = head->next;
	while(p != head){
		q = p->next;
		while(q != head){
			if((p->data) > (q->data)){
				temp = p->data;
				p->data = q->data;
				q->data = temp;
			}
			q = q->next;
		}
		p = p->next;
	}
	cout<<"排序完成!"<<endl;
}

void TrackList::putList(){ // @suppress("Member declaration not found")
	Listnode* currentNode = head->next;
	cout<<"链表顺序为:"<<endl;
	while(currentNode != head){//循环输出element
		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;
	p = head->next;
	while(p->data <= data){//找到data所在的范围,p指向比data大的那一节点
		p = p->next;
		if(p == head)
			break;
	}
	if (p == head)
		p = head->pre;
	else if(p != head){
		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;
		while (p != head) {
			cout << p->data << "  ";
			distance = (float) fabs(data - (p->data));
			data = p->data;
			sum += distance;
			cout << distance << endl;
			p = p->next;
		}
		while (q != head) {
			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;
		while(q != head){
			cout<<q->data<<"  ";
			distance = fabs((q->data) - data);
			cout<<distance<<endl;
			data = q->data;
			sum += distance;
			q = q->pre;
		}
		while(p != head){
			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;
	p = head->next;
	while (p->data <= data) { //找到data所在的范围,p指向比data大的那一节点
		p = p->next;
		if(p == head)
			break;
	}
	if(p == head)
		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;
		}
		if (p == head) {
			p = head->next;
			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
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值