处理机调度

#include<iostream>
#include<string>
#define MAXSIZE 10

using namespace std;

struct PCB
{
	int name;
	//进程名
	int priority;
	//进程优先数
	int time;
	//进程执行时间
};

struct Queue_Process
{
	PCB data[MAXSIZE]; 
	//PCB 队列
	int front;
	// 队首
	int rear;
	// 队尾
};

void InitQueue(Queue_Process *Q)
{
	Q->front=Q->rear=0;
}

bool IsQueueEmpty(Queue_Process Q) //队空判断函数
{
	return(Q. front == Q. rear)? true:false;
}

bool IsQueueFull(Queue_Process Q) //队满判断函数
{
	return(Q. front ==(Q. rear+1)%MAXSIZE)? true:false;
}

void EnQueue(Queue_Process *Q,PCB x) //入队函数
{
	if(IsQueueFull(*Q)) // 判断队列是否为满
	{
		cout<<"队满,入队操作失败! "<<endl;
		exit(0);
	}
	//队列非满,将PCB入队,将其中信息填入
	Q->data[Q->rear]. name = x. name;
	Q->data[Q->rear]. priority= x. priority;
	Q->data[Q->rear]. time = x. time;
	Q->rear=(Q->rear+1)%MAXSIZE; //队列队尾指针后移
}

void DeleQueue(Queue_Process *Q)
{
	if(IsQueueEmpty(*Q)) //判断队列是否为空
	{
		cout<<"队空,出队操作失败!"<<endl;
		exit(0);
	}
	Q->front=(Q->front+1)%MAXSIZE;//将队列首指针后移
}

void SortPCB(PCB *pcb,int n) //PCB 优先数大小从大到小排列函数
{
	PCB temp;
	bool exchange = true; //交换标志
	for(int i =n-1; i>0&& exchange; i--) //冒泡排序算法排序
	{
		exchange = false;
		for(int j =0; j<i; j++)
		{
			if(pcb[j].priority <pcb[j+1].priority) //交换PCB中的数据
			{
				temp.name = pcb[j].name;
				temp.priority =pcb[j].priority;
				temp.time = pcb[j].time;
				pcb[j].name = pcb[j+1].name;
				pcb[j].priority = pcb[j+1].priority;
				pcb[j].time = pcb[j+1].time;
				pcb[j+1].name = temp.name;
				pcb[j+1].priority =temp.priority;
				pcb[j+1].time = temp.time;
				exchange = true;
			}
		}
	}
}

void Input(PCB*pcb,int n) //进程信息输入函数
{
	cout<<"请输入每个进程的优先数和执行时间:"<<endl;
	int p,t;
	for(int i=0; i< n; i++) //输入每个进程的优先数和执行时间
	{
		cin>>p>>t;
		pcb[i].name = i;
		pcb[i].priority =p;
		pcb[i].time = t;
	}
}

void Display(Queue_Process *queue)//输出每个时刻的进程运行情况函数{
{
	cout<<" 进程名\t"<<"\t优先数\t"<<"\t执行时间\t"<<"进程状态"<<endl<<endl;
	do
	{
		if(queue->data[queue->front].time >1)
		{
			cout<<" "<<queue->data[queue->front].name
			    <<"\t\t"<<queue->data[queue->front].priority
				<<"\t\t"<<queue->data[queue->front].time
				<<"\t\t"<<"执行中..."<<endl;
			queue->data[queue->front].priority -= 1;
			queue->data[queue->front].time -= 1;
			for(int i = 1; i < queue->rear-queue->front; i++)
				cout<<" "<<queue->data[queue->front+i].name
				<<"\t\t"<<queue->data[queue->front+i].priority
				<<"\t\t"<<queue->data[queue->front+i].time
				<<"\t\t"<<"等待中..."<<endl;
			cout<<endl;
		}
		else
		{
			cout<<" "<<queue->data[queue->front].name
	    		<<"\t\t"<<queue->data[queue->front].priority
				<<"\t\t"<<queue->data[queue->front].time
				<<"\t\t"<<"执行中..."<<endl;
			for(int i=1; i<queue->rear-queue->front; i++)
				cout<<" "<<queue->data[queue->front+i].name
		    	<<"\t\t"<<queue->data[queue->front+i].priority
				<<"\t\t"<<queue->data[queue->front+i].time
				<<"\t\t"<<"等待中..."<<endl;
			cout<<endl<<"->“进程 "<<queue->data[queue->front].name
	    		<<"”执行完毕!"<<endl<<endl;
			DeleQueue(queue);
		}
		SortPCB(queue->data,queue->rear-queue->front); //对队列中的优先数进程重新排序
	}while(!IsQueueEmpty(*queue));
}

int main()
{
	PCB process[MAXSIZE-1] ; //进程数组
	Queue_Process queue; //进程队列
	
	int num; //要输入的进程数
	cout<<"请输入进程同步数 num: "<<endl; 
	cin>>num;
	
	InitQueue(&queue); //初始化队列
	Input(process,num);
	for(int i=0; i< num; i++) //通过循环使每个进程入队
	{
		EnQueue(&queue,process[i]);
	}
	SortPCB(queue.data,queue.rear-queue.front); //对进程按优先数从大到小排列
	
	cout<<endl<<"---------------------------------------------------------"<<endl;
	cout<<"\t\t进程执行情况";
	cout<<endl<<"---------------------------------------------------------"<<endl;
	Display(&queue); //进程运行函数
}