#define MAXQSIZE 100 //最大队列长度
#define ERROR 0
#define OVERFLOW 1
#define OK 2
#define STATUS int
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
typedef struct process{
int Name; //进程名
int NeedTime; //需要执行时间
int OverTime; //已执行时间
int Status; //进程状态:0表示等待,1表示就绪,2表示正在执行
int Priority; // 数字越小优先级越高
}PCB;
typedef struct Queue{
PCB* base[MAXQSIZE];// 数据域,存放队列中的PCB
int front; //头指针,若队列不空,指向队列头元素
int rear; //尾指针,若队列不空,指向队列尾元素的下一个位置
}Queue;
Queue ready,wait,run; //分别定义就绪队列、等待队列、运行队列
STATUS InitQueue (Queue &Q) // 构造一个最大存储空间为MAXQSIZE的空队列Q
{
if (Q.base == NULL)
exit (OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
STATUS EnQueue (Queue &Q, PCB* e) // 插入元素e为Q的新的队尾元素,并返回OK;否则返回ERROR
{
if (Q.rear+1 == Q.front)
return ERROR;
Q.base[Q.rear] = e;
Q.rear = Q.rear+1;
return OK;
}
STATUS DeQueue (Queue &Q, PCB* &e) //删除Q的队头元素,用e返回其值,并返回OK; 否则返回ERROR
{
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = Q.front+1;
return OK;
}
void Print(PCB *s) //打印单个进程的情况
{
cout<<s->Name<<" "<<s->Priority<<" "<<s->NeedTime<<" "<<s->OverTime<<" "<<s->Status<<endl;
}
void PrintQueue(Queue s) //打印整个队列
{
int n;
n = s.front;
while(n < s.rear)
{
Print(s.base[n]);
n++;
}
}
void initial(Queue &wait,Queue &ready)//进程初始化
{
int i;
cout<<"初始进程队列如下:"<<endl;
cout<<"进程名 优先级 所需时间 已执行时间 进程状态"<<endl;
srand((unsigned)time(NULL)); //让随机数不重复
for(i=1;i<=10;i++)
{
PCB *s;
s = (PCB *)malloc(sizeof(PCB));//分配空间
if(!s)
exit(1);
s->Name = i-1; //赋初值
s->NeedTime = rand()%30+10;
s->Status = rand()%2;
s->Priority = rand()%10;//优先级
s->OverTime = 0;//已执行时间初始为0
if(s->Status == 0)//根据初始status入队
EnQueue(wait,s);
else
EnQueue(ready,s);
Print(s);
}
}
void FCFS(Queue &wait,Queue &ready,Queue &run)//FCFS
{
srand((unsigned)time(0));
int t=rand()%20+10; //进程轮转时间
PCB *temp;
for(;(wait.front != wait.rear) || (ready.front != ready.rear);)//ready,wait均非空
{
if( ready.front < ready.rear )//ready非空
{
DeQueue(ready,temp);
temp->Status = 2;
EnQueue(run,temp); //ready队头元素入队列run
cout<<"当前执行进程"<<run.base[run.front]->Name<<",该进程能够执行的时间为"<<t<<endl;
}
else //ready为空
cout<<"就绪队列为空"<<endl;
if((wait.front < wait.rear) && (rand()%2 == 1) )//wait非空,且随机数为1
{
DeQueue(wait,temp);
temp->Status = 1;
EnQueue(ready,temp);
}
cout<<"进程名 优先级 所需时间 已执行时间 进程状态"<<endl;
PrintQueue(run); //打印3个队列
PrintQueue(ready);
PrintQueue(wait);
if(run.front != run.rear)//run非空
if( run.base[run.front]->NeedTime - run.base[run.front]->OverTime <= t )
DeQueue(run,temp);//进程已执行完毕
else //时间已到,未执行完
{
DeQueue(run,temp);
temp->OverTime = t + temp->OverTime;
if(rand()%2 == 1)//进ready还是进wait
{
temp->Status = 0;
EnQueue(wait,temp);
}
else
{
temp->Status = 1;
EnQueue(ready,temp);
}
}
}
}
void Priority(Queue &wait,Queue &ready,Queue &run)//Priority
{
PCB *temp;
int n;
srand((unsigned)time(0));
int t=rand()%20+10;//进程轮转时间
for(;(wait.front != wait.rear) || (ready.front != ready.rear);)//ready,wait均非空
{
if( ready.front != ready.rear )//ready非空
{
DeQueue(ready,temp);
n = ready.front;
while(n < ready.rear)//选择优先级最高的,数字越小优先级越高
{
if(temp->Priority > ready.base[n]->Priority)
{
EnQueue(ready,temp);
DeQueue(ready,temp);
n++;
}
else
n++;
}
temp->Status = 2;
EnQueue(run,temp);
cout<<"当前执行进程"<<run.base[run.front]->Name<<",该进程能够执行的时间为"<<t<<endl;
}
else//ready为空
cout<<"就绪队列为空"<<endl;
if((wait.front < wait.rear) && (rand()%2 == 1) )//wait非空,且随机数为1
{
DeQueue(wait,temp);
temp->Status = 1;
EnQueue(ready,temp);
}
cout<<"进程名 优先级 所需时间 已执行时间 进程状态"<<endl;
PrintQueue(run);//打印3个队列
PrintQueue(ready);
PrintQueue(wait);
if(run.front != run.rear)//run非空
if( run.base[run.front]->NeedTime - run.base[run.front]->OverTime <= t )
DeQueue(run,temp);//进程已执行完毕
else//时间已到,未执行完
{
DeQueue(run,temp);
temp->OverTime = t + temp->OverTime;
if(rand()%2 == 1)//进ready还是进wait
{
temp->Status = 0;
EnQueue(wait,temp);
}
else
{
temp->Status = 1;
EnQueue(ready,temp);
}
}
}
}
void Shortage(Queue &wait,Queue &ready,Queue &run)//Shortage
{
PCB *temp;
int n;
srand((unsigned)time(0));
int t=rand()%20+10;
for(;(wait.front != wait.rear) || (ready.front != ready.rear);)//ready,wait均非空
{
if( ready.front != ready.rear )//ready非空
{