📄 process.cpp
字号:
//
//程序名称:进程调度模拟程序
//程序作者:张焕人
//作者邮箱: renwairen369@yahoo.com.cn
// renwairen369@hotmail.com
//作者QQ:27949278
//
//
//
#include <iostream.h>
#include <stdio.h>
typedef struct pcb
{
char name[10]; //进程名
char state; //状态w(就绪)r(运行)f(结束)
int super; //优先级
int atime; //到达时间
int ntime; //需运行的时间
int rtime; //已运行的时间
struct pcb *next;
}*pcb1;
int time=0;
int alg=1;
pcb1 s,w;//s,w分别是就绪队列、阻塞队列的节点
void print(pcb1 p)
//输出就绪队列信息
{
pcb1 p0; //c语言当中指针传递直接传地址!!!
if(p==NULL) cout<<" 当前队列为空"<<endl;
else
{
p0=p;
cout<<" "<< p0->name;
p0=p0->next;
while(p0!=NULL)
{
cout<<" -> ";
cout<<p0->name;
p0=p0->next;
}
cout<<endl;
}
}
void printpcb()
//输出pcb信息
{
pcb1 p0;
if(s==NULL&&w==NULL) cout<<" 当前没有进程处于调度中"<<endl;
else
{
cout<<"当前处于调度中进程的PCB信息:"<<endl;
cout<<"\t进程名 "<<"\t优先级 "<<"\t状态"<<"\t到达时间 "<<"\t已运行时间 "<<"\t需运行时间"<<endl;
p0=s;
while(p0!=NULL)
{
cout<<"\t"<<p0->name<<"\t "<<p0->super<<"\t "<<p0->state<<"\t "<<p0->atime<<"\t\t "<<p0->rtime<<"\t\t "<<p0->ntime<<endl;
p0=p0->next;
}
p0=w;
while(w!=NULL)
{
cout<<"\t"<<p0->name<<"\t "<<p0->super<<"\t "<<p0->state<<"\t "<<p0->atime<<"\t\t "<<p0->rtime<<"\t\t "<<p0->ntime<<endl;
p0=p0->next;
}
}
}
//check the queue if empty
bool empty(pcb1 r)
//队列为空返回true,否则返回false
{
if(r==NULL)
return true;
else
return false;
}
void sort0(pcb1 &r,pcb1 p)
{
pcb1 p0=r;
bool in=false;
if(r==NULL)//队列为空
{
r=p;
}
else
{
while(p0->next!=NULL) p0=p0->next;
p0->next=p;
}
}
void sort(pcb1 &r,pcb1 p)
//根据优先级将进程排序,插入到就绪队列或阻塞队列当中
{
pcb1 p1,p2;
bool in=false;
if(r==NULL)//队列为空
{
r=p;
}
else
{
if(p->super>=r->super)//待插入的进程优先级比队列第一个进程优先级要高
{
p->next=r;
r=p;
}
else
{
p1=r;
p2=r->next;
if(p2==NULL)//only one process in the queue
{
r->next=p;
}
else
{
while(in==false&&p2!=NULL)//insert to the middle of the queue
{
if(p->super>=p2->super)
{
p->next=p2;
p1->next=p;
in=true;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
}
if(!in)//如果进程没有插入到队列之中(优先级最小)将进程插入到就绪队列最后
p1->next=p;
}
}
}
void block()
//将进程阻塞并插入到阻塞队列当中block one process and insert to block queue
{
if(!empty(s)) //就绪队列不空
{
if(s->next==NULL)
{
if(alg) sort(w,s);
else sort0(w,s);
s=s->next;
}
else
{
pcb1 p1;
p1=s;
s=s->next;
p1->next=NULL;
if(alg) sort(w,p1);
else sort0(w,p1);
}
}
else
{
cout<<endl<<endl;
cout<<" 现在就绪队列已经为空,再没有进程需要阻塞"<<endl;
}
}
void wake()
//唤醒阻塞队列当中的一个进程并插入到就绪队列当中
{
if(!empty(w)) //就绪队列不空
{
pcb1 p1;
p1=w;
w=w->next;
p1->next=NULL;
if(alg) sort(s,p1);
else sort0(s,p1);
}
else
{
cout<<endl<<endl;
cout<<" 阻塞队列已经为空,没有进程再需要唤醒"<<endl;
}
}
bool finished()
//检查就绪队列的第一个进程是否完成
{
pcb1 p;
p=s;
if(p->rtime==p->ntime)
{
p->state='F';//进程运行结束
cout<<endl<<" 进程"<<p->name<<" 已经结束"<<endl<<endl;
return true;
}
else
return false;
}
void run()
{
time++;
if(!empty(s)) //就绪队列不空
{
pcb1 p;
p=s;
if(!finished()) //就绪队列队首进程是否运行结束
{//没有运行结束
s=s->next;
p->rtime++;
p->super--;
p->next=NULL;
if(alg) sort(s,p);
else sort0(s,p);
}
else
{//已经运行结束
s=s->next;
delete p;
}
}
else //就绪队列为空
{
cout<<endl<<" 就绪队列已经为空"<<endl<<endl;
}
cout<<"就绪队列的信息:"<<endl;
print(s);
printpcb();
printf("%s","按任意键继续...");
getchar();
}
void creat()
//创建进程
{
pcb1 p;
p=new pcb;
cout<<"请输入进程的相应信息"<<endl;
cout<<" 进程名: ";
cin>>p->name;
cout<<" 进程优先级: ";
cin>>p->super;
// cout<<"程序到达时间时间: ";
// cin>>p->atime;
p->atime=time;
cout<<" 需要运行时间: ";
cin>>p->ntime;
p->atime=time;
p->rtime=0;
p->state='W'; //就绪状态
p->next=NULL;
if(alg) sort(s,p);//按优先级将进程p插入到就绪队列当中
else sort0(s,p);
}
void menu()
{
cout<<endl<<" 进程调度程序"<<endl<<endl;
cout<<" 1. 创建进程 2. 运行进程 "<<endl;
cout<<" 3. 阻塞进程 4. 唤醒进程 "<<endl;
cout<<" 5. 查看就绪队列 6. 查看阻塞队列 " <<endl;
cout<<" 7. 查看进程 PCB 8. 退出程序 "<<endl<<endl;
}
//main function
void main()
{
char ch;
s=NULL;
w=NULL;
cout<<"请选择用于调度的算法 0.先来先服务 1.优先权算法: ";
cin>>alg;
menu();
cout<<"请选择相应功能: ";
cin>>ch;
while(ch!='8')
{
switch(ch)
{
case '1': creat(); break;
case '2': run(); break;
case '5': cout<<"就绪队列进程信息:"<<endl;
print(s);
printf("%s","按任意键继续...");
getchar();
break;
case '6': cout<<"阻塞队列进程信息:"<<endl;
print(w);
printf("%s","按任意键继续...");
getchar();
break;
case '7': printpcb();
printf("%s","按任意键继续...");
getchar();
break;
case '4': wake(); break;
case '3': block(); break;
}
menu();
cout<<"请选择相应功能: ";
cin>>ch;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -