-
大小: 2KB文件類(lèi)型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-27
- 語(yǔ)言: 其他
- 標(biāo)簽:
資源簡(jiǎn)介
進(jìn)程調(diào)度模擬設(shè)計(jì)--先來(lái)先服務(wù)、強(qiáng)占式短進(jìn)程優(yōu)先算法

代碼片段和文件信息
#include
#include
using?namespace?std;
struct?PCB
{
string?name;//進(jìn)程名
int?ta;//進(jìn)程到達(dá)時(shí)間
int?ts;//進(jìn)程估計(jì)運(yùn)行的時(shí)間
int?tb;//進(jìn)程開(kāi)始運(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)程的指針
};
string?pname[100];//保存進(jìn)程調(diào)度隊(duì)列
int?pronum;//定義進(jìn)程數(shù)為pronum
int?total;//記錄所有進(jìn)程的總周轉(zhuǎ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ù)量
void?FCFS(PCB?*head);//先來(lái)先服務(wù)算法
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;
cout<<“*進(jìn)程調(diào)度模擬設(shè)計(jì)——先來(lái)先服務(wù)、強(qiáng)占式短進(jìn)程優(yōu)先算法*“< cout<<“***********1.先來(lái)先服務(wù)算法***************************“< cout<<“***********2.強(qiáng)占式短進(jìn)程優(yōu)先算法*********************“< cout<<“***********3?退出*************************************“< l1: cout<<“請(qǐng)輸入您的選擇:“< l2: cin>>choice;
PCB?*head=NULL;
switch(choice)
{
case?1:head=create(head);FCFS(head);goto?l1;
case?2:head=create(head);SJFrun(head);goto?l1;
case?3:break;
default:cout<<“輸入錯(cuò)誤!\n請(qǐng)重新輸入:“< }
}
PCB?*create(PCB?*head)
{
PCB?*p1*p2;
p1=p2=new?PCB;
head=p1;
cout<<“請(qǐng)輸入進(jìn)程數(shù):“;
cin>>pronum;
for(int?i=0;i {
p2=p1;
p1=new?PCB;
p1->next=NULL;
cout<<“請(qǐng)依次輸入第“< cin>>p1->name>>p1->ta>>p1->ts;
p1->tm=p1->ts;
p1->rn=1;
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;
}
void?FCFS(PCB?*head)//先來(lái)先服務(wù)算法
{
int?i=0j=0;
total=0;
weight=0;
sort(head);
int?time=0count;
PCB?*p*q;
while(head->next!=NULL)
{
count=getCount(headtime);
if(count==0)time++;
else
{
p=head;
q=p->next;
cout<<“******************************************************“< cout<<“進(jìn)程名:“<name< pname[i]=q->name;
i++;
cout<<“到達(dá)時(shí)間:“<ta< q->tb=time;
cout<<“進(jìn)程開(kāi)始運(yùn)行時(shí)間:“<tb< time=q->tb+q->ts;
cout<<“進(jìn)程運(yùn)行結(jié)束時(shí)間:“< q->totalTime=time-q->ta;
cout<<“周轉(zhuǎn)時(shí)間:“<totalTime< total+=q->totalTime;
q->weightTotalTime=q->totalTime/(double)q->ts;
weight+=q->weightTotalTime;
cout<<“帶權(quán)周轉(zhuǎn)時(shí)間:“<weightTotalTime< cou
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????6165??2010-01-25?21:32??fcfs&sjf.cpp
-----------?---------??----------?-----??----
?????????????????6165????????????????????1
評(píng)論
共有 條評(píng)論