新聞中心
操作系統(tǒng)基本調(diào)度算法,高響應(yīng)比算法。先來先服務(wù)和短作業(yè)優(yōu)先策略都很可能會(huì)引起進(jìn)程的饑餓現(xiàn)象,而高響應(yīng)比算法在每次從就緒隊(duì)列選擇進(jìn)程執(zhí)行時(shí),會(huì)計(jì)算各個(gè)進(jìn)程的響應(yīng)比,選出一個(gè)響應(yīng)比最高的進(jìn)程執(zhí)行,響應(yīng)比計(jì)算如下 :(等待時(shí)間+服務(wù)時(shí)間) / 服務(wù)時(shí)間
這樣的策略兼顧提高系統(tǒng)吞吐率與減少進(jìn)程饑餓現(xiàn)象,當(dāng)進(jìn)程等待的越久,響應(yīng)比越高,被執(zhí)行的概率久越大,而服務(wù)時(shí)間要求短的進(jìn)程本身具有較高響應(yīng)比
定義pcb,作業(yè)完成隊(duì)列。
輸入進(jìn)程信息,將所有進(jìn)程按到達(dá)時(shí)間升序排序。
設(shè)置pcb數(shù)組存放已到達(dá)進(jìn)程,當(dāng)前時(shí)間current_time,下一個(gè) 要運(yùn)行進(jìn)程的序號(hào)next_process。
循環(huán)(如果還有進(jìn)程未到達(dá)或未完成)
{
?循環(huán)(還有進(jìn)程未到達(dá))
?{ 將當(dāng)前時(shí)間到達(dá)的進(jìn)程加到pcb數(shù)組
?}
?更新各進(jìn)程響應(yīng)比
?遍歷已到達(dá)進(jìn)程,找到下一個(gè)執(zhí)行的進(jìn)程序號(hào)(響應(yīng)比最高)
?執(zhí)行進(jìn)程,修改信息,插入完成隊(duì)列
?將完成了的進(jìn)程與pcb數(shù)組最后一個(gè)進(jìn)程調(diào)換,到達(dá)進(jìn)程數(shù)-1(相當(dāng)于刪除)
}
代碼實(shí)現(xiàn)如下
#include#define N 5 //進(jìn)程數(shù)
#include#includeusing namespace std;
struct pcb
{
char process_name[10]; //進(jìn)程名字
int arrive_time; //到達(dá)時(shí)間
int service_time; //需要服務(wù)時(shí)間
float response; //響應(yīng)比
int complete_time; //完成時(shí)間
int round_time; //周轉(zhuǎn)時(shí)間
float force_round_time; //帶權(quán)周轉(zhuǎn)時(shí)間
char state; //進(jìn)程狀態(tài)
}PCB[N + 1]; //0號(hào)單元不存數(shù)據(jù),直接用作交換
queueFinish_queue; //已完成隊(duì)列
void input(int n)
{
cout<< "\n請(qǐng)輸入各進(jìn)程的信息\n"<< endl;
for (int i = 1; i<= n; i++) //輸入各進(jìn)程信息
{
PCB[i].state = 'c'; //coming (未到達(dá))
cout<< "------\n請(qǐng)輸入第"<< i<< "進(jìn)程名字: ";
cin >>PCB[i].process_name;
cout<< "請(qǐng)輸入到達(dá)時(shí)間: ";
cin >>PCB[i].arrive_time;
cout<< "請(qǐng)輸入服務(wù)時(shí)間: ";
cin >>PCB[i].service_time;
PCB[i].response = 1;
}
}
void sort(int n)
{
int i, j;
for (i = 1; i< n; i++) //按到達(dá)時(shí)間升序
{
for (j = 1; j<= n - i; j++)
{
if (PCB[j].arrive_time >PCB[j + 1].arrive_time)
{
PCB[0] = PCB[j];
PCB[j] = PCB[j + 1];
PCB[j + 1] = PCB[0];
}
}
}
}
void high_response(int n)
{
int current_time = 0; //系統(tǒng)時(shí)間
int i,
next_process, //下一個(gè)要運(yùn)行進(jìn)程的序號(hào)
arrived_process_num = 0, //已到達(dá)進(jìn)程數(shù)
wait_process_num = 0; //正在等待的進(jìn)程數(shù)
pcb wait_process_array[N + 1]; //存放已到達(dá)的進(jìn)程
while (arrived_process_num< n || wait_process_num >0) //還有進(jìn)程未到達(dá)或未運(yùn)行
{ //如果還有進(jìn)程未到達(dá),將當(dāng)前時(shí)間所有到達(dá)的進(jìn)程加入等待數(shù)組
while (arrived_process_num< n && current_time >= PCB[arrived_process_num + 1].arrive_time)
{
wait_process_array[++wait_process_num] = PCB[++arrived_process_num];
}
for (i = 1; i<= wait_process_num; i++)
{
wait_process_array[i].response = //更新響應(yīng)比
1.0*(current_time - wait_process_array[i].arrive_time + wait_process_array[i].service_time) / wait_process_array[i].service_time;
}
next_process = 1;
for (i = 2; i<= wait_process_num; i++)
{
if (wait_process_array[i].response >wait_process_array[next_process].response)
{
next_process = i; //找到下一個(gè)運(yùn)行進(jìn)程的序號(hào)
}
}
{
current_time += wait_process_array[next_process].service_time; //運(yùn)行結(jié)束后的時(shí)間
wait_process_array[next_process].complete_time = current_time; //進(jìn)程完成時(shí)間等于現(xiàn)在時(shí)間
wait_process_array[next_process].round_time = current_time - wait_process_array[next_process].arrive_time; //周轉(zhuǎn)時(shí)間
wait_process_array[next_process].force_round_time = 1.0*wait_process_array[next_process].round_time / wait_process_array[next_process].service_time; //帶權(quán)周轉(zhuǎn)時(shí)間
wait_process_array[next_process].state = 'f'; //狀態(tài)改為完成
wait_process_num--; //正在等待的進(jìn)程數(shù)減一
Finish_queue.push(wait_process_array[next_process]); //插入完成隊(duì)列
cout<< "\n第"<< Finish_queue.size()<< "次調(diào)度進(jìn)程: "<< Finish_queue.back().process_name<< endl;
}
{ //將完成了的進(jìn)程換到等待進(jìn)程數(shù)組的下一個(gè)位置(相當(dāng)于刪除)
wait_process_array[0] = wait_process_array[next_process];
wait_process_array[next_process] = wait_process_array[wait_process_num + 1];
wait_process_array[wait_process_num + 1] = wait_process_array[0];
}
}
return;
}
void print(int n)
{
int i = 1;
float sum = 0; //存放各進(jìn)程的帶權(quán)周轉(zhuǎn)時(shí)間和
cout<< "\n 進(jìn)程 |"<< "到達(dá)時(shí)間 |"<< " 服務(wù)時(shí)間 |"<< " 完成時(shí)間 |"<< " 開始運(yùn)行時(shí)的響應(yīng)比 | "<< " 周轉(zhuǎn)時(shí)間 |"<< " 帶權(quán)周轉(zhuǎn)時(shí)間"<< endl;
while (Finish_queue.empty() == false)
{
cout<< Finish_queue.front().process_name
<< "\t| "<< Finish_queue.front().arrive_time
<< "\t | "<< Finish_queue.front().service_time<< " \t | "<< Finish_queue.front().complete_time
<< "\t | "<< setprecision(3)<< Finish_queue.front().response
<< "\t\t | "<< Finish_queue.front().round_time
<< "\t | "<< Finish_queue.front().force_round_time
<< endl;
sum += Finish_queue.front().force_round_time;
Finish_queue.pop();
}
cout<< "\n\n系統(tǒng)平均帶權(quán)周轉(zhuǎn)時(shí)間: "<< (sum / n)<< endl;
}
int main()
{
input(N);
sort(N);
high_response(N);
print(N);
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
文章標(biāo)題:高響應(yīng)比優(yōu)先算法(c++)-創(chuàng)新互聯(lián)
文章URL:http://fisionsoft.com.cn/article/pdsgs.html