处理机调度
先来先服务
#include <iostream>
using namespace std;
const int R=1;//就绪
const int E=2;//结束
const int B=3;//阻塞
struct pcb{
string name;
int time;
int status;
pcb *next;//用于指向同一个状态的pcb队列里的下一个结点
};
pcb *R_head=new pcb,*R_tail=NULL,*B_head=new pcb,*B_tail=NULL;//两个队列的头指针与尾指针
void New(){
cout<<"新建进程......"<<endl;
pcb *process=new pcb;
if(R_head->next==NULL)
{//判断就绪队列是否为空
R_head->next=process;
}
else
{
R_tail->next=process;
}
cout<<"输入线程的名称:"<<endl;
cin>>process->name;
cout<<"输入线程的运行时间:"<<endl;
cin>>process->time;
process->status=R;
process->next=NULL;
//将尾指针指向队列的最后结点
R_tail=process;
cout<<"进程创建成功!"<<endl;
}
void Run_finish()
{
if(R_head->next==NULL)
{
cout<<"就绪队列为空"<<endl;
return;
}
pcb *del=R_head->next;
R_head->next=R_head->next->next;
cout<<"进程"<<del->name<<"运行完毕"<<endl;
delete del;
}
void Run_wait()
{
if(R_head->next==NULL)
{
cout<<"就绪队列为空"<<endl;
return;
}
pcb *process=R_head->next;
R_head->next=R_head->next->next;
cout<<"进程"<<process->name<<"运行中"<<endl;
process->status=B;
cout<<"该进程阻塞"<<endl;
if(B_head->next==NULL)
{
B_head->next=process;
}
else
{
B_tail->next=process;
}
B_tail=process;
B_tail->next=NULL;
cout<<"进程阻塞成功"<<endl;
}
void Signal()
{
if(B_head==NULL)
{
cout<<"阻塞队列为空!"<<endl;
return;
}
pcb *process=B_head->next;
B_head->next=B_head->next->next;
if(R_head->next==NULL)
{//判断就绪队列是否为空
R_head->next=process;
}
else
{
R_tail->next=process;
}
process->status=R;
process->next=NULL;
}
void Show()
{
cout<<"进程名\t"<<"指针\t"<<"进程状态\t"<<"运行时间\t"<<endl;
pcb *R_iter=R_head->next;
pcb *B_iter=B_head->next;
while(R_iter!=NULL)
{
cout<<R_iter->name<<"\t"<<R_iter->next<<"\t"<<R_iter->status<<"\t"<<R_iter->time<<endl;
R_iter=R_iter->next;
}
while(B_iter!=NULL)
{
cout<<B_iter->name<<"\t"<<B_iter->next<<"\t"<<B_iter->status<<"\t"<<B_iter->time<<endl;
B_iter=B_iter->next;
}
}
int main()
{
R_head->next=NULL;
B_head->next=NULL;
cout<<"==================欢迎使用进程控制系统========================="<<endl;
while(true)
{
cout<<"----------------------------------------------------"<<endl;
cout<<"New:新建进程\t\t"<<"Run(finish):运行进程并结束\t"<<endl;
cout<<"Run(wait):运行后阻塞\t"<<"Signal:唤醒阻塞进程\t"<<endl;
cout<<"Show:显示所有进程PCB信息\t"<<"Exit:结束"<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"请输入命令>>";
string conmand;
cin>>conmand;
if("New"==conmand)
{
New();
}
else if("Run(finish)"==conmand)
{
Run_finish();
}
else if("Run(wait)"==conmand)
{
Run_wait();
}
else if("Signal"==conmand)
{
Signal();
}
else if("Show"==conmand)
{
Show();
}
else if("Exit"==conmand)
{
break;
}
else
{
cout<<"错误的输入"<<endl;
}
}
cout<<"======================欢迎您再次使用====================="<<endl;
return 0;
}