#include<iostream> #include<algorithm> #include<ctime> #include<cstdlib> using namespace std; class Core{ public: int* IOtable;//IO表 int Nums; int CiDaoHao;//當前磁道號 int Direct;//當前磁臂方向 1表示磁道增長 0表示磁道遞減 int PID;//當前進程 int ToCiDao;//要訪問的磁道號 Core(int Nums){ this->Nums=Nums; this->IOtable =new int[Nums]; srand(time(NULL)); for(int i=0;i<this->Nums;i++){ IOtable[i] = rand()%200; } this->Direct = 1; cout<<" 當前磁道方向初始化爲增長方向"<<endl; this->PID = 0; cout<<" 當前進程爲0"<<endl; this->CiDaoHao = 100; cout<<" 當前磁道號爲 100"<<endl; } ~Core(){ cout<<" 磁盤調度結束"; } void print(){ cout<<" IO表以下所示:"<<endl; cout<<" 進程名\t"<<"| 要求訪問的磁道"<<endl; for(int i=0;i<this->Nums;i++){ cout<<" "<<i<<"\t"<<" |"<<this->IOtable[i]<<endl; } } void SCAN(){ cout<<" 執行磁盤調度SCAN算法"<<endl; sort(this->IOtable,this->IOtable+this->Nums); cout<<" 當前磁道號爲 "<<this->CiDaoHao<<endl; cout<<" 被訪問的下一個磁道號 | 移動距離(磁道數)"<<endl; int sign = 0; int reCiDaoHao = this->CiDaoHao; int temp = 0; do{ if(this->Direct==1){ for(int i=0;i<this->Nums;i++){ if(this->IOtable[i]>=reCiDaoHao){ cout<<IOtable[i]<<"\t |"<<IOtable[i]-this->CiDaoHao<<endl; temp+=(IOtable[i]-this->CiDaoHao); this->CiDaoHao=IOtable[i]; sign++; } } this->Direct=0; } if(this->Direct==0){ for(int i=this->Nums-1;i>=0;i--){ if(this->IOtable[i]<reCiDaoHao){ cout<<IOtable[i]<<"\t |"<<this->CiDaoHao-IOtable[i]<<endl; temp+=(this->CiDaoHao-IOtable[i]); this->CiDaoHao=IOtable[i]; sign++; } } this->Direct=1; } }while(sign<this->Nums); temp/=this->Nums; cout<<" 平均尋道長度="<<temp<<endl; } void Request(){ cout<<" 接受請求"<<endl; srand(time(NULL)); for(int i=0;i<this->Nums;i++){ this->IOtable[i] = rand()%200; } print(); } }; int main(){ Core* core = new Core(8); core->print(); srand(time(NULL)); char check; do{ if(rand()%10>5) core->SCAN(); else core->Request(); cout<<" 是否繼續? 繼續按0 退出按1"<<endl; cin>>check; }while(check!=1); delete core; }
運行結果以下所示:ios