先来先服务短作业优先时间片轮转高优先权算法.docx
《先来先服务短作业优先时间片轮转高优先权算法.docx》由会员分享,可在线阅读,更多相关《先来先服务短作业优先时间片轮转高优先权算法.docx(16页珍藏版)》请在冰豆网上搜索。
![先来先服务短作业优先时间片轮转高优先权算法.docx](https://file1.bdocx.com/fileroot1/2023-2/21/4a809e2b-3f3a-40bb-922e-cf3e565e0cda/4a809e2b-3f3a-40bb-922e-cf3e565e0cda1.gif)
先来先服务短作业优先时间片轮转高优先权算法
#include
#defineN4
#include
//时间片系列
#include
#include
#defineMAX4//进程数量
#defineRR4//时间片大小
//时间片系列
usingnamespacestd;
structtime{
stringname;
floatarriveTime;
floatrunTime;
floatfinishTime;
floattotalTime;
floatweightTotalTime;
};
//时间片系列
structpro
{
charnum;
intarriveTime;
intburst;
intrt;//记录进程被运行的次数
structpro*next;
};
intTOTALTIME;//记录所有进程的总时间
//时间片系列
//函数声明
structpro*creatList();
voidinsert(structpro*head,structpro*s);
structpro*searchByAT(structpro*head,intAT);
voiddel(structpro*p);
intgetCount(structpro*head,inttime);
structpro*searchEnd(structpro*head);
voidmove(structpro*headF,structpro*headT,intn);
structpro*creatList()//创建链表,按照进程的到达时间排列,记
录所有进程的信息
{
structpro*head=(structpro*)malloc(sizeof(structpro));
head->next=NULL;
structpro*s;
inti;
TOTALTIME=0;
cout<<"输入进程名&到达时间&运行时间(example:
a00):
"<cout<<"name"<<"arrivetime"<<"runtime"<for(i=0;i{
s=(structpro*)malloc(sizeof(structpro));
cin>>s->num;
cin>>s->arriveTime;
cin>>s->burst;
TOTALTIME+=s->burst;//计算总时间
s->rt=1;//rt的初始值为1
s->next=NULL;
insert(head,s);
}
returnhead;//到达队列中的进程按照其到达时间的先后顺序排列
}
voidinsert(structpro*head,structpro*s)//插入节点
{
structpro*p=searchByAT(head,s->arriveTime);
s->next=p->next;
p->next=s;
return;
}
structpro*searchByAT(structpro*head,intAT)//查找第一个
到达时间大于等于AT的节点,返回其前一个指针
{
structpro*p,*q;
p=head;
q=head->next;
while(q!
=NULL&&q->arriveTime<=AT)
{
p=q;
q=q->next;
}
returnp;
}
voiddel(structpro*p)//删除p的下一个节点
{
structpro*tmp;
tmp=p->next;
p->next=tmp->next;
free(tmp);
return;
}
intgetCount(structpro*head,inttime)//察看在time之前到达
但未移动到运行队列的进程数量
{
intcount=0;
structpro*s,*t;
s=head;
t=s->next;
while(t!
=NULL&&t->arriveTime<=time)
{
s=t;
t=t->next;
count++;//count记录当前时刻到达的进程数
}
returncount;
}
structpro*searchEnd(structpro*head)//查找并返回循坏队列
的尾节点的前一个节点
{
structpro*p,*q;
p=head;
q=head->next;
while(q->next!
=head)
{
p=q;
q=q->next;
}
returnp;
}
voidmove(structpro*headF,structpro*headT,intn)//将
headF后的n个节点移动到循环队列headT中
{
structpro*r,*s,*t;
s=headF;
t=s->next;
r=t;//r记录要移动的第一个节点
while(n>1)
{
t=t->next;
n--;
}
s->next=t->next;//以上完成从原队列中摘除相关节点,r,t分别为第
一个和最后一个节点
s=searchEnd(headT);
if(s!
=headT)
s=s->next;
t->next=s->next;
s->next=r;
structpro*f=s;
}
voidrun(structpro*head)
{
inttime=0;//记录当前时间
intnewarrive;//新到达进程数
structpro*runhead=(structpro*)malloc(sizeof(structpro));
runhead->next=runhead;//创建新的循环链表,存放当前就绪队列中
的进程
structpro*p,*q;
p=runhead;
q=p->next;//q记录当前应当运行的进程
while(time<=TOTALTIME)
{
newarrive=getCount(head,time);
if(newarrive>0)
move(head,runhead,newarrive);//将head后的newarrive个节点移
动到runhead队列中
if(runhead->next==runhead)//就绪队列中没有进程
time++;
elseif(q==runhead)
{
p=q;
q=q->next;
}
else
{
cout<<"进程名:
"<num<cout<<"到达时间:
"<arriveTime<if(q->rt==1)
printf("响应时间:
%d\n",time-q->arriveTime);
else
printf("第%d次运行开始时间:
%d\n",q->rt,time);
if(q->burst<=RR)
{
time+=q->burst;
printf("第%d次运行结束时间:
%d\n",q->rt,time);
printf("周转时间:
%d\n",time-q->arriveTime);
printf("************************************\n");
structpro*tmp=q;
q=q->next;
p->next=q;
runhead->next=q;
free(tmp);
}
else//q->burst>RR
{
time+=RR;
printf("第%d次运行结束时间:
%d\n",q->rt,time);
printf("************************************\n");
q->burst-=RR;
q->rt++;
structpro*h;
h=runhead->next;
if(h->next!
=runhead)
{while(h->next!
=runhead)
h=h->next;
runhead->next=q->next;
h->next=q;
q->next=runhead;
q=runhead->next;}
else
{structpro*n;
n=q;
p=q;
q=q->next;}
}
}
}
}
//时间片系列
voidInputTime(time*p)
{
inti;//counter
cout<<"inputname&arrivetime&runtime(example:
a0
0):
"<cout<<"name"<<"arrivetime"<<"runtime"<for(i=0;i<=N-1;i++){
floattemp1,temp2;
stringname;
cin>>name;
p[i].name=name;
cin>>temp1;
p[i].arriveTime=temp1;
cin>>temp2;
p[i].runTime=temp2;
}
}
voidPrint(time*p,floattotalTimeSum,float
weightTotalTimeSum){
cout<<"********运行次序:
"<for(intk=0;k<=N-1;k++)
{
cout<
}
cout<cout<<"平均周转时间:
"<cout<<"平均带权周转时间:
"<}
voidsort(time*p)
{
for(inti=0;i<=N-1;i++)
for(intj=0;j<=i;j++)
if(p[i].arriveTime
{
timetemp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
voiddeal(time*p,float&totalTimeSum,float
&weightTotalTimeSum)
{
intk;//counter
for(k=0;k<=N-1;k++)
{
if(k==0)
p[k].finishTime=p[k].arriveTime+p[k].runTime;
else
p[k].finishTime=p[k-1].finishTime+p[k].runTime;
}
for(k=0;k<=N-1;k++)
{
p[k].totalTime=p[k].finishTime-p[k].arriveTime;
p[k].weightTotalTime=p[k].totalTime/p[k].runTime;
totalTimeSum+=p[k].totalTime;
weightTotalTimeSum+=p[k].weightTotalTime;
}
}
voidFCFS(time*p)
{
floattotalTimeSum=0,weightTotalTimeSum=0;
sort(p);
deal(p,totalTimeSum,weightTotalTimeSum);
cout<cout<<"**********************"<cout<<"***先来先服务:
"<Print(p,totalTimeSum,weightTotalTimeSum);
cout<<"****************"<}
voidSWF(time*p)
{
floattotalTimeSum=0,weightTotalTimeSum=0;
sort(p);
for(intm=0;m{
if(m==0)
p[m].finishTime=p[m].arriveTime+p[m].runTime;
else
p[m].finishTime=p[m-1].finishTime+p[m].runTime;
inti=0;
for(intn=m+1;n<=N-1;n++)
{
if(p[n].arriveTime<=p[m].finishTime)
i++;
}
floatmin=p[m+1].runTime;
intfollow=m+1;
for(intk=m+1;k{
if(p[k+1].runTime{min=p[k+1].runTime;
follow=k+1;}
}
timetemp;
temp=p[m+1];
p[m+1]=p[follow];
p[follow]=temp;
}
deal(p,totalTimeSum,weightTotalTimeSum);
cout<cout<<"**********************"<cout<<"***短作业优先:
"<Print(p,totalTimeSum,weightTotalTimeSum);
cout<<"**********************"<}
voidTRRF(time*p){
floattotalTimeSum=0,weightTotalTimeSum=0;
sort(p);
for(intm=0;m{
if(m==0)
p[m].finishTime=p[m].arriveTime+p[m].runTime;
else
p[m].finishTime=p[m-1].finishTime+p[m].runTime;
inti=0;
for(intn=m+1;n<=N-1;n++)
{
if(p[n].arriveTime<=p[m].finishTime)
i++;
}
floatmax=(p[m].finishTime-p[m+1].arriveTime)/p
[m+1].runTime;
intfollow=m+1;
for(intk=m+1;k{
if(max<=(p[m].finishTime-p[k+1].arriveTime)/p
[k+1].runTime){
max=(p[m].finishTime-p[k+1].arriveTime)/p
[k+1].runTime;
follow=k+1;
}
}
timetemp;
temp=p[m+1];
p[m+1]=p[follow];
p[follow]=temp;
}
deal(p,totalTimeSum,weightTotalTimeSum);
cout<cout<<"**********************"<cout<<"***最高响应比优先:
"<Print(p,totalTimeSum,weightTotalTimeSum);
cout<<"**********************"<}
voidmain(){
intlg;
cout<<"请选择任一种算法:
"<cout<<"1.FCFS(先来先服务)2.SWF(短作业优先)3.TRRF
(高响应比优先)4.RR(时间片轮转)0.退出"<cin>>lg;
cout<timea[N],b[N],c[N];
while(lg)
{if(lg==1)
{InputTime(a);
FCFS(a);
cout<//time*b=a;time*c=a;
if(lg==2)
{InputTime(b);
SWF(b);
cout<if(lg==3)
{InputTime(c);
TRRF(c);
cout<//时间片系列
if(lg==4)
{cout<cout<<"********时间片模拟*******"<structpro*head=creatList();
printf("当前时间片大小为:
%d\n",RR);
run(head);
cout<cout<<"1.FCFS(先来先服务)2.SWF(短作业优先)3.TRRF(高响
应比优先)4.RR(时间片轮转)0.退出"<cin>>lg;
cout<cout<<"您选择了退出模拟。
"<}