新聞中心
- 一、前言
- 二、實驗簡介和算法流程圖
- 三、算法實現(xiàn)思路
- 四、實驗源碼
- 五、實驗結(jié)果截圖
參考文章:
- 1、https://blog.csdn.net/weixin_44949135/article/details/116539292
- 2、https://blog.csdn.net/qq_40159978/article/details/90934681
- 3、http://c.biancheng.net/view/480.html
輕微強迫癥最后為了對齊每個數(shù)據(jù)的格式,調(diào)了好多次才非常整齊!本文主要參考文章2完成了操作系統(tǒng)實驗最高優(yōu)先級調(diào)度算法
,參考文章3學習了priority_queue
容器的用法,擴展內(nèi)容為參考文章1其他幾種調(diào)度算法的學習。
動態(tài)最高優(yōu)先級調(diào)度算法是指在進程創(chuàng)建時先確定一個初始優(yōu)先數(shù), 以后在進程運行中隨著進程特性的改變不斷修改優(yōu)先數(shù),這樣,由于開始優(yōu)先數(shù)很低而得不到CPU的進程,就能因為等待時間的增長而優(yōu)先數(shù)變?yōu)樽罡叨玫紺PU運行。
算法流程圖:
priority_queue 容器適配器定義了一個元素有序排列的隊列。默認隊列頭部的元素優(yōu)先級最高。因為它是一個隊列,所以只能訪問第一個元素,這也意味著優(yōu)先級最高的元素總是第一個被處理。
這里我們就利用這個容器就能非常容易完成實驗,
首先,先初始化pcb,獲取輸入的進程名、優(yōu)先級、運行時間后加入隊列中,根據(jù)重載運算符的規(guī)則自動排序,權(quán)值大的優(yōu)先,如果權(quán)值一樣,時間短的優(yōu)先,每次push都會有一次排序的操作;
然后,就是不斷的出隊頂端進程運算,如果運行時間為0即運行完畢不再入隊否則繼續(xù)入隊運行;
最后,所有進程運行完畢退出!其實也就是算法流程圖的具體實現(xiàn)。
#include#include#includeusing namespace std;
typedef struct pcb {string pName;//進程名
int priorityNumber;//優(yōu)先數(shù)
float needTime;//估計服務時間
float runTime;//已運行時間
char state;//進程狀態(tài) W等待 R運行 D結(jié)束
friend bool operator<(pcb a, pcb b){if (a.priorityNumber == b.priorityNumber)
return a.needTime >b.needTime; //時間小的優(yōu)先
return a.priorityNumber< b.priorityNumber;//權(quán)值大的優(yōu)先
}
}PCB;
priority_queuewaitList;//就緒隊列
int n;//進程個數(shù)
void init_pcb()//初始化pcb,輸入進程信息
{printf("\n\n\n\t\t\t--------------------\n");
printf("\t\t\t|最高優(yōu)先級調(diào)度算法|\n");
printf("\t\t\t| 作者:q_bing |\n");
printf("\t\t\t| 2022年11月29日 |\n");
printf("\t\t\t--------------------\n");
cout<< "請輸入進程的個數(shù):";
cin >>n;
PCB r;//臨時工作結(jié)點
for (int i = 0; icout<< "請輸入第"<< i + 1<< "個進程的名字、優(yōu)先數(shù)、服務時間(例如:A 12 8 ):";
cin >>r.pName;
cin >>r.priorityNumber;
cin >>r.needTime;
r.runTime = 0;
r.state = 'W';
waitList.push(r);
}
cout<< endl;
}
void showProcess(priority_queuewaitList) //顯示進程信息
{PCB s;//臨時工作結(jié)點
cout<< "進程名\t|優(yōu)先數(shù) |服務時間|已運行時間|"<< endl;
while (waitList.size() != 0) {s = waitList.top();
cout<< s.pName<< "\t|"<< s.priorityNumber<< "\t|"<< s.needTime<< "\t |"<< s.runTime<< "\t |"<< endl;
waitList.pop();
}
cout<< endl;
}
void runProcess(priority_queue&waitList) {//運行進程
PCB s;
while(waitList.size()!=0){s = waitList.top();
waitList.pop();
cout<< "正在運行的進程"<< endl;
cout<< "進程名\t|優(yōu)先數(shù) |服務時間|已運行時間|"<< endl;//輸出當前進程
cout<< s.pName<< "\t|"<< s.priorityNumber<< "\t|"<< s.needTime<< "\t |"<< s.runTime<< "\t |"<< endl;
s.priorityNumber--;//優(yōu)先數(shù)-1
s.runTime++;//已運行時間+1
s.needTime--;//還需要時間-1
if (s.needTime == 0) { s.state = 'D';
cout<<"已完成進程"<<"---------------------------------------->"< cout<<"\n所有進程運行完畢!"<init_pcb();
showProcess(waitList);
runProcess(waitList);
system("pause");
return 0;
}
在參考程序輸入測試數(shù)據(jù)
001 6 3
003 6 2
002 5 4
005 4 4
004 3 4
應該得到的結(jié)果順序
003
001
002
005
004
本程序驗證的運行數(shù)據(jù)
003
001
002
005
004
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當前標題:操作系統(tǒng)實驗動態(tài)最高優(yōu)先級調(diào)度算法(C++實現(xiàn))-創(chuàng)新互聯(lián)
文章起源:http://www.ef60e0e.cn/article/dgdoco.html