一、FCFS 代碼一個(gè)班(20信安)的同學(xué)搜到這篇?jiǎng)e直接copy我的,代碼僅供參考
成都創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷,包括做網(wǎng)站、成都網(wǎng)站制作、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營(yíng)銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營(yíng)銷等。成都創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,成都創(chuàng)新互聯(lián)核心團(tuán)隊(duì)10年專注互聯(lián)網(wǎng)開(kāi)發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹(shù)立了良好口碑。
#includeusing namespace std;
float ave_aroundtime; // 平均周轉(zhuǎn)時(shí)間
float ave_weight_aroundtime; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
int n;
float nowtime=0;
struct FCFS
{int name;
char state;
float arrivetime; // 到達(dá)時(shí)間(即進(jìn)入外存的時(shí)間點(diǎn))
float servicetime; // 服務(wù)時(shí)間(所需cpu時(shí)間)
float finishtime; // 結(jié)束時(shí)間
float aroundtime; //周轉(zhuǎn)時(shí)間(結(jié)束時(shí)間-到達(dá)時(shí)間,即進(jìn)入外存到執(zhí)行完畢所需時(shí)間)
float weight_aroundtime; // 帶權(quán)周轉(zhuǎn)時(shí)間
};
bool cmp(FCFS a, FCFS b)
{return a.arrivetimecout<< "請(qǐng)輸入作業(yè)信息:"<< endl;
cout<< "名稱:";
cin >>p.name;
cout<< "到達(dá)時(shí)間:";
cin >>p.arrivetime;
cout<< "服務(wù)時(shí)間:";
cin >>p.servicetime;
cout<< endl;
}
void out(FCFS a)
{cout<< "作業(yè)"<< a.name<< "\t"<< a.arrivetime<< "\t\t"<< a.servicetime<< "\t\t"<< a.aroundtime<< endl;
}
void left_move(vector&a)
{for(int i=0; ia[i] = a[i+1];
}
a.pop_back();// 彈出插入隊(duì)尾的多出的隊(duì)首元素
}
void run(FCFS &node)
{node.finishtime = max(nowtime,node.arrivetime)+node.servicetime;
node.aroundtime = node.finishtime - node.arrivetime;
node.weight_aroundtime = node.aroundtime/node.servicetime;
nowtime = node.finishtime;
}
int main(){vectorsort_queue;// 保存排序后的隊(duì)列
vectorback_queue;// 后備隊(duì)列
vectorwait_queue;// 就緒隊(duì)列
FCFS temp[10];
cout<< "輸入作業(yè)數(shù)目:";
cin >>n;
for(int i=0; iinput(temp[i]);
back_queue.push_back(temp[i]);// 進(jìn)入后備隊(duì)列
}
sort(back_queue.begin(), back_queue.end(), cmp);// 按到達(dá)時(shí)間從小到大排序
sort_queue = back_queue;
while(!back_queue.empty())
{// 將一個(gè)作業(yè)從外存進(jìn)入內(nèi)存
wait_queue.push_back(back_queue[0]);
// 后備隊(duì)列隊(duì)首彈出
left_move(back_queue);
// 就緒隊(duì)列作業(yè)開(kāi)始占用cpu
run(temp[wait_queue[0].name-1]);
// 就緒隊(duì)列作業(yè)完成,彈出
wait_queue.clear();
}
cout<< "FCFS算法進(jìn)程的運(yùn)行順序?yàn)?"<< endl;
for(int i=0; icout<< "作業(yè)"<< sort_queue[i].name<< "-->";
}
cout<< "over";
cout<< "進(jìn)程完成后具體信息記錄:"<< endl;
cout<< "進(jìn)程\t到達(dá)時(shí)間\t服務(wù)時(shí)間\t周轉(zhuǎn)時(shí)間\t"<< endl;
for(int i=0; iout(temp[i]);
ave_aroundtime += temp[i].aroundtime;
ave_weight_aroundtime += temp[i].weight_aroundtime;
}
ave_aroundtime/=n;
ave_weight_aroundtime/=n;
cout<< "采用FCFS算法算得的平均周轉(zhuǎn)時(shí)間為:"<< ave_aroundtime<< endl;
cout<< "采用FCFS算法算得的平均帶權(quán)周轉(zhuǎn)時(shí)間為:"<< ave_weight_aroundtime<< endl;
}
運(yùn)行截圖二、SJF
代碼#includeusing namespace std;
float ave_aroundtime; // 平均周轉(zhuǎn)時(shí)間
float ave_weight_aroundtime; // 平均帶權(quán)周轉(zhuǎn)時(shí)間
int n;
float nowtime=0;
struct SJF
{int name;
char state;
float arrivetime; // 到達(dá)時(shí)間(即進(jìn)入外存的時(shí)間點(diǎn))
float servicetime; // 服務(wù)時(shí)間(所需cpu時(shí)間)
float finishtime; // 結(jié)束時(shí)間
float aroundtime; //周轉(zhuǎn)時(shí)間(結(jié)束時(shí)間-到達(dá)時(shí)間,即進(jìn)入外存到執(zhí)行完畢所需時(shí)間)
float weight_aroundtime; // 帶權(quán)周轉(zhuǎn)時(shí)間
};
bool cmp(SJF a, SJF b)
{return a.arrivetimecout<< "請(qǐng)輸入作業(yè)信息:"<< endl;
cout<< "名稱:";
cin >>p.name;
cout<< "到達(dá)時(shí)間:";
cin >>p.arrivetime;
cout<< "服務(wù)時(shí)間:";
cin >>p.servicetime;
cout<< endl;
}
void out(SJF a)
{cout<< "作業(yè)"<< a.name<< "\t"<< a.arrivetime<< "\t\t"<< a.servicetime<< "\t\t"<< a.aroundtime<< endl;
}
void left_move(vector&a)
{for(int i=0; ia[i] = a[i+1];
}
a.pop_back();// 彈出插入隊(duì)尾的多出的隊(duì)首元素
}
void run(SJF &node)
{node.finishtime = max(nowtime,node.arrivetime)+node.servicetime;
node.aroundtime = node.finishtime - node.arrivetime;
node.weight_aroundtime = node.aroundtime/node.servicetime;
nowtime = node.finishtime;
}
void i_first(vector&back_queue, int first_i)
{// 前移該進(jìn)程至隊(duì)首
SJF temp = back_queue[first_i];
for(int i=first_i; i>0; i--)
{back_queue[i] = back_queue[i-1];
}
back_queue[0] = temp;
}
void zero_flush(vector&a)
{vectorb;
for(int i=0; iif(a[i].name!=0) b.push_back(a[i]);
}
a.clear();
for(int i=0; ia.push_back(b[i]);
}
}
void fill(vector&back_queue, vector&sort_queue)
{if(sort_queue.empty()) return;
for(int i=0; iif(sort_queue[i].arrivetime<= nowtime)
{back_queue.push_back(sort_queue[i]);
sort_queue[i].name=0;
}
}
zero_flush(sort_queue);
if(sort_queue.empty()) return;
// 這里很關(guān)鍵,如果執(zhí)行完進(jìn)程后后備隊(duì)列出現(xiàn)空窗期,則直接將nowtime賦值為下一個(gè)即將到來(lái)的進(jìn)程的到來(lái)時(shí)間
if(back_queue.empty())
{nowtime = sort_queue[0].arrivetime;
fill(back_queue, sort_queue);
}
return;
}
void short_first(vector&back_queue)
{if(back_queue.empty()) return;
int mini=0, minservicetime=back_queue[0].servicetime;
// 遍歷尋找在后備隊(duì)列中的最短服務(wù)時(shí)間進(jìn)程
for(int i=0; iif(minservicetime >back_queue[i].servicetime)
{mini = i;
minservicetime = back_queue[i].servicetime;
}
}
// 前移該進(jìn)程至隊(duì)首
SJF temp = back_queue[mini];
for(int i=mini; i>0; i--)
{back_queue[i] = back_queue[i-1];
}
back_queue[0] = temp;
}
void prt(vectora)
{for(int i=0; icout<< a[i].name<< " ";
}
cout<< endl;
}
int main(){vectorsort_queue;// 保存排序后的隊(duì)列
vectorback_queue;// 后備隊(duì)列
vectorwait_queue;// 就緒隊(duì)列
vectorover_queue;// 執(zhí)行完畢隊(duì)列
SJF temp[10];
cout<< "輸入作業(yè)數(shù)目:";
// input
cin >>n;
for(int i=0; iinput(temp[i]);
sort_queue.push_back(temp[i]);// 進(jìn)入后備隊(duì)列
}
sort(sort_queue.begin(), sort_queue.end(), cmp);// 按到達(dá)時(shí)間從小到大排序
// 第一次進(jìn)程調(diào)度
back_queue.push_back(sort_queue[0]);// 第一個(gè)進(jìn)程直接進(jìn)入
left_move(sort_queue);
// 一次進(jìn)程調(diào)度
wait_queue.push_back(back_queue[0]);
left_move(back_queue);
run(temp[wait_queue[0].name-1]);
over_queue.push_back(wait_queue[0]);
wait_queue.clear();
cout<< nowtime<< " ";
cout<< "back:";
prt(back_queue);
cout<< "sort:";
prt(sort_queue);
while(over_queue.size()!=n)
{
fill(back_queue, sort_queue); // 根據(jù)上一個(gè)進(jìn)程結(jié)束時(shí)間nowtime填充后備隊(duì)列back_queue
short_first(back_queue); // 前移后備隊(duì)列中服務(wù)時(shí)間最短的進(jìn)程到隊(duì)首
// cout<< back_queue[0].name<< endl;
cout<< nowtime<< " ";
cout<< "back:";
prt(back_queue);
cout<< "sort:";
prt(sort_queue);
wait_queue.push_back(back_queue[0]); // 后備隊(duì)列隊(duì)首進(jìn)程放到就緒隊(duì)列中
left_move(back_queue);// 后備隊(duì)列隊(duì)首彈出
run(temp[wait_queue[0].name-1]);// 就緒隊(duì)列作業(yè)開(kāi)始占用cpu
over_queue.push_back(wait_queue[0]);
wait_queue.clear();// 就緒隊(duì)列作業(yè)完成,彈出
}
cout<< "SJF算法進(jìn)程的運(yùn)行順序?yàn)?"<< endl;
for(int i=0; icout<< "作業(yè)"<< over_queue[i].name<< "-->";
}
cout<< "over"<< endl;
cout<< "進(jìn)程完成后具體信息記錄:"<< endl;
cout<< "進(jìn)程\t到達(dá)時(shí)間\t服務(wù)時(shí)間\t周轉(zhuǎn)時(shí)間\t"<< endl;
for(int i=0; iout(temp[i]);
ave_aroundtime += temp[i].aroundtime;
ave_weight_aroundtime += temp[i].weight_aroundtime;
}
ave_aroundtime/=n;
ave_weight_aroundtime/=n;
cout<< "采用SJF算法算得的平均周轉(zhuǎn)時(shí)間為:"<< ave_aroundtime<< endl;
cout<< "采用SJF算法算得的平均帶權(quán)周轉(zhuǎn)時(shí)間為:"<< ave_weight_aroundtime<< endl;
}
運(yùn)行截圖三、小結(jié)執(zhí)行的進(jìn)程數(shù)據(jù)是參考ppt最后給出的測(cè)試用例,為了簡(jiǎn)單期間ABCDE進(jìn)程名稱設(shè)置為12345.
作業(yè)調(diào)度的過(guò)程沒(méi)有用一個(gè)時(shí)鐘進(jìn)行不斷推進(jìn),而是利用每一個(gè)執(zhí)行進(jìn)程直接計(jì)算結(jié)束時(shí)間,因此會(huì)出現(xiàn)進(jìn)程執(zhí)行完,后備隊(duì)列依然為空的情況,這里就需要判斷進(jìn)程是否全部執(zhí)行完畢,即一開(kāi)始需要先輸入進(jìn)程的個(gè)數(shù),否則無(wú)法判斷后續(xù)是否還有未到來(lái)的進(jìn)程。
短作業(yè)調(diào)度算法的過(guò)程中,需要在每次進(jìn)程執(zhí)行完畢,在后備隊(duì)列內(nèi)進(jìn)程中搜索服務(wù)時(shí)間最短的進(jìn)程進(jìn)行執(zhí)行,在這個(gè)過(guò)程中,需要找到合適的進(jìn)程,將其挪到后備隊(duì)列隊(duì)首,出隊(duì)放入就緒隊(duì)列,然后占用CPU時(shí)間
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
分享標(biāo)題:操作系統(tǒng)實(shí)驗(yàn)(二)——作業(yè)調(diào)度算法-創(chuàng)新互聯(lián)
文章位置:http://jinyejixie.com/article36/djecpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、微信小程序、做網(wǎng)站、企業(yè)網(wǎng)站制作、虛擬主機(jī)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容