📄 czxtjcdd.cpp
字号:
#include<iostream.h>
#include <malloc.h>
#define P_NUM 5
#define P_TIME 20
enum state{ready, execute, block, finish };//设置进程的四种状态
struct pcb
{
char name[4];
int priority;
int cputime;
int needtime;
int count;
int round;
state status;
pcb * next;
};
//创建进程块
pcb * get_process()
{
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"请输入进程的名称和所需的运行时间(共五个):"<<endl;
while (i<P_NUM)
{
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->priority=P_TIME-q->needtime;
q->status=ready;
q->next=NULL;
if (i==0)
{
p=q;
t=q;
}
else
{
t->next=q;
t=q;
}
i++;
}
return p;
}
//显示各进程在不同的运行次数时的属性(如进程名,运行状态等)
void display(pcb *p)
{
cout<<"进程名"<<" "<<"运行次数"<<" "<<"所需时间"<<" "<<"优先级"<<" "<<"状态"<<endl;
while(p)
{
cout<<p->name<<" ";
cout<<p->cputime<<" ";
cout<<p->needtime<<" ";
cout<<p->priority<<" ";
switch(p->status)
{
case ready: cout<<"ready"<<endl; break;
case execute: cout<<"execute"<<endl; break;
case block: cout<<"block"<<endl; break;
case finish: cout<<"finish"<<endl; break;
}
p=p->next;
}
}
//判断所有进程是否已全部完成
int process_finish(pcb *q)
{
int bl=1;
while(bl&&q)
{
bl=bl&&q->needtime==0;
q=q->next;
}
return bl;
}
//确定CPU执行哪一个程序,即根据各进程的优先级来分配CPU
void cpuexe(pcb *q)
{
pcb *t=q;
int tp=0;
while(q){
if (q->status!=finish)
{
q->status=ready;
if(q->needtime==0)
{
q->status=finish;
}
}
if(tp<q->priority&&q->status!=finish)
{
tp=q->priority;
t=q;
}
q=q->next;
}
if(t->needtime!=0)
{
t->priority-=3;
t->needtime--;
t->status=execute;
t->cputime++;
}
}
void display_menu()
{
cout<<"请选择调度算法:"<<endl;
cout<<"1 优先级调度算法"<<endl;
cout<<"2 时间片轮转调度算法"<<endl;
cout<<"3 退出"<<endl;
}
//获取下一个进程
pcb * get_next(pcb * k,pcb * head)
{
pcb * t;
t=k;
do
{
t=t->next;
}
while (t && t->status==finish);
if(t==NULL)
{
t=head;
while (t->next!=k && t->status==finish)
{
t=t->next;
}
}
return t;
}
//根据运行过程中各进程的运行情况,重新设置各进程的状态
void set_status(pcb *p)
{
while(p)
{
if (p->needtime==0)
{
p->status=finish;
}
if (p->status==execute)
{
p->status=ready;
}
p=p->next;
}
}
pcb * get_process_round()
{
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"请输入进程的名称和所需的运行时间(共五个):"<<endl;
while (i<P_NUM)
{
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->round=0;
q->count=0;
q->status=ready;
q->next=NULL;
if (i==0)
{
p=q;
t=q;
}
else
{
t->next=q;
t=q;
}
i++;
} //while
return p;
}
void cpu_round(pcb *q)
{
q->cputime+=1;
q->needtime-=1;
if(q->needtime<0)
{
q->needtime=0;
}
q->count++;
q->round++;
q->status=execute;
}
//显示进程情况
void display_round(pcb *p)
{
cout<<"进程名"<<" "<<"运行次数"<<" "<<"所需时间"<<" "<<"计数"<<" "<<"轮数"<<" "<<"状态"<<endl;
while(p)
{
cout<<p->name;
cout<<" ";
cout<<p->cputime;
cout<<" ";
cout<<p->needtime;
cout<<" ";
cout<<p->count;
cout<<" ";
cout<<p->round;
cout<<" ";
switch(p->status)
{
case ready:cout<<"ready"<<endl;break;
case execute:cout<<"execute"<<endl;break;
case finish:cout<<"finish"<<endl;break;
}
p=p->next;
}
}
//优先级进程调度算法
void priority_cal()
{
pcb * p;
p=get_process();
int cpu=0;
while(!process_finish(p))
{
cpu++;
cout<<"运行次数:"<<cpu<<endl;
cpuexe(p);
display(p);
}
cout<<("所有的进程都完成了!");
}
//时间片轮转调度算法
void round_cal()
{
pcb * p;
pcb * r;
p=get_process_round();
int cpu=0;
r=p;
while(!process_finish(p))
{
cpu+=1;
cpu_round(r);
r=get_next(r,p);
cout<<"运行的次数:"<<cpu<<endl;
display_round(p);
set_status(p);
}
}
//主函数
void main()
{
display_menu();
int k;
cin>>k;
switch(k)
{
case 1:priority_cal();break;
case 2:round_cal();break;
case 3:break;
display_menu();
cout<<k;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -