一、輪轉(zhuǎn)調(diào)度算法 RR 代碼一個(gè)班(20信安)的同學(xué)搜到這篇?jiǎng)e直接copy我的,代碼僅供參考
公司主營業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出蕉城免費(fèi)做網(wǎng)站回饋大家。
#includeusing namespace std;
// 總時(shí)間
int clk_t = 0;
// 額外記錄的量
float ave_aroundtime=0; // 平均周轉(zhuǎn)時(shí)間
float ave_weight_aroundtime=0; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
//pcb結(jié)構(gòu)體
struct pcb{int pno;// 進(jìn)程號
char state = 'W';// 狀態(tài),就緒W,運(yùn)行R
int arrivetime;// 到達(dá)時(shí)間
int runtime = 0;// 已經(jīng)運(yùn)行的時(shí)間
int needtime;// 服務(wù)時(shí)間
int achievetime;// 完成時(shí)間
int aroundtime;// 周轉(zhuǎn)時(shí)間
float weight_aroundtime;// 帶權(quán)周轉(zhuǎn)時(shí)間
int waittime;// 等待時(shí)間
}pro[10];
// 創(chuàng)建進(jìn)程就緒隊(duì)列
vectorprocess_que;
// 進(jìn)程數(shù)
int n;
// 時(shí)間片長度
int age;
// 完成進(jìn)程標(biāo)志數(shù)組
int hash_pro[10]={0};
int flag=0;
//聲明
void init_print();
void run_print();
void rightmove();
void init_print(){cout<< "\n##################當(dāng)前初始化進(jìn)程###################"<< endl;
cout<< "name \t state \t needtime runtime"<< endl;
for(int i=0; icout<< "|"<< pro[i].pno<< "\t";
cout<< "|"<< pro[i].state<< "\t";
cout<< "|"<< pro[i].needtime<< "\t";
cout<< "|"<< pro[i].runtime<< "\t"<< endl;
}
cout<< "################################################\n"<< endl;
}
void run_print(){cout<< "\n##################當(dāng)前執(zhí)行進(jìn)程###################"<< endl;
cout<< "name \t state \t needtime runtime "<< endl;
if(!process_que.empty()){cout<< "|"<< process_que[0].pno<< "\t";
cout<< "|"<< process_que[0].state<< "\t";
cout<< "|"<< process_que[0].needtime<< "\t";
cout<< "|"<< process_que[0].runtime<< "\t";
} else {cout<< "process_queue is none now!";
}
cout<< endl;
for(int i=1; icout<< "|"<< process_que[i].pno<< "\t";
cout<< "|"<< process_que[i].state<< "\t";
cout<< "|"<< process_que[i].needtime<< "\t";
cout<< "|"<< process_que[i].runtime<< "\t"<< endl;
}
cout<< "################################################\n"<< endl;
}
void end_print(){cout<< "\n##################進(jìn)程完成總覽###################"<< endl;
cout<< "name \t state \t arrivetime \t needtime \t achievetime \t aroundtime \t weight_aroundtime \t waittime"<< endl;
if(!process_que.empty()){cout<< "|"<< process_que[0].pno<< "\t";
cout<< "|"<< process_que[0].state<< "\t";
cout<< "|"<< process_que[0].arrivetime<< "\t";
cout<< "|"<< process_que[0].needtime<< "\t";
cout<< "|"<< process_que[0].achievetime<< "\t";
cout<< "|"<< process_que[0].aroundtime<< "\t";
cout<< "|"<< process_que[0].weight_aroundtime<< "\t";
cout<< "|"<< process_que[0].waittime<< "\t";
} else {cout<< "process_queue is none now!";
}
cout<< endl;
for(int i=0; icout<< "|"<< pro[i].pno<< "\t";
cout<< "|"<< pro[i].state<< "\t";
cout<< "|"<< pro[i].arrivetime<< "\t\t";
cout<< "|"<< pro[i].needtime<< "\t\t";
cout<< "|"<< pro[i].achievetime<< "\t\t";
cout<< "|"<< pro[i].aroundtime<< "\t\t";
cout<< "|"<< pro[i].weight_aroundtime<< "\t\t\t";
cout<< "|"<< pro[i].waittime<< "\t"<< endl;//3 20 1 30 50 2 40 30 3 50 100
}
cout<< "################################################\n"<< endl;
}
void leftmove(){process_que.push_back(process_que.front());// 插入隊(duì)首元素
for(int i=0; iprocess_que[i] = process_que[i+1];
}
process_que.pop_back();// 彈出插入隊(duì)尾的多出的隊(duì)首元素
}
//主函數(shù)
int main(){cout<< "請輸入進(jìn)程數(shù):";
cin >>n;// 輸入進(jìn)程數(shù)1~10
cout<< "請輸入時(shí)間片長度:";
cin >>age;// 時(shí)間片長度
// 初始化隊(duì)列
for(int i=0; icout<< "第"<< i+1<< "個(gè)進(jìn)程的進(jìn)程號,到達(dá)時(shí)間,服務(wù)時(shí)間:";
cin >>pro[i].pno >>pro[i].arrivetime >>pro[i].needtime;
// process_que.push_back(pro[i]);
}
init_print();// 打印
// 核心代碼
while(flag != n){for(int i=0; i//判斷是否有新抵達(dá)的進(jìn)程
if(pro[i].arrivetime<= clk_t && hash_pro[i]==0)
{process_que.push_back(pro[i]);
hash_pro[i] = 1;
}
}
if(!process_que.empty()){// 如果進(jìn)程隊(duì)列還有待調(diào)度進(jìn)程
process_que[0].state = 'R';
process_que[0].runtime = min(process_que[0].runtime+age, process_que[0].needtime);
run_print();
process_que[0].state = 'W';
leftmove();
if(process_que.back().runtime==process_que.back().needtime){struct pcb *node = &pro[process_que.back().pno-1];
node->achievetime = clk_t;//計(jì)算完成進(jìn)程的完成時(shí)間點(diǎn)
node->aroundtime = clk_t-node->arrivetime;//計(jì)算完成進(jìn)程的周轉(zhuǎn)時(shí)間
node->weight_aroundtime = node->aroundtime/node->needtime;//計(jì)算完成進(jìn)程的帶權(quán)周轉(zhuǎn)時(shí)間(周轉(zhuǎn)時(shí)間/服務(wù)時(shí)間)
node->waittime = node->aroundtime-node->needtime;//完成進(jìn)程的等待時(shí)間(周轉(zhuǎn)時(shí)間-服務(wù)時(shí)間)
process_que.pop_back();
flag++;
}
}
clk_t += age;//時(shí)間片結(jié)束,系統(tǒng)時(shí)間增加
}
end_print();
cout<< n<<"條進(jìn)程執(zhí)行完畢";
for(int i=0; iave_aroundtime += pro[i].aroundtime; // 平均周轉(zhuǎn)時(shí)間
ave_weight_aroundtime += pro[i].weight_aroundtime; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
}
ave_aroundtime/=n;
ave_weight_aroundtime/=n;
cout<< "平均周轉(zhuǎn)時(shí)間:"<< ave_aroundtime<< endl;
cout<< "平均帶權(quán)周轉(zhuǎn)時(shí)間:"<< ave_weight_aroundtime<< endl;
return 0;
}
運(yùn)行截圖只要不斷地對就緒隊(duì)列隊(duì)首的進(jìn)程改變狀態(tài),執(zhí)行一個(gè)時(shí)間片,并且取增加一個(gè)時(shí)間片和服務(wù)時(shí)間的最小值確保不會超過服務(wù)時(shí)間,然后執(zhí)行完畢就leftmove,,整個(gè)隊(duì)列移動,然后這時(shí)該進(jìn)程已經(jīng)跑到隊(duì)尾,此時(shí)判斷.back()的隊(duì)尾進(jìn)程,判斷執(zhí)行時(shí)間是否等于服務(wù)時(shí)間,是就說明執(zhí)行完畢可以彈出。最后結(jié)束的時(shí)候不能只判斷隊(duì)列是否為空,還要確認(rèn)所有進(jìn)程是否已經(jīng)到達(dá),因?yàn)榭赡苡械倪M(jìn)程還在到達(dá)的路上,還沒有進(jìn)隊(duì),所以最終判斷的條件應(yīng)該是是否所有進(jìn)程都已經(jīng)結(jié)束,或者說完成的進(jìn)程數(shù)是否等于創(chuàng)建的進(jìn)程數(shù)n
二、優(yōu)先級調(diào)度算法PSA 代碼#includeusing namespace std;
// 總時(shí)間
int clk_t = 0;
// 額外記錄的量
float ave_aroundtime=0; // 平均周轉(zhuǎn)時(shí)間
float ave_weight_aroundtime=0; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
// pcb結(jié)構(gòu)體
struct pcb{int pno;// 進(jìn)程號
char state = 'W';// 狀態(tài),就緒W,運(yùn)行R
int arrivetime;// 到達(dá)時(shí)間
int runtime = 0;// 已經(jīng)運(yùn)行的時(shí)間
int needtime;// 服務(wù)時(shí)間
int rank;// 優(yōu)先級
int achievetime;// 完成時(shí)間
int aroundtime;// 周轉(zhuǎn)時(shí)間
float weight_aroundtime;// 帶權(quán)周轉(zhuǎn)時(shí)間
int waittime;// 等待時(shí)間
}pro[10];
// 創(chuàng)建進(jìn)程就緒隊(duì)列
vectorprocess_que;
// 進(jìn)程數(shù)
int n;
// 時(shí)間片長度
int age;
// 完成進(jìn)程標(biāo)志數(shù)組
int hash_pro[10]={0};
int flag=0;
// 聲明
void init_print();
void run_print();
void rightmove();
void init_print(){cout<< "\n##################當(dāng)前初始化進(jìn)程###################"<< endl;
cout<< "name \t state \t needtime \t runtime \t rank"<< endl;
for(int i=0; icout<< "|"<< pro[i].pno<< "\t";
cout<< "|"<< pro[i].state<< "\t";
cout<< "|"<< pro[i].needtime<< "\t\t";
cout<< "|"<< pro[i].runtime<< "\t\t" ;
cout<< "|"<< pro[i].rank<< "\t"<< endl;
}
cout<< "################################################\n"<< endl;
}
void run_print(){cout<< "\n##################當(dāng)前運(yùn)行進(jìn)程###################"<< endl;
cout<< "name \t state \t needtime rank runtime"<< endl;
if(!process_que.empty()){cout<< "|"<< process_que[0].pno<< "\t";
cout<< "|"<< process_que[0].state<< "\t";
cout<< "|"<< process_que[0].rank<< "\t";
cout<< "|"<< process_que[0].needtime<< "\t";
cout<< "|"<< process_que[0].runtime<< "\t";
} else {cout<< "process_queue is none now!";
}
cout<< endl;
for(int i=1; icout<< "|"<< process_que[i].pno<< "\t";
cout<< "|"<< process_que[i].state<< "\t";
cout<< "|"<< process_que[i].rank<< "\t";
cout<< "|"<< process_que[i].needtime<< "\t";
cout<< "|"<< process_que[i].runtime<< "\t"<< endl;
}
cout<< "################################################\n"<< endl;
}
void end_print(){cout<< "\n##################進(jìn)程完成總覽###################"<< endl;
cout<< "name \t state \t arrivetime \t needtime \t rank \t achievetime \t aroundtime \t weight_aroundtime \t waittime"<< endl;
if(!process_que.empty()){cout<< "|"<< process_que[0].pno<< "\t";
cout<< "|"<< process_que[0].state<< "\t";
cout<< "|"<< process_que[0].arrivetime<< "\t";
cout<< "|"<< process_que[0].needtime<< "\t";
cout<< "|"<< process_que[0].rank<< "\t";
cout<< "|"<< process_que[0].achievetime<< "\t";
cout<< "|"<< process_que[0].aroundtime<< "\t";
cout<< "|"<< process_que[0].weight_aroundtime<< "\t";
cout<< "|"<< process_que[0].waittime<< "\t";
} else {cout<< "process_queue is none now!";
}
cout<< endl;
for(int i=0; icout<< "|"<< pro[i].pno<< "\t";
cout<< "|"<< pro[i].state<< "\t";
cout<< "|"<< pro[i].arrivetime<< "\t\t";
cout<< "|"<< pro[i].needtime<< "\t\t";
cout<< "|"<< pro[i].rank<< "\t\t";
cout<< "|"<< pro[i].achievetime<< "\t\t";
cout<< "|"<< pro[i].aroundtime<< "\t\t";
cout<< "|"<< pro[i].weight_aroundtime<< "\t\t\t";
cout<< "|"<< pro[i].waittime<< "\t"<< endl;//3 20 1 30 50 2 40 30 3 50 100
}
cout<< "################################################\n"<< endl;
}
void leftmove(){struct pcb node = process_que.front();
int j;
for(j=0; jif(process_que[j].rank >= node.rank) j++;
else break;
}
process_que.insert(process_que.begin()+j,node);
for(int i=0; iprocess_que[i] = process_que[i+1];
}
process_que.pop_back();// 彈出插入隊(duì)尾的多出的隊(duì)首元素
}
int first_search(vectora){int ret=0;
for(int i=0; iif(a[i].rank>a[ret].rank) ret = i;
}
return ret;
}
// 主函數(shù)
int main(){cout<< "請輸入進(jìn)程數(shù):";
cin >>n;// 輸入進(jìn)程數(shù)1~10
cout<< "請輸入時(shí)間片長度:";
cin >>age;// 時(shí)間片長度
// 初始化隊(duì)列
for(int i=0; icout<< "第"<< i+1<< "個(gè)進(jìn)程的進(jìn)程號,到達(dá)時(shí)間,服務(wù)時(shí)間,優(yōu)先級:";
cin >>pro[i].pno >>pro[i].arrivetime >>pro[i].needtime >>pro[i].rank;
// process_que.push_back(pro[i]);
}
init_print();// 打印
// 核心代碼
while(flag != n){for(int i=0; i// 判斷是否有新抵達(dá)的進(jìn)程
if(pro[i].arrivetime<= clk_t && hash_pro[i]==0)
{hash_pro[i] = 1;
if(process_que.empty())
{process_que.push_back(pro[i]);
continue;
}
int j;
for(j=0; jif(process_que[j].rank >= pro[i].rank) j++;
else break;
}
process_que.insert(process_que.begin()+j,pro[i]);
}
}
if(!process_que.empty()){// 如果進(jìn)程隊(duì)列還有待調(diào)度進(jìn)程
process_que[0].state = 'R';
process_que[0].runtime = min(process_que[0].runtime+age, process_que[0].needtime);
run_print();
process_que[0].state = 'W';
if(process_que[0].runtime==process_que[0].needtime){struct pcb *node = &pro[process_que[0].pno-1];
node->achievetime = clk_t;// 計(jì)算完成進(jìn)程的完成時(shí)間點(diǎn)
node->aroundtime = clk_t-node->arrivetime;// 計(jì)算完成進(jìn)程的周轉(zhuǎn)時(shí)間
node->weight_aroundtime = node->aroundtime/node->needtime;// 計(jì)算完成進(jìn)程的帶權(quán)周轉(zhuǎn)時(shí)間(周轉(zhuǎn)時(shí)間/服務(wù)時(shí)間)
node->waittime = node->aroundtime-node->needtime;// 完成進(jìn)程的等待時(shí)間(周轉(zhuǎn)時(shí)間-服務(wù)時(shí)間)
process_que.pop_back();
flag++;
}
leftmove();
}
clk_t += age;// 時(shí)間片結(jié)束,系統(tǒng)時(shí)間增加
}
end_print();
cout<< n<<"條進(jìn)程執(zhí)行完畢";
for(int i=0; iave_aroundtime += pro[i].aroundtime; // 平均周轉(zhuǎn)時(shí)間
ave_weight_aroundtime += pro[i].weight_aroundtime; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
}
ave_aroundtime/=n;
ave_weight_aroundtime/=n;
cout<< "平均周轉(zhuǎn)時(shí)間:"<< ave_aroundtime<< endl;
cout<< "平均帶權(quán)周轉(zhuǎn)時(shí)間:"<< ave_weight_aroundtime<< endl;
return 0;
}
運(yùn)行截圖在時(shí)間片基礎(chǔ)上,只需要增加pcb塊的內(nèi)容,即優(yōu)先級rank,然后在leftmove的時(shí)候通過遍歷判斷優(yōu)先級,確保不會插入到低優(yōu)先級的就緒進(jìn)程后,然后在彈出的時(shí)候也需要修改們不能彈出隊(duì)尾了,要在leftmove前就判斷是否可以彈出,如果可以彈出直接彈出,不能再進(jìn)行l(wèi)eftmove。
三、小結(jié)算法本身不難,只要細(xì)心去確認(rèn)判斷條件,RR進(jìn)程調(diào)度算法做出來了優(yōu)先級進(jìn)程調(diào)度算法也就是增加一些判斷條件,主要是對算法本身的理解要透徹,具體實(shí)現(xiàn)起來就是時(shí)間問題了。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧