📄 进程调度.cpp
字号:
//THIS IS THE PRIORITY ALGORITHM
//display the processes manager
//write by Li Xin
//June 23,2006
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#define P_NUM 3
#define P_PRI 40
enum state{ //枚举类型,进程的三种状态
ready,
working,
finish
};
struct pcb{ //结构体,pcb的结构
char name[4];
int priority;
int cputime;
int needtime;
state process;
pcb * next;
};
pcb * get_process(){ //初始化进程
pcb *q;
pcb *t;
pcb *p;
int i=0;
cout<<"input ProcessNames and ProcessTime"<<endl;
while (i<P_NUM){
q=(struct pcb *)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->priority=P_PRI-q->needtime;
q->process=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<<"name"<<" "<<"cputime"<<" "<<"needtime"<<" "<<"priority"<<" "<<"state"<<endl;
while(p){
cout<<p->name;
cout<<" ";
cout<<p->cputime;
cout<<" ";
cout<<p->needtime;
cout<<" ";
cout<<p->priority;
cout<<" ";
switch(p->process){
case ready:cout<<"ready"<<endl;break;
case working:cout<<"working"<<endl;break;
case finish:cout<<"finish"<<endl;break;
}
p=p->next;
}
cout<<"_______________________________________________________"<<endl;
}
int process_finish(pcb *q){ //判断进程是否完成
int b=1;
while(b&&q){
b=(b&&q->needtime==0);
q=q->next;
}
return b;
}
void cpuexe(pcb *q){ //进程一次的运行
pcb *t=q;
int c=0;
while(q){
if (q->process!=finish){
q->process=ready;
if(q->needtime==0){
q->process=finish;
}
}
if(c<q->priority&&q->process!=finish){
c=q->priority;
t=q;
}
q=q->next;
}
if(t->needtime!=0){
t->priority-=3;
t->needtime--;
t->process=working;
t->cputime++;
}
}
void priority_cal(){ //优先数程序
pcb * p;
p=get_process();
int cpu=0;
while(!process_finish(p)){
cpu++;
cout<<"cputime:"<<cpu<<endl;
cpuexe(p);
display(p);
}
printf("All Processes Have Finished!");
}
void display_menu(){ //显示菜单
cout<<"*****************THIS IS THE PRIORITY ALGORITHM******************"<<endl;
cout<<" ONLY DISPALY 3 PROCESSes! "<<endl;
cout<<" 1 : ENTER 2 : EXIT "<<endl;
cout<<"*****************************************************************"<<endl;
}
void main(){
display_menu();
int a;
cin>>a;
switch(a){
case 1:priority_cal();break;
case 2:break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -