操作系统的五种进程调度算法地代码.docx
《操作系统的五种进程调度算法地代码.docx》由会员分享,可在线阅读,更多相关《操作系统的五种进程调度算法地代码.docx(23页珍藏版)》请在冰点文库上搜索。
操作系统的五种进程调度算法地代码
进程调度算法的模拟实现
⏹实验目的
1.本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
2.利用程序设计语言编写算法,模拟实现先到先服务算法FCFS、轮转调度算法RR、最短作业优先算法SJF、优先级调度算法PRIOR、最短剩余时间优先算法SRTF。
3.进行算法评价,计算平均等待时间和平均周转时间。
⏹实验内容及结果
1.先来先服务算法
2.轮转调度算法
3.优先级调度算法
4.最短时间优先算法
5.最短剩余时间优先算法
⏹实验总结
在此次模拟过程中,将SRTF单独拿了出来用指针表示,而其余均用数组表示。
⏹完整代码
【Srtf.cpp代码如下:
】
//最短剩余时间优先算法的实现
#include
#include
#include
typedefstruct
{
intremain_time;//进程剩余执行时间
intarrive_time;//进程到达时间
intTp;//进入就绪队列的时间
intTc;//进入执行队列的时间
intTo;//进程执行结束的时间
intnumber;//进程编号
}Process_Block;//定义进程模块
typedefstruct_Queue
{
Process_BlockPB;
struct_Queue*next;
}_Block,*Process;//定义一个进程模块队列中结点
typedefstruct
{
Processhead;//队列头指针
Processend;//队列尾指针
}Process_Queue;//进程队列
Process_QueuePQ;//定义一个全局队列变量
intt;//全局时间
ProcessRun_Now;//当前正在运行的进程,作为全局变量
voidInitQueue(Process_QueuePQ)
{
PQ.head->next=NULL;
PQ.end->next=PQ.head;
}/*初始化队列*/
intIsEmpty(Process_QueuePQ)
{
if(PQ.end->next==PQ.head)
return1;//队列空的条件为头指针指向尾指针并且尾指针指向头指针
else
return0;
}/*判定队列是否为空队列*/
voidEnQueue(Process_QueuePQ,ProcessP)
{
Processtemp=(Process)malloc(sizeof(_Block));
temp=PQ.end;
temp->next->next=P;
PQ.end->next=P;
}/*插入队列操作*/
ProcessDeQueue(Process_QueuePQ)
{
if(IsEmpty(PQ))
returnNULL;
Processtemp=PQ.head->next;
PQ.head->next=temp->next;
if(PQ.end->next==temp)
PQ.end->next=PQ.head;
returntemp;
}/*出列操作*/
ProcessShortestProcess(Process_QueuePQ)
{
if(IsEmpty(PQ))//如果队列为空,返回
{
if(!
Run_Now)
returnNULL;
else
returnRun_Now;
}
Processtemp,shortest,prev;
intmin_time;
if(Run_Now)//如果当前有进程正在执行,
{
shortest=Run_Now;//那么最短进程初始化为当前正在执行的进程,
min_time=Run_Now->PB.remain_time;
}
else//如果当前没有进程执行,
{
shortest=PQ.head->next;//则最短进程初始化为队列中第一个进程
min_time=PQ.head->next->PB.remain_time;
}
temp=PQ.head;
prev=temp;
while(temp->next)
{
if(temp->next->PB.remain_time{
shortest=temp->next;//则保存当前进程,
min_time=shortest->PB.remain_time;
prev=temp;//及其前驱
}
temp=temp->next;
}
if(shortest==PQ.end->next)//如果最短剩余时间进程是队列中最后一个进程,
PQ.end->next=prev;//则需要修改尾指针指向其前驱
prev->next=shortest->next;//修改指针将最短剩余时间进程插入到队头
returnshortest;
}/*调度最短剩余时间的进程至队头*/
voidRun()
{
Run_Now->PB.remain_time--;//某一时间运行它的剩余时间减
return;
}/*运行函数*/
voidWait()
{
return;
}
intsum(intarray[],intn)
{
inti,sum=0;
for(i=0;isum+=array[i];
returnsum;
}
intmain()
{
PQ.head=(Process)malloc(sizeof(_Block));
PQ.end=(Process)malloc(sizeof(_Block));
Run_Now=(Process)malloc(sizeof(_Block));
Run_Now=NULL;
InitQueue(PQ);
inti,N,Total_Time=0;//Total_Time为所有进程的执行时间之和
printf("请输入计算机中的进程数目:
\n");
scanf("%d",&N);
Process*P,temp;
P=(Process*)malloc(N*sizeof(Process));
int*wt,*circle_t;
wt=(int*)malloc(N*sizeof(int));
circle_t=(int*)malloc(N*sizeof(int));
for(i=0;i{
P[i]=(Process)malloc(sizeof(_Block));
P[i]->PB.number=i+1;
P[i]->next=NULL;
wt[i]=0;
circle_t[i]=0;
printf("输入第%d个进程的到达时间及剩余执行时间:
\n",i+1);
scanf("%d%d",&P[i]->PB.arrive_time,&P[i]->PB.remain_time);
}
for(i=0;iTotal_Time+=P[i]->PB.remain_time;
printf("\n进程按顺序运行依次为:
\n");
i=0;
intk=0;
for(t=0;;t++)
{
if(Run_Now)//如果当前有进程正在执行
{
Run();
if(t==P[i]->PB.arrive_time)//如果当前时间正好有进程进入
{
if(P[i]->PB.remain_timePB.remain_time)
{
temp=P[i];
P[i]=Run_Now;
Run_Now=temp;//则调度它至运行队列中,
Run_Now->PB.Tp=t;
Run_Now->PB.Tc=t;
wt[Run_Now->PB.number-1]+=Run_Now->PB.Tc-Run_Now->PB.Tp;
printf("%d",Run_Now->PB.number);
}
EnQueue(PQ,P[i]);//并将当前运行进程重新插入队列中
P[i]->PB.Tp=t;
k++;
i=(i+1)>(N-1)?
(N-1):
(i+1);
}
if(Run_Now->PB.remain_time==0)//如果当前进程运行结束,
{
Run_Now->PB.To=t;//进程运行结束的时间
circle_t[Run_Now->PB.number-1]+=t-Run_Now->PB.arrive_time;
free(Run_Now);//则将它所占资源释放掉,
Run_Now=NULL;//并修改Run_Now为NULL
Run_Now=ShortestProcess(PQ);//从就绪队列中调出最短剩余时间进程至队头,
if(!
Run_Now)//如果队列为空,转为等待状态
{
if(IsEmpty(PQ)&&k>=N)break;
Wait();
continue;
}
else
{
Run_Now->PB.Tc=t;
wt[Run_Now->PB.number-1]+=Run_Now->PB.Tc-Run_Now->PB.Tp;
printf("%d",Run_Now->PB.number);
}
}
}
else//如果当前运行进程为空,那么
{
if(t==P[i]->PB.arrive_time)//如果正好这时有进程入队
{
k++;
EnQueue(PQ,P[i]);
Run_Now=DeQueue(PQ);//则直接被调入运行队列中
Run_Now->PB.Tp=t;
Run_Now->PB.Tc=t;
printf("%d",Run_Now->PB.number);
i=(i+1)>(N-1)?
(N-1):
(i+1);
}
else
{
Wait();
continue;
}
}
}
printf("\n");
printf("平均等待时间是:
\n%f\n",((float)sum(wt,N))/N);
printf("平均周转时间是:
\n%f\n",((float)sum(circle_t,N))/N);
return0;
}
//////////////////////////////////////////////////////
【Process.cpp代码如下:
】
#include
#include
usingnamespacestd;
classProcess
{
public:
stringProcessName;//进程名字
intTime;//进程需要时间
intleval;//进程优先级
intLeftTime;//进程运行一段时间后还需要的时间
};
voidCopy(Processproc1,Processproc2);//把proc2赋值给proc1
voidSort(Processpr[],intsize);//此排序后按优先级从大到小排列
voidsort1(Processpr[],intsize);//此排序后按需要的cpu时间从小到大排列
voidFcfs(Processpr[],intnum,intTimepice);//先来先服务算法
voidTimeTurn(Processprocess[],intnum,intTimepice);//时间片轮转算法
voidPriority(Processprocess[],intnum,intTimepice);//优先级算法
voidmain()
{
inta;
cout<cout<<"选择调度算法:
"<cout<<"1:
FCFS2:
时间片轮换3:
优先级调度4:
最短作业优先5:
最短剩余时间优先"<cin>>a;
constintSize=30;
Processprocess[Size];
intnum;
intTimePice;
cout<<"输入进程个数:
"<cin>>num;
cout<<"输入此进程时间片大小:
"<cin>>TimePice;
for(inti=0;i{
stringname;
intCpuTime;
intLeval;
cout<<"输入第"<
"<cin>>name;
cin>>CpuTime>>Leval;
process[i].ProcessName=name;
process[i].Time=CpuTime;
process[i].leval=Leval;
cout<}
for(intk=0;kprocess[k].LeftTime=process[k].Time;//对进程剩余时间初始化
cout<<"(说明:
在本程序所列进程信息中,优先级一项是指进程运行后的优先级!
!
)";
cout<cout<<"进程名字"<<"共需占用CPU时间"<<"还需要占用时间"<<"优先级"<<"状态"<if(a==1)
Fcfs(process,num,TimePice);
elseif(a==2)
TimeTurn(process,num,TimePice);
elseif(a==3)
{
Sort(process,num);
Priority(process,num,TimePice);
}
else//最短作业算法,先按时间从小到大排序,再调用Fcfs算法即可
{
sort1(process,num);
Fcfs(process,num,TimePice);
}
}
voidCopy(Processproc1,Processproc2)
{
proc1.leval=proc2.leval;
proc1.ProcessName=proc2.ProcessName;
proc1.Time=proc2.Time;
}
voidSort(Processpr[],intsize)//以进程优先级高低排序
{//直接插入排序
for(inti=1;i{
Processtemp;
temp=pr[i];
intj=i;
while(j>0&&temp.leval{
pr[j]=pr[j-1];
j--;
}
pr[j]=temp;
}//直接插入排序后进程按优先级从小到大排列
for(intd=size-1;d>size/2;d--)
{
Processtemp;
temp=pr[d];
pr[d]=pr[size-d-1];
pr[size-d-1]=temp;
}//此排序后按优先级从大到小排列
}
/*最短作业优先算法的实现*/
voidsort1(Processpr[],intsize)//以进程时间从低到高排序
{//直接插入排序
for(inti=1;i{
Processtemp;
temp=pr[i];
intj=i;
while(j>0&&temp.Time{
pr[j]=pr[j-1];
j--;
}
pr[j]=temp;
}
}
/*先来先服务算法的实现*/
voidFcfs(Processprocess[],intnum,intTimepice)
{//process[]是输入的进程,num是进程的数目,Timepice是时间片大小
while(true)
{
if(num==0)
{
cout<<"所有进程都已经执行完毕!
"<exit
(1);
}
if(process[0].LeftTime==0)
{
cout<<"进程"<"<for(inti=0;iprocess[i]=process[i+1];
num--;
}
elseif(process[num-1].LeftTime==0)
{
cout<<"进程"<"<num--;
}
else
{
cout<process[0].LeftTime=process[0].LeftTime-Timepice;
process[0].leval=process[0].leval-1;
cout<<""<cout<cout<for(ints=1;s{
cout<<""<cout<}
}//else
cout<system("pause");
cout<}//while
}
/*时间片轮转调度算法实现*/
voidTimeTurn(Processprocess[],intnum,intTimepice)
{
while(true)
{
if(num==0)
{
cout<<"所有进程都已经执行完毕!
"<exit
(1);
}
if(process[0].LeftTime==0)
{
cout<<"进程"<"<for(inti=0;iprocess[i]=process[i+1];
num--;
}
if(process[num-1].LeftTime==0)
{
cout<<"进程"<"<num--;
}
elseif(process[0].LeftTime>0)
{
cout<process[0].LeftTime=process[0].LeftTime-Timepice;
process[0].leval=process[0].leval-1;
cout<<""<cout<cout<for(ints=1;s{
cout<<""<cout<if(s==1)
cout<<"就绪"<else
cout<<"等待"<}
Processtemp;
temp=process[0];
for(intj=0;jprocess[j]=process[j+1];
process[num-1]=temp;
}//else
cout<system("pause");
cout<}//while
}
/*优先级调度算法的实现*/
voidPriority(Processprocess[],intnum,intTimepice)
{
while(true)
{
if(num==0)
{
cout<<"所有进程都已经执行完毕!
"<