-
大小: 7KB文件類型: .cpp金幣: 1下載: 0 次發(fā)布日期: 2021-05-16
- 語言: C/C++
- 標(biāo)簽:
資源簡介
⑴ 能夠選擇不同的調(diào)度算法——時(shí)間片輪轉(zhuǎn)算法和強(qiáng)占式短進(jìn)程優(yōu)先算法;⑵ 能夠輸入進(jìn)程的基本信息——進(jìn)程名、到達(dá)時(shí)間和運(yùn)行時(shí)間等;⑶ 根據(jù)選擇的調(diào)度算法顯示進(jìn)程調(diào)度隊(duì)列;⑷ 根據(jù)選擇的調(diào)度算法計(jì)算平均周轉(zhuǎn)時(shí)間和平均帶權(quán)周轉(zhuǎn)時(shí)間。
代碼片段和文件信息
#include
#include
using?namespace?std;
#define?QT?2//定義時(shí)間片長度為2
struct?PCB
{
string?name;//進(jìn)程名
int?ta;//進(jìn)程到達(dá)時(shí)間
int?ts;//進(jìn)程估計(jì)運(yùn)行的時(shí)間
int?tb;//進(jìn)程開始運(yùn)行時(shí)間
int?tm;//進(jìn)程仍需運(yùn)行的時(shí)間
int?to;//進(jìn)程完成的時(shí)間
int?rn;//進(jìn)程運(yùn)行的次數(shù)
int?totalTime;//周轉(zhuǎn)時(shí)間
double?weightTotalTime;//帶權(quán)周轉(zhuǎn)時(shí)間(周轉(zhuǎn)時(shí)間/估計(jì)運(yùn)行時(shí)間)
PCB?*next;//定義指向下一個(gè)進(jìn)程的指針
};
int?pronum;//定義進(jìn)程數(shù)為pronum
int?total;//記錄所有進(jìn)程的總時(shí)間
double?weight;//記錄所有進(jìn)程的帶權(quán)周轉(zhuǎn)時(shí)間
PCB?*create(PCB?*head);//創(chuàng)建進(jìn)程隊(duì)列
void?del(PCB?*p);//刪除p的下一個(gè)節(jié)點(diǎn)
void?sort(PCB?*head);//將進(jìn)程按到達(dá)的先后順序排列
int?getCount(PCB?*headint?time);//察看在time之前到達(dá)但未移動(dòng)到運(yùn)行隊(duì)列的進(jìn)程數(shù)量
PCB?*searchEnd(PCB?*head);//查找并返回循壞隊(duì)列的尾節(jié)點(diǎn)
void?move(PCB?*headFPCB?*headTint?n);//將headF后的n個(gè)節(jié)點(diǎn)移動(dòng)到循環(huán)隊(duì)列headT中
void?cyclerun(PCB?*head);//時(shí)間片輪轉(zhuǎn)算法
PCB?*SJF(PCB?*headint?count);//在頭節(jié)點(diǎn)后的count個(gè)節(jié)點(diǎn)中選擇需時(shí)間最小的返回
void?SJFrun(PCB?*head);//強(qiáng)占式短進(jìn)程優(yōu)先算法
void?main()
{
int?choice;
char?a;
cout<<“*進(jìn)程調(diào)度模擬設(shè)計(jì)——時(shí)間片輪轉(zhuǎn)、強(qiáng)占式短進(jìn)程優(yōu)先算法*“< cout<<“***********1.時(shí)間片輪轉(zhuǎn)算法***************************“< cout<<“***********2.強(qiáng)占式短進(jìn)程優(yōu)先算法*********************“< cout<<“***********3?退出*************************************“< l1: cout<<“請輸入您的選擇:“< l2: cin>>choice;
PCB?*head=NULL;
switch(choice)
{
case?1:head=create(head);cyclerun(head);goto?l1;
case?2:head=create(head);SJFrun(head);goto?l1;
case?3:break;
default:cout<<“輸入錯(cuò)誤!\n請重新輸入:“< }
}
PCB?*create(PCB?*head)
{
PCB?*p1*p2;
p1=p2=new?PCB;
head=p1;
cout<<“請輸入進(jìn)程數(shù):“;
cin>>pronum;
for(int?i=0;i {
p2=p1;
p1=new?PCB;
p1->next=NULL;
cout<<“請依次輸入第“< cin>>p1->name>>p1->ta>>p1->ts;
p1->tm=p1->ts;
p1->rn=1;
total+=p1->ts;
p2->next=p1;
}
return?head;
}
void?sort(PCB?*head)//將進(jìn)程按到達(dá)的先后順序排列
{
PCB?*p*q*r*s;
if(head->next!=NULL)
{
p=head->next->next;
head->next->next=NULL;
}
while(p)
{
q=p;
p=p->next;
r=head;
s=head->next;
while(s&&s->ta<=q->ta)
{
r=s;
s=s->next;
}
r->next=q;
q->next=s;
}
}
void?del(PCB?*?p)//刪除p的下一個(gè)節(jié)點(diǎn)
{
PCB?*tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
}
int?getCount(PCB?*headint?time)//察看在time之前到達(dá)但未移動(dòng)到運(yùn)行隊(duì)列的進(jìn)程數(shù)量
{
int?count=0;
PCB?*s*t;
s=head;
t=s->next;
while(t!=NULL&&t->ta<=time)
{
???s=t;
???t=t->next;?
???count++;//count記錄當(dāng)前時(shí)刻到達(dá)的進(jìn)程數(shù)
}
return?count;
}
PCB*?searchEnd(PCB?*head)//查找并返回循壞隊(duì)列的尾節(jié)點(diǎn)
{
PCB?*p*q;
p=head;
q=head->next;
while(q->next!=head)
{
???p=q;
???q=q->next;
}
return?p;
}
void?move(PCB?*headFPCB?*headTint?n)//將headF后的n個(gè)節(jié)點(diǎn)移動(dòng)到循環(huán)隊(duì)列headT中
{
PCB?*r*s*t;
s=headF;
t=s->next;
r=t;//r記錄要移動(dòng)的第一個(gè)節(jié)點(diǎn)
while(n>1)
{
???t=t->next;
???n--;
}
s->next=t->next;//以上完成從原隊(duì)列中摘除相關(guān)節(jié)點(diǎn)rt分別為第一個(gè)和最后一個(gè)節(jié)點(diǎn)?
s=searchEnd(headT);
t->next=s->next;
s->next=r;
}
void?cyclerun(PCB?*head)//時(shí)間片輪轉(zhuǎn)算法
{
sort(head);
int?time=0;//記錄當(dāng)前時(shí)間
int?newarrive;//新到達(dá)進(jìn)程數(shù)
PCB?*rhead;
rhead=new?PCB;
rhead->next=rhead;//創(chuàng)建新的循環(huán)鏈表,存放當(dāng)前就緒隊(duì)列中的進(jìn)程
PCB?*p*q;
p=rhead;
q=p->next;//q記錄當(dāng)前應(yīng)當(dāng)運(yùn)行的
評論
共有 條評論