移臂调度算法c语言,磁盘移臂调度算法实验
操作系統實驗報告
實驗題目:實驗八:磁盤移臂調度算法實驗
軟件環境:Linux操作系統
實驗目的:加深對于操作系統設備管理技術的了解,體驗磁盤移臂調度算法的重要性;掌握幾種重要的磁盤移臂調度算法,練習模擬算法的編程技巧,鍛煉研究分析試驗數據的能力。
實驗內容:請在示例實驗程序中補充SCAN,C-SCAN,LOOK磁盤移臂調度算法的模擬程序。輸入不同的磁盤柱面請求序列,觀察和分析其調度效果和性能,并將其與FCFS和SSTF算法進行比較。改進以上示例實驗程序,使之能夠隨機的產生磁盤柱面請求序列,以便能動態的觀測各種調度算法的性能。
實驗思路:首先明確了SCAN,C-SCAN,LOOK磁盤移臂調度算法的思想,明確需要完成的任務即給出尋道序列,并根據方向轉換次數和尋到數分析算法的優劣并比較。其次,結合私立程序給出的SSTF算法和SCAN算法,寫出SCAN,C-SCAN,LOOK算法,并添加到示例程序中。編譯后執行,多次磁盤請求序列的測試,查看實驗結果是否正確。
實驗代碼://dask.h
#include
#include
#include
#include?
#include?
using namespace std;
class DiskArm{
public:
DiskArm();
~DiskArm();
void InitSpace(char * MethodName); //初始化尋道記錄
void Report(void); // 報告算法執行情況
void Fcfs(void); //先來先服務算法
void Sstf(void); //最短尋道時間優先算法
void Scan(void); //電梯調度算法
void CScan(void); //均勻電梯調度算法
void Look(void); //LOOK 調度算法
private:
int *Request ;
//磁盤請求道號
int *Cylinder;
//工作柱面道號號
int RequestNumber;
//磁盤請求數
int CurrentCylinder;
//當前道號
int SeekDirection;
//磁頭方向
int SeekNumber;
//移臂總數
int SeekChang;
//磁頭調頭數
};
//dask.c
#include "dask.h"
DiskArm::DiskArm(){
int
i;
//輸入當前道號
cout
<< "Please input Current cylinder :"
;
cin
>> CurrentCylinder;
//磁頭方向,輸入 0 表示向小道號移動,1 表示向大道號移動
cout
<< "Please input Current Direction
(0/1) :" ;
cin
>> SeekDirection;
//輸入磁盤請求數,請求道號
cout
<< "Please input Request Numbers :"
;
cin
>> RequestNumber;
cout
<< "Please input Request cylinder
string :";
Request =
new int[sizeof(int) * RequestNumber];
Cylinder
= new int[sizeof(int) * RequestNumber];
for (i =
0; i < RequestNumber; i++)
cin
>> Request[i];
}
DiskArm::~DiskArm(){
}
//初始化道號,尋道記錄
void DiskArm::InitSpace(char * MethodName)
{
int
i;
cout
<< endl
<< MethodName
<< endl;
SeekNumber = 0;
SeekChang
= 0;
for (i =
0; i < RequestNumber; i++)
Cylinder[i] = Request[i];
}
// 統計報告算法執行情況
void DiskArm::Report(void){
cout
<< endl;
cout
<< "Seek Number: "
<< SeekNumber
<< endl;
cout
<< "Chang Direction: "
<< SeekChang
<< endl
<< endl;
}
//先來先服務算法
void DiskArm::Fcfs(void)
{
int
Current = CurrentCylinder;
int
Direction = SeekDirection;
InitSpace("FCFS");
cout
<< Current;
for(int
i=0; i
if(((Cylinder[i] >= Current)
&& !Direction)||((Cylinder[i]
< Current) &&
Direction)){
//需要調頭
SeekChang++; //調頭數加 1
Direction = !Direction ; //改變方向標志
//報告當前響應的道號
cout << endl
<< Current
<< " -> "
<< Cylinder[i];
}
else //不需調頭,報告當前響應的道號
cout << " -> "
<< Cylinder[i] ;
//累計尋道數,響應過的道號變為當前道號
SeekNumber += abs(Current -Cylinder[i]);
Current = Cylinder[i];
}
//報告磁盤移臂調度的情況
Report();
}
void DiskArm::Sstf(void)
{
int
Shortest;
int
Distance = 999999 ;
int
Direction = SeekDirection;
int
Current = CurrentCylinder;
InitSpace("SSTF");
cout
<< Current;
for(int
i=0; i
//查找當前最近道號
for(int j=0; j
if(Cylinder[j] == -1) continue; //-1 表示已經響應過了
if(Distance > abs(Current-Cylinder[j])){
//到下一道號比當前距離近,下一道號為當前距離
Distance = abs(Current-Cylinder[j]);
Shortest = j;
}
}
if((( Cylinder[Shortest] >= Current)
&& !Direction)||((
Cylinder[Shortest] < CurrentCylinder)
&& Direction)){
//需要調頭
SeekChang++; //調頭數加 1
Direction = !Direction ; //改變方向標志
//報告當前響應的道號
cout << endl
<< Current
<< " -> "
<< Cylinder[Shortest];
}
else //不需調頭,報告當前響應的道號
cout << " -> "
<< Cylinder[Shortest] ;
//累計尋道數,響應過的道號變為當前道號
SeekNumber += abs(Current -Cylinder[Shortest]);
Current = Cylinder[Shortest];
//恢復最近距離,銷去響應過的道號
Distance = 999999;
Cylinder[Shortest] = -1;
}
Report();
}
//電梯調度算法
void DiskArm::Scan(void){
int
Current = CurrentCylinder;
int
Direction = SeekDirection;
InitSpace("SCAN");
cout
<< Current;
for(int
i=0; i
int index=-1;
int Distance = 999999;
for(int j=0;j
if(Cylinder[j]==-1)
continue;
else
if((Direction==0&&Cylinder[j]
||(Direction==1&&Cylinder[j]>Current&&(Cylinder[j]-Current)
index=j;
Distance=abs(Current-Cylinder[j]);
}
}
if(Direction==0){
if(index!=-1){
cout<"<
SeekNumber+=Current-Cylinder[index];
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
cout<"<<0<
SeekNumber+=Current;
Direction=!Direction;
//cout<<0;
Current=0;
SeekChang++;
i--;
}
}
else if(Direction==1){
if(index!=-1){
cout<"<
SeekNumber+=Cylinder[index]-Current;
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
cout<"<<199<
SeekNumber+=199-Current;
Direction=!Direction;
//cout<<199;
Current=199;
SeekChang++;
i--;
}
}
}
//報告磁盤移臂調度的情況
Report();
}
//均勻電梯調度算法
void DiskArm::Look(void){
int
Current = CurrentCylinder;
int
Direction = SeekDirection;
InitSpace("Look");
cout
<< Current;
for(int
i=0; i
int index=-1;
int Distance = 999999;
for(int j=0;j
if(Cylinder[j]==-1)
continue;
else
if((Direction==0&&Cylinder[j]
||(Direction==1&&Cylinder[j]>Current&&(Cylinder[j]-Current)
index=j;
Distance=abs(Current-Cylinder[j]);
}
}
if(Direction==0){
if(index!=-1){
cout<"<
SeekNumber+=Current-Cylinder[index];
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
//cout<
Direction=!Direction;
SeekChang++;
i--;
}
}
else if(Direction==1){
if(index!=-1){
cout<"<
SeekNumber+=Cylinder[index]-Current;
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
//cout<
Direction=!Direction;
SeekChang++;
i--;
}
}
}
//報告磁盤移臂調度的情況
Report();
}
//LOOK 調度算法
void DiskArm::CScan(void)
{
int Current = CurrentCylinder;
int
Direction = SeekDirection;
InitSpace("CScan");
cout
<< Current;
for(int
i=0; i
int index=-1;
int Distance = 999999;
for(int j=0;j
if(Cylinder[j]==-1)
continue;
else
if((Direction==0&&Cylinder[j]
||(Direction==1&&Cylinder[j]>Current&&(Cylinder[j]-Current)
index=j;
Distance=abs(Current-Cylinder[j]);
}
}
if(Direction==0){
if(index!=-1){
cout<"<
SeekNumber+=Current-Cylinder[index];
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
cout<"<<0<
SeekNumber+=Current;
Current=199;
cout<199";
SeekChang++;
i--;
}
}
else if(Direction==1){
if(index!=-1){
cout<"<
SeekNumber+=Cylinder[index]-Current;
Current=Cylinder[index];
Cylinder[index]=-1;
}else{
cout<"<<199<
SeekNumber+=199-Current;
Current=0;
SeekChang++;
i--;
}
}
}
Report();
}
//程序啟動入口
int main(int argc,char *argv[]){
//建立磁盤移臂調度類
DiskArm *dask = new DiskArm();
//比較和分析 FCFS 和 SSTF 兩種調度算法的性能
dask->Fcfs();
dask->Sstf();
dask->Scan();
dask->CScan();
dask->Look();
}
總結
以上是生活随笔為你收集整理的移臂调度算法c语言,磁盘移臂调度算法实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安全漏洞中的倚天剑——XSS跨站脚本攻击
- 下一篇: suma++ TensorRT Not