关于最短剩余时间优先算法-进程调度模拟【C++】
生活随笔
收集整理的這篇文章主要介紹了
关于最短剩余时间优先算法-进程调度模拟【C++】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@TOC關于最短剩余時間優先算法-進程調度模擬
算法思想:在就緒隊列中,在已到達的進程內挑選剩余執行時間最短的進程進行一個時間單元之后暫停,若有其它新的進程添加進來需要考慮是否剩余時間最短,若有進程比暫停進程更符合算法條件,則該進程搶占CPU進行一個時間片,直到所有的進程都進行完畢。
關于如何理解該算法:該算法是從SJF出發經過延申出來的算法,首先從SJF開始理解,若SRT中沒有了時間單元(時間片),則與SJF是一致的。
struct s_pro { int PID; //進程PID int arrivetime; //到達時間 int cputime; //已執行時間 int resttime; //剩余時間// int zt; //狀態【是否結束】}; //s_ro 結構體類型需要注意的是就緒隊列使用的是可變長度數組vector .
數據結構定義
vector<s_pro>V_S; //創建私有容器 vector<s_pro>temp; //為已到達進程創建載體具體程序實現如下:
/* DATE:2019/12/10 8:40 by:LuoBin function:SRT */#include<iostream> #include<vector> #include<time.h> #include<windows.h> using namespace std; // const int key=0; //設置每過兩個時間單元,增加一個新進程 int pro_id=0; //設置進程PID int xttime=0; //記錄系統時間struct s_pro{ int PID; //進程PID int arrivetime; //到達時間 int cputime; //已執行時間 int resttime; //剩余時間 // int zt; //狀態【是否結束】 }; //s_ro 結構體類型class moni { public: moni() { cout<<"moni constructed !"<<endl; } void create_s_pro(); void SRT(); private: vector<s_pro>V_S; //創建私有容器 vector<s_pro>temp; }; void moni::create_s_pro() //創建具有搶占機制的進程 { s_pro item; item.PID=pro_id++; item.arrivetime=xttime+rand()%5+1; //進程隨機到達 item.cputime=rand()%10+1;// item.zt=1; //活躍狀態 item.resttime=item.cputime; V_S.push_back(item); } void moni::SRT() { s_pro S; int rd; cout<<"歡迎進入最短剩余時間調度算法演示系統 ! "<<endl; //初始化5個進程 for(int i=0;i<5;i++) { create_s_pro(); } cout<<"對初始化的進程進行進程信息遍歷....."<<endl<<endl; for(int j=0;j<V_S.size();j++) { cout<<"pid為: "<<V_S[j].PID<<endl; cout<<"到達時間為: "<<V_S[j].arrivetime<<endl; cout<<"預計執行時間: "<<V_S[j].cputime<<endl<<endl; } while(!V_S.empty()) { cout<<"time: "<<xttime<<"\t"; // s_pro *p; //指向resttime=0的進程,并擦除該進程 int pid; //正在進行的 進程pid int index=0; //數組下標記錄 int m; for(int j=0;j<V_S.size();j++) //就緒隊列加入[刷新]進程 { if(V_S[j].arrivetime<=xttime ) { S=V_S[j]; temp.push_back(S); } } if(!temp.empty()) { int mintime=10; // 最短剩余時間轉換中間值 for( i=0;i<temp.size();i++) // 從就緒隊列找出下次運行進程的PID,并在原隊列中進行調度 { //找出該已到達進程隊列中剩余時間最短的進程 if(temp[i].resttime<=mintime) { mintime=temp[i].resttime; m=i; } } pid=temp[m].PID; cout<<"正在運行PID為 "<<pid<<" 的進程..."<<endl; Sleep(1*1000); /*對原進程隊列進行進程調度模擬*/ for(i=0;i<V_S.size();i++) { if(V_S[i].PID==pid) { index=i; } } V_S[index].resttime--; //選中的進程剩余時間-1 cout<<"當前進程的剩余執行時間為: "<<V_S[index].resttime<<endl; xttime++; //系統時間加一 if(V_S[index].resttime==0) { cout<<"PID為"<<V_S[index].PID<<"的進程已運行完畢! "<<endl; cout<<"======================正在從就緒隊列中刪除..."<<endl; V_S.erase(V_S.begin() + index); } rd=rand()%10+1; if(rd>9) { create_s_pro(); cout<<"新加入的進程的pid為: "<<V_S.back().PID<<endl; cout<<"新加入的進程的到達時間: "<<V_S.back().arrivetime<<endl; cout<<"新加入的進程的預計執行時間為: "<<V_S.back().cputime<<endl;cout<<endl; } cout<<"--------------------"<<endl; }else { cout<<"當前時間無進程正在運行..."<<endl<<endl; xttime++; Sleep(2*1000); }temp.clear(); }} void main() { moni D;D.SRT(); }初始化進程頁面:
vsddfagdgfdsg
所有進程結束界面:
總結:在程序的編寫過程中,碰到了很多問題,比如應該設置一個多大的時間片,以及在已到達進程的隊列每次傳遞一個元素之后忘記清空該數組,導致在查錯上花了很多時間。
總結
以上是生活随笔為你收集整理的关于最短剩余时间优先算法-进程调度模拟【C++】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python requests下载超大
- 下一篇: Maya 2018 Qt Designe