📄 0.cpp
字号:
#include <stdlib.h>
#include <time.h>
#include <iostream.h>
struct PCB
{
int pid;
int time; //到达时间
int runtime; //运行时间
int zt; //状态
int yx; //优先级
};
void main()
{
PCB pcb[4];
//随机值初始化
srand((unsigned)time(NULL));
for(int i=0;i<4;i++)
{
pcb[i].pid=i;
pcb[i].time=rand()%30;
pcb[i].runtime=rand()%50;
pcb[i].zt=rand()%2;
pcb[i].yx=rand()%20;
}
cout<<"1.演示先来先服务"<<endl;
cout<<"2.演示时间片轮转"<<endl;
cout<<"3.演示优先级调度"<<endl;
int j,n,m;
int w=0;
cout<<"请选择:";
cin>>i;
switch(i)
{
case 1 :
for(j=0;j<4;j++)
for(n=0;n<3;n++)
{
if(pcb[n].time>pcb[n+1].time)
{
PCB temp;
temp=pcb[n];
pcb[n]=pcb[n+1];
pcb[n+1]=temp;
}
}
cout<<"PID 到达时间 运行时间 状态 优先级"<<endl;
for(j=0;j<4;j++)
cout<<pcb[j].pid<<" "<<pcb[j].time<<" "<<pcb[j].runtime<<" "<<pcb[j].zt<<" "<<pcb[j].yx<<endl;
break;
case 2:
cout<<"PID 到达时间 运行时间 状态 优先级"<<endl;
for(j=0;j<4;j++)
for(n=0;n<3;n++)
{
if(pcb[n].runtime>pcb[n+1].runtime)
{
PCB temp;
temp=pcb[n];
pcb[n]=pcb[n+1];
pcb[n+1]=temp;
}
}
m=pcb[3].runtime;
while(w!=m+1)
{
if(w==pcb[0].runtime)
cout<<pcb[0].pid<<" "<<pcb[0].time<<" "<<pcb[0].runtime<<" "<<pcb[0].zt<<" "<<pcb[0].yx<<endl;
if(w==pcb[1].runtime)
cout<<pcb[1].pid<<" "<<pcb[1].time<<" "<<pcb[1].runtime<<" "<<pcb[1].zt<<" "<<pcb[1].yx<<endl;
if(w==pcb[2].runtime)
cout<<pcb[2].pid<<" "<<pcb[2].time<<" "<<pcb[2].runtime<<" "<<pcb[2].zt<<" "<<pcb[2].yx<<endl;
if(w==pcb[3].runtime)
cout<<pcb[3].pid<<" "<<pcb[3].time<<" "<<pcb[3].runtime<<" "<<pcb[3].zt<<" "<<pcb[3].yx<<endl;
w++;
}
break;
case 3:
cout<<"PID 到达时间 运行时间 状态 优先级"<<endl;
for(j=0;j<4;j++)
for(n=0;n<3;n++)
{
if(pcb[n].yx<pcb[n+1].yx)
{
PCB temp;
temp=pcb[n];
pcb[n]=pcb[n+1];
pcb[n+1]=temp;
}
}
for(j=0;j<4;j++)
cout<<pcb[j].pid<<" "<<pcb[j].time<<" "<<pcb[j].runtime<<" "<<pcb[j].zt<<" "<<pcb[j].yx<<endl;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -