#include <iostream>
#include <string>
#define CPUSOURCE 10;
using namespace std;
class PCB
{
public:
char Pname;
int Pdengji;
int Ptime;
int Pruntime;
int Pusetime;
char Pstatu;
PCB(char,int,int,int,int,char);
int Pprint(PCB p);
};//pcb对象
typedef struct QNode
{
PCB p;
struct QNode *next;
}QNode,* QueuePtr;//队列节点的结构
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;//指针结构
//构造一个空队列。
int InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(0);
Q.front->next=NULL;
return 1;
}
//插入PCB e;
void EnQueue(LinkQueue &Q,PCB e)
{
QueuePtr a;
a=(QueuePtr)malloc(sizeof(QNode));
if(!a) exit(0);
a->p.Pname=e.Pname;
a->p.Pdengji=e.Pdengji;
a->p.Ptime=e.Ptime;
a->p.Pruntime=e.Pruntime;
a->p.Pusetime=e.Pusetime;
a->p.Pstatu=e.Pstatu;
a->next=NULL;
Q.rear->next=a;
Q.rear=a;
}
//选择进程队列中等级最高的进程进入处理器
int SELQueue(LinkQueue &Q)
{
int i=0;
int j=0;
int num;
char name;
int a[5]={0,0,0,0,0};//存储进程等级
char b[5]={' ',' ',' ',' ',' '};//存储进程名。
char c=' ';
if(Q.front==Q.rear)
{
cout<<"无进程元素可供选择,进程队列为空."<<endl;
return 0;
}
Q.rear=Q.front;
do
{
//cout<<"@@@@@@@@@@@##########"<<endl;
if(Q.rear->next->p.Pstatu=='w')
{
a[i]=Q.rear->next->p.Pdengji;
b[i]=Q.rear->next->p.Pname;
cout<<a[i]<<" "<<b[i];
i++;
j++;
}
Q.rear=Q.rear->next;
}
while(Q.rear->next!=NULL);
if(a[0]>=a[1]&&a[0]>=a[2]&&a[0]>=a[3]&&a[0]>=a[4])
name=b[0];
else if(a[1]>=a[0]&&a[1]>=a[2]&&a[1]>=a[3]&&a[1]>=a[4])
name=b[1];
else if(a[2]>=a[0]&&a[2]>=a[1]&&a[2]>=a[3]&&a[2]>=a[4])
name=b[2];
else if(a[3]>=a[0]&&a[3]>=a[1]&&a[3]>=a[2]&&a[3]>=a[4])
name=b[3];
else if(a[4]>=a[0]&&a[4]>=a[1]&&a[4]>=a[2]&&a[4]>=a[3])
name=b[4];
Q.rear=Q.front;
while(Q.rear->next!=NULL)
{
if(name==Q.rear->next->p.Pname)
{
Q.rear->next->p.Pdengji--;
Q.rear->next->p.Pusetime++;
Q.rear->next->p.Pstatu='r';
cout<<"进程名:"<<(Q.rear->next->p).Pname<<endl;
cout<<"进程等级:"<<(Q.rear->next->p).Pdengji<<endl;
cout<<"进程进入时间:"<<(Q.rear->next->p).Ptime<<endl;
cout<<"进程运行时间:"<<(Q.rear->next->p).Pruntime<<endl;
cout<<"进程所用时间:"<<(Q.rear->next->p).Pusetime<<endl;
cout<<"进程状态:"<<(Q.rear->next->p).Pstatu<<endl;
}
Q.rear=Q.rear->next;
}
cout<<"时间片是否用完??(y/n)"<<endl;
cin>>c;
if(c=='y')
{
Q.rear=Q.front;
do
{
if(Q.rear->next->p.Pruntime==Q.rear->next->p.Pusetime)
Q.rear->next->p.Pstatu='f';
else
Q.rear->next->p.Pstatu='w';
Q.rear=Q.rear->next;
}
while(Q.rear->next!=NULL);
}
else SELQueue(Q);
}
//顺序访问队列元素
int QueQueue(LinkQueue &Q)
{
if(Q.front==Q.rear)
{
cout<<"无进程元素可打印,进程队列为空."<<endl;
return 0;
}
Q.rear=Q.front;
//Q.rear=Q.rear->next;//队列的第一个单元为空,需避开.
do
{
cout<<"进程名:"<<(Q.rear->next->p).Pname<<endl;
cout<<"进程等级:"<<(Q.rear->next->p).Pdengji<<endl;
cout<<"进程进入时间:"<<(Q.rear->next->p).Ptime<<endl;
cout<<"进程运行时间:"<<(Q.rear->next->p).Pruntime<<endl;
cout<<"进程所用时间:"<<(Q.rear->next->p).Pusetime<<endl;
cout<<"进程状态:"<<(Q.rear->next->p).Pstatu<<endl;
Q.rear=Q.rear->next;
}
while(Q.rear->next!=NULL);
}
//查看队列中的进程状态
int QQueue(LinkQueue &Q)
{
int i=0;
if(Q.front==Q.rear)
{
cout<<"无进程可查看,进程队列为空."<<endl;
return 0;
}
Q.rear=Q.front;
do
{
if(Q.rear->next->p.Pstatu=='r')
{
cout<<"处在当前处理器中的进程信息:"<<endl;
cout<<"进程名:"<<Q.rear->next->p.Pname<<endl;
cout<<"进程等级:"<<Q.rear->next->p.Pdengji<<endl;
cout<<"进程进入时间:"<<Q.rear->next->p.Ptime<<endl;
cout<<"进程运行时间:"<<Q.rear->next->p.Pruntime<<endl;
cout<<"进程所用时间:"<<Q.rear->next->p.Pusetime<<endl;
cout<<"进程状态:"<<Q.rear->next->p.Pstatu<<endl;
i=1;
}
Q.rear=Q.rear->next;
}
while(Q.rear->next!=NULL);
if(i==0)
cout<<"处理器中无进程正在被运行"<<endl;
}
/***********构造函数*************/
PCB::PCB(char n,int d,int t,int r,int u,char s)
{
Pname=n;
Pdengji=d;
Ptime=t;
Pruntime=r;
Pusetime=u;
Pstatu=s;
}
/************打印进程信息***************/
int PCB::Pprint(PCB p)
{
cout<<"进程名字:"<<p.Pname<<endl;
cout<<"进程等级:"<<p.Pdengji<<endl;
cout<<"进程进入时间:"<<p.Ptime<<endl;
cout<<"进程运行时间:"<<p.Pruntime<<endl;
cout<<"进程所用时间:"<<p.Pusetime<<endl;
cout<<"进程状态:"<<p.Pstatu<<endl;
return 0;
}
void main()
{
LinkQueue Q;
PCB p('0',0,0,0,0,'w');
PCB p1('a',15,0,2,0,'w');
PCB p2('b',13,0,5,0,'w');
PCB p3('c',16,0,3,0,'w');
PCB p4('d',15,0,10,0,'w');
int i;
int j=0;
char choice;
cout<<"^_^进程队列的第一次使用初始化^_^"<<endl;
while(j==0)
{
cout<<" 1 创建空进程队列 "<<endl;
cout<<" 2 插入进程 "<<endl;
cout<<" 3 打印所有进程 "<<endl;
cout<<" 4 进入处理器 "<<endl;
cout<<" 5 查看处理器正在处理的进程信息"<<endl;
cout<<" 6 结束程序 "<<endl;
cin>>i;
switch(i)
{
case 1:
{
InitQueue(Q);
cout<<"是否还要进行操作?(y/n)"<<endl;
cin>>choice;
if(choice=='y')
j=0;
else
j=1;
break;
}
case 2:
{
EnQueue(Q,p1);
EnQueue(Q,p2);
EnQueue(Q,p3);
EnQueue(Q,p4);
cout<<"是否还要进行操作?(y/n)"<<endl;
cin>>choice;
if(choice=='y')
j=0;
else
j=1;
break;
}
case 3:
{
QueQueue(Q);
cout<<"是否还要进行操作?(y/n)"<<endl;
cin>>choice;
if(choice=='y')
j=0;
else
j=1;
break;
}
case 4:
{
SELQueue(Q);
cout<<"是否还要进行操作?(y/n)"<<endl;
cin>>choice;
if(choice=='y')
j=0;
else
j=1;
break;
}
case 5:
{
QQueue(Q);
cout<<"是否还要进行操作?(y/n)"<<endl;
cin>>choice;
if(choice=='y')
j=0;
else
j=1;
break;
}
case 6:
{
j=1;
break;
}
}
}
}