多级反馈队列FCFS

多级反馈队列FCFS(周转时间的计算方法可能是错误的)在这里插入图片描述

代码1

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

#include<Windows.h>

//作业结构体
typedef struct PCB
{
	//int Numbers;					//进程个数
	char name;						//进程名
	//int ID;							//进程号
	int ComeTime;					//到达时间
	int ServerTime;					//服务时间
	int FinishTime;					//完成时间
	int NeedTime;					// 需要时间
	int TurnoverTime;				//周转时间
	double WeightedTurnoverTime;	//带权周转时间
	char state;		/*进程的状态,W——就绪态,R——执行态,F——完成态*/
	int round; // 分配给进程的时间片
	int count; // 计数器
	struct PCB* next;
}PCB;

typedef struct Queue/*多级就绪队列节点信息*/
{
	PCB* LinkPCB;/*就绪队列中的进程队列指针*/
	int prio;/*本就绪队列的优先级*/
	int round; /*本就绪队列所分配的时间片*/
	struct Queue* next;/*指向下一个就绪队列的链表指针*/
}ReadyQueue;

int cometime;
int ProcessNum = 1; // 进程个数
int  timeslice =2;		//当前时间片
PCB* run = NULL, * finish = NULL;/*定义三个队列,就绪队列,执行队列和完成队列*/
int TimNum=0;				//时间统计
int ReadyNum = 3;		//队列个数
char name = 'A';		//进程名字

ReadyQueue* Head = NULL; // 定义多级就绪队列的头指针


//void Output();		/*进程信息输出函数*/
void endOutput();
void InsertFinish(PCB* in);		/*将进程插入到完成队列尾部*/
void PrioCreate();				/*创建就绪队列函数*/
void GetFirst(ReadyQueue* queue); //取得某一个就绪队列中的队头进程
void InsertLast(PCB* in, ReadyQueue* queue);//将进程插入到就绪队列末尾
void InsertFinal(PCB* pcb);/*将进程插入到完成队列尾部*/
void ProcessCreate();//进程创建函数
void RoundRun(ReadyQueue* timechip);//时间片轮转调度算法
void MultiDispatch();//多级调度算法,每次执行一个时间片
void SortQueue(ReadyQueue* que);  // 队列排序
void InsertPrio(ReadyQueue* in);
void InsertReady(PCB* pcb, ReadyQueue* queue);
void sortPCB(PCB* T);//进程排序
int main(void)
{
	PrioCreate();//创建就绪队列
	ProcessCreate();//创建就绪进程队列
	MultiDispatch();//算法开始
	//Output();//输出最终的调度序列
	endOutput();
	return 0;
}



void MultiDispatch()//多级调度算法,每次执行一个时间片
{
	//char choice;
	ReadyQueue* q;

 	q = Head;
	printf("正在进行排序\n");
	sortPCB(Head->LinkPCB);
	printf("排序完成\n");
	//char* tempName;
	int flag=1;
	printf("取队列头元素\n");
	GetFirst(q);  // 取就绪队列的第一个进程,开始算法
	//int begintime = run->ComeTime;
	while (run != NULL) // 循环执行,执行队列中的进程
	{
		if (Head->LinkPCB != NULL)
			q = Head;
		while (flag)
		{
			run->count++;// 进程的执行次数加1
			run->ServerTime++;// 进程的服务时间加1
			run->NeedTime--;// 进程的需要时间减1
			TimNum++;
			if (run->NeedTime == 0)// 进程执行完毕
			{
				run->FinishTime = TimNum ;		//完成时间
				run->TurnoverTime = run->FinishTime - run->ComeTime;//周转时间
				run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;//带权周转时间
				run->state = 'F';
				InsertFinish(run);// 把该进程插入到完成队列中
				flag = 0;
			}
			else if (run->count == run->round)
			{
				run->state = 'w';
				run->count = 0;
				if (q->next != NULL)
				{
					run->round = q->next->round;
					InsertLast(run, q->next);
					flag = 0;
				}
				else
				{
					RoundRun(q);
				}
			}
			
		}
		flag = 1;

		if (q->LinkPCB == NULL)/*就绪队列指针下移*/
			q = q->next;
		if (q->next == NULL)
		{
			RoundRun(q);
			break;
		}
		GetFirst(q);

	}
}





void GetFirst(ReadyQueue* queue)
{
	if (queue->LinkPCB != NULL) // 队列中的进程不为空
	{
		run = queue->LinkPCB;
		run->state = 'R';
		queue->LinkPCB = queue->LinkPCB->next; // 队列指向进程的指针后移
		run->next = NULL;
	}
	else
	{
		run = NULL;
	}
}



void PrioCreate()/*创建就绪队列输入函数*/
{
	ReadyQueue* tmp;
	int i;
	printf("输入就绪队列的个数,最多输入3个:\n");
	scanf("%d", &ReadyNum);

	//printf("输入每个就绪队列的CPU时间片;\n");
	//创建3个就绪队列
	for (i = 0; i < ReadyNum; i++)
	{
		int times = timeslice * pow(2, i);
		if ((tmp = (ReadyQueue*)malloc(sizeof(ReadyQueue))) == NULL)
		{
			perror("malloc");
			exit(1);
		}

		//scanf("%d", &(tmp->round)); //输入此就绪队列中给每个进程所分配的CPU时间片* /
		tmp->round = times;
		tmp->prio = times;/*设置其优先级,时间片越高,其优先级越低*/
		tmp->LinkPCB = NULL; /*初始化其连接的进程队列为空*/

		tmp->next = NULL;
		InsertPrio(tmp);
	}
}


void ProcessCreate()/*进程创建函数*/
{
	
	time_t ts;//设置时间变量
	PCB* tmp;
	int i;
	int Pronum;
	printf("输入进程的个数:\n");
	scanf("%d", &Pronum);
	getchar();
	cometime = 0;	//将第一个进程到达的时间设为0
	//printf("输入进程名字和进程所需要时间\n");
	printf("等待%d秒钟,正在生成随机数", Pronum);
	for (i = 0; i < Pronum; i++)
	{
		Sleep(1000);
		//随机函数
		srand((unsigned)time(NULL));
		
		int num1 = rand() % 5+1 ;	//到达时间
		int num2 = rand() %9 + 1;		//服务时间

		

		if ((tmp = (PCB*)malloc(sizeof(PCB))) == NULL)
		{
			perror("malloc");
			exit(1);
		}
		
		tmp->name = name;
		name = name + 1;		//字符+1,自动更改进程名字
		tmp->ComeTime= cometime;
		cometime = num1;	//为下一个进程设定到达时间
	
		tmp->ServerTime = 0;
		tmp->NeedTime= num2;
		tmp->state = 'W';
		//tmp->. = 50 - tmp->needtime; /*设置其优先级,需要的时间越多,优先级越低*/

		tmp->round = Head->round;
		tmp->count = 0;
		InsertLast(tmp, Head);/*按照先来先服务*/
	}
}

void InsertPrio(ReadyQueue* in)
{
	ReadyQueue* fst, * nxt;
	fst = nxt = Head;
	if (Head == NULL)
	{
		in->next = Head;
		Head = in;
	}
	else
	{
		if (in->prio <= fst->prio)
		{
			in->next = Head;
			Head = in;
		}
		else
		{
			while (fst->next != NULL)
			{
				nxt = fst;
				fst = fst->next;
			}

			if (fst->next == NULL)
			{
				in->next = fst->next;
				fst->next = in;
			}
			else
			{
				nxt = in;
				in->next = fst;
			}
		}
	}
}

void InsertReady(PCB* pcb, ReadyQueue* queue)
{
	PCB* p;
	pcb->state = 'W';
	pcb->round = queue->round;
	p = queue->LinkPCB;

	if (queue->LinkPCB == NULL) // 队列中进程为空,直接插入到队列中
	{
		pcb->next = queue->LinkPCB;
		queue->LinkPCB = pcb;
	}
	else
	{
		while (p->next != NULL) // 搜索到队列中进程的尾部,将其插入到尾部
		{
			p = p->next;
		}
		pcb->next = p->next;
		p->next = pcb;
	}
}


void InsertFinal(PCB* pcb) // 将进程插入到完成队列尾部
{
	PCB* p;
	pcb->state = 'F';
	p = finish;
	if (finish == NULL)  // 完成队列为空,直接插入
	{
		pcb->next = NULL;
		finish = pcb;
	}
	else
	{
		while (p->next != NULL)
		{
			p = p->next;
		}
		pcb->next = p->next;
		p->next = pcb;
	}
}

void InsertLast(PCB* in, ReadyQueue* queue) /*将进程插入到就绪队列尾部*/
{
	PCB* fst;
	fst = queue->LinkPCB;

	if (queue->LinkPCB == NULL)
	{
		in->next = queue->LinkPCB;
		queue->LinkPCB = in;
	}
	else
	{
		while (fst->next != NULL)
		{
			fst = fst->next;
		}

		in->next = fst->next;
		fst->next = in;
	}
}


void InsertFinish(PCB* in)
{
	PCB* fst;
	fst = finish;
	if (finish == NULL)
	{
		in->next = finish;
		finish = in;
	}
	else
	{
		while (fst->next != NULL)
		{
			fst = fst->next;
		}

		in->next = fst->next;
		fst->next = in;
	}
}


void RoundRun(ReadyQueue* timechip)
{
	int flag = 1;
	GetFirst(timechip);
	while (run != NULL)
	{
		while (flag)
		{
			run->count++;
			//run->cputime++;
			run->ServerTime++;

			run->NeedTime--;
			
			TimNum++;
			if (run->NeedTime == 0)
			{
				run->FinishTime = TimNum;		//完成时间
				run->TurnoverTime = run->FinishTime - run->ComeTime;//周转时间
				run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;//带权周转时间
				run->state = 'F';
				InsertFinish(run);
				flag = 0;
			}
			else if (run->count == timechip->round)
			{
				run->state = 'W';
				run->count = 0;
				InsertLast(run, timechip);

				flag = 0;

				break;
			}
		}

		flag = 1;
		if (timechip->LinkPCB != NULL) // 就绪队列中的进程不为空
		{
			GetFirst(timechip);
			//Output();
		}
		else
		{

			//printf("查看当前全部进程的状态:\n");
			//printf("多级反馈队列调度算法模拟结束,所有进程执行完毕\n");
			endOutput();
			// printf("多级反馈队列调度算法模拟结束,所有进程执行完毕!\n");
			break;
		}
		

	}
}



void endOutput()
{
	PCB* p;
	printf("进程名\t到达时间\t已服务时间\t完成时间\t周转时间\t带权周转时间\n");

	p = finish;

	while (p != NULL)
	{
		/*p->TurnoverTime = p->FinishTime - p->ComeTime;
		p->WeightedTurnoverTime = p->TurnoverTime / p->ServerTime;*/

		printf("%c\t %d\t\t%d\t\t%d\t\t %d\t\t%.2f\n", p->name, p->ComeTime, p->ServerTime, p->FinishTime, p->TurnoverTime, p->WeightedTurnoverTime);


			p = p->next;
	}

}

void SortQueue(ReadyQueue* queue)
{

	ReadyQueue* m, * n;
	m = n = Head;
	if (Head == NULL)
	{
		queue->next = NULL;
		Head = queue;
	}
	else
	{
		if (queue->prio <= m->prio)
		{
			queue->next = m;
			Head = queue;
		}
		else
		{
			while (m->next != NULL)
			{
				n = m;
				m = m->next;
			}

			if (m->next == NULL)
			{
				queue->next = m->next;
				m->next = queue;
			}
			else
			{
				n->next = queue;
				queue->next = m;
			}
		}
	}
}

void sortPCB(PCB* T)
{

	PCB* X = (PCB*)malloc(sizeof(PCB));//用来保存排序后的链表
	PCB* p = (PCB*)malloc(sizeof(PCB));//用来保存当此最小值的前一位
	PCB* Current = T->next;
	PCB* PreCurrent = T;
	PCB* TailX = X;
	*X = *T;
	while (T->next != NULL) {
		int tem = 999998;

		Current = T->next;
		PreCurrent = T;
		while (Current != NULL) {
			if (Current->ComeTime<= tem) {
				tem = Current->ComeTime;

				p = PreCurrent;
				//cout << "处理" << p->name << p->need << "\n";
			}
			Current = Current->next;
			PreCurrent = PreCurrent->next;
		}


		TailX->next = p->next;
		TailX = TailX->next;

		if (p->next->next != NULL)
			p->next = p->next->next;
		else
			p->next = NULL;
	}
	*T = *X;
}

代码2

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>



//作业结构体
typedef struct PCB
{
	int Numbers;					//进程个数
	char name;						//进程名
	//int ID;							//进程号
	int ComeTime;					//到达时间
	int ServerTime;					//服务时间
	int FinishTime;					//完成时间
	int NeedTime;					// 需要时间
	int TurnoverTime;				//周转时间
	double WeightedTurnoverTime;	//带权周转时间
	char state;		/*进程的状态,W——就绪态,R——执行态,F——完成态*/
	int round; // 分配给进程的时间片
	int count; // 计数器
	struct PCB* next;
}PCB;

typedef struct Queue/*多级就绪队列节点信息*/
{
	PCB* LinkPCB;/*就绪队列中的进程队列指针*/
	int prio;/*本就绪队列的优先级*/
	int round; /*本就绪队列所分配的时间片*/
	struct Queue* next;/*指向下一个就绪队列的链表指针*/
}ReadyQueue;

int cometime;
int ProcessNum = 1; // 进程个数
int  timeslice =1;		//当前时间片
PCB* run = NULL, * finish = NULL;/*定义三个队列,就绪队列,执行队列和完成队列*/
int TimNum=0;				//时间统计
int ReadyNum = 3;		//队列个数
char name = 'A';		//进程名字

ReadyQueue* Head = NULL; // 定义多级就绪队列的头指针


void Output();		/*进程信息输出函数*/
void endOutput();
void InsertFinish(PCB* in);		/*将进程插入到完成队列尾部*/
void PrioCreate();				/*创建就绪队列输入函数*/
void GetFirst(ReadyQueue* queue); //取得某一个就绪队列中的队头进程
void InsertLast(PCB* in, ReadyQueue* queue);//将进程插入到就绪队列末尾
void InsertFinal(PCB* pcb);/*将进程插入到完成队列尾部*/
void ProcessCreate();//进程创建函数
void RoundRun(ReadyQueue* timechip);//时间片轮转调度算法
void MultiDispatch();//多级调度算法,每次执行一个时间片
void SortQueue(ReadyQueue* que);  // 队列排序
void InsertPrio(ReadyQueue* in);
void InsertReady(PCB* pcb, ReadyQueue* queue);

int main(void)
{
	PrioCreate();//创建就绪队列
	ProcessCreate();//创建就绪进程队列
	MultiDispatch();//算法开始
	//Output();//输出最终的调度序列
	endOutput();
	return 0;
}



void MultiDispatch()//多级调度算法,每次执行一个时间片
{
	//char choice;
	ReadyQueue* q;
 	q = Head;
	//char* tempName;
	int flag=1;

	GetFirst(q);  // 取就绪队列的第一个进程,开始算法
	//int begintime = run->ComeTime;
	while (run != NULL) // 循环执行,执行队列中的进程
	{
		if (Head->LinkPCB != NULL)
			q = Head;
		while (flag)
		{
			run->count++;// 进程的执行次数加1
			run->ServerTime++;// 进程的服务时间加1
			run->NeedTime--;// 进程的需要时间减1
			TimNum++;
			if (run->NeedTime == 0)// 进程执行完毕
			{
				run->FinishTime = TimNum ;		//完成时间
				run->TurnoverTime = run->FinishTime - run->ComeTime;//周转时间
				run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;//带权周转时间
				run->state = 'F';
				InsertFinish(run);// 把该进程插入到完成队列中
				flag = 0;
			}
			else if (run->count == run->round)
			{
				run->state = 'w';
				run->count = 0;
				if (q->next != NULL)
				{
					run->round = q->next->round;
					InsertLast(run, q->next);
					flag = 0;
				}
				else
				{
					RoundRun(q);
				}
			}
			
		}
		flag = 1;

		if (q->LinkPCB == NULL)/*就绪队列指针下移*/
			q = q->next;
		if (q->next == NULL)
		{
			RoundRun(q);
			break;
		}
		GetFirst(q);

	}
}
		//while (1) 
		//{
		//	//cout << "当前时间片:" << ++timeslice << endl;
		//	//outFile << "当前时间" << timeSlice << ":" << "第" << q->level << "队列的进程" << run->name << "正在执行" << endl;
		//	run->ServerTime++;  // 进程的服务时间加1
		//	run->ServerTime--;  // 进程的需要时间减1
		//	run->count++; // 进程的执行次数加1
		//	if (run->ServerTime == 0) // 进程执行完毕
		//	{
		//		InsertFinal(run); // 把该进程插入到完成队列中
		//		tempName = run->name;
		//		run->FinishTime =timeslice;
		//		run = NULL;  // 执行队列设为空
		//		Output();
		//		//cout << tempName << "进程已完成,插入到完成队列中" << endl << endl;
		//		//outFile << "当前时间" << timeSlice << ":" << "进程" << tempName << "执行完毕,插入完成队列中" << endl;
		//		break; // 跳出循环
		//
		//	}
		//	else if(run)
		//	{ 
		//		//Output();
		//		//if (run->count == run->round) // 时间片用完
		//		//{
		//		//	run->count = 0; // 进程的执行次数归零
		//		//	if (q->next != NULL) // 还有下一个队列
		//		//	{
		//		//		InsertReady(run, q->next); // 将进程插入到下一个就绪队列中
		//		//		//cout << "进程:" << run->name << "在第" << q->level << "队列内未完成,插入第" << q->level + 1 << "队列中" << endl;
		//		//	//	outFile << "当前时间" << timeSlice << ":" << "进程" << run->name << "在第" << q->level << "队列内未完成,插入第" << q->next->level << "队列" << endl;
		//		//		break;
		//		/*	}*/
		//		/*}*/
		//	}
		//}

		 插入新进程
		cout << "是否有新进程到达,请选择(Y/N):" << endl;
		cin >> choice;
		//if (choice == 'y' || choice == 'Y') // 有新进程到达
		//{
		//	CreateProcess();  // 创建新进程
		//	q = Head;  // 指向就绪队列
		//	GetFirst(q);  // 取首进程
		//}
		//else
		//{
		//	cout << "没有新进程到达" << endl << endl;
		//	if (q->LinkPCB == NULL) // 队列中的进程为空,指向队列的指针后移
		//	{
		//		q = q->next;
		//	}
		//	if (q->next == NULL) // 后继队列为空,说明当前是最后一个队列
		//	{
		//		cout << "执行到最后一个队列,使用时间片轮转算法" << endl;
		//		outFile << "当前时间" << timeSlice << ":" << "执行到最后一个队列,使用时间片轮转算法" << endl;
		//		RR(q);
		//		break;
		//	}
			//GetFirst(q);
		//}
	//}






void GetFirst(ReadyQueue* queue)
{
	if (queue->LinkPCB != NULL) // 队列中的进程不为空
	{
		run = queue->LinkPCB;
		run->state = 'R';
		queue->LinkPCB = queue->LinkPCB->next; // 队列指向进程的指针后移
		run->next = NULL;
	}
	else
	{
		run = NULL;
	}
}



void PrioCreate()/*创建就绪队列输入函数*/
{
	ReadyQueue* tmp;
	int i;
	/*printf("输入就绪队列的个数:\n");
	scanf("%d", &ReadyNum);*/

	//printf("输入每个就绪队列的CPU时间片;\n");
	//创建3个就绪队列
	for (i = 0; i < ReadyNum; i++)
	{
		int times = timeslice * pow(2, i);
		if ((tmp = (ReadyQueue*)malloc(sizeof(ReadyQueue))) == NULL)
		{
			perror("malloc");
			exit(1);
		}

		//scanf("%d", &(tmp->round)); //输入此就绪队列中给每个进程所分配的CPU时间片* /
		tmp->round = times;
		tmp->prio = times;/*设置其优先级,时间片越高,其优先级越低*/
		tmp->LinkPCB = NULL; /*初始化其连接的进程队列为空*/

		tmp->next = NULL;
		InsertPrio(tmp);
	}
}


void ProcessCreate()/*进程创建函数*/
{
	//随机函数
	srand(time(NULL));

	PCB* tmp;
	int i;
	int num;
	printf("输入进程的个数:\n");
	scanf("%d", &num);
	getchar();
	cometime = 0;	//将第一个进程到达的时间设为0
	//printf("输入进程名字和进程所需要时间\n");
	for (i = 0; i < num; i++)
	{
		//随机函数
		//srand(time(NULL));
		//
		//int num1 = rand() % 3+1 ;	//到达时间
		//int num2 = rand() % 5 + 1;		//服务时间

		/*scanf("%s", tmp->name);
		getchar();*/

		//strcpy(tmp->name ,(char *) ('A' + i));

		if ((tmp = (PCB*)malloc(sizeof(PCB))) == NULL)
		{
			perror("malloc");
			exit(1);
		}
		printf("输入进程到达时间和进程所需要时间\n");

		scanf("%d%d", &tmp->ComeTime,&tmp->NeedTime);
		tmp->name = name;
		name = name + 1;		//字符+1,自动更改进程名字
		//tmp->ComeTime= cometime;
		//cometime = num1;	//为下一个进程设定到达时间
		//cometime = num1;
		tmp->ServerTime = 0;
		//tmp->NeedTime= num2;
		tmp->state = 'W';
		//tmp->. = 50 - tmp->needtime; /*设置其优先级,需要的时间越多,优先级越低*/

		tmp->round = Head->round;
		tmp->count = 0;
		InsertLast(tmp, Head);/*按照先来先服务*/
	}
}

void InsertPrio(ReadyQueue* in)
{
	ReadyQueue* fst, * nxt;
	fst = nxt = Head;
	if (Head == NULL)
	{
		in->next = Head;
		Head = in;
	}
	else
	{
		if (in->prio <= fst->prio)
		{
			in->next = Head;
			Head = in;
		}
		else
		{
			while (fst->next != NULL)
			{
				nxt = fst;
				fst = fst->next;
			}

			if (fst->next == NULL)
			{
				in->next = fst->next;
				fst->next = in;
			}
			else
			{
				nxt = in;
				in->next = fst;
			}
		}
	}
}

void InsertReady(PCB* pcb, ReadyQueue* queue)
{
	PCB* p;
	pcb->state = 'W';
	pcb->round = queue->round;
	p = queue->LinkPCB;

	if (queue->LinkPCB == NULL) // 队列中进程为空,直接插入到队列中
	{
		pcb->next = queue->LinkPCB;
		queue->LinkPCB = pcb;
	}
	else
	{
		while (p->next != NULL) // 搜索到队列中进程的尾部,将其插入到尾部
		{
			p = p->next;
		}
		pcb->next = p->next;
		p->next = pcb;
	}
}


//void InsertPrio(ReadyQueue* in)
//{
//	ReadyQueue* fst, * nxt;
//	fst = nxt = Head;
//	if (Head == NULL)
//	{
//		in->next = Head;
//		Head = in;
//	}
//	else
//	{
//	
//			while (fst->next != NULL)
//			{
//				nxt = fst;
//				fst = fst->next;
//			}
//
//			if (fst->next == NULL)
//			{
//				in->next = fst->next;
//				fst->next = in;
//			}
//	
//		}
//	}
//}


void InsertFinal(PCB* pcb) // 将进程插入到完成队列尾部
{
	PCB* p;
	pcb->state = 'F';
	p = finish;
	if (finish == NULL)  // 完成队列为空,直接插入
	{
		pcb->next = NULL;
		finish = pcb;
	}
	else
	{
		while (p->next != NULL)
		{
			p = p->next;
		}
		pcb->next = p->next;
		p->next = pcb;
	}
}

void InsertLast(PCB* in, ReadyQueue* queue) /*将进程插入到就绪队列尾部*/
{
	PCB* fst;
	fst = queue->LinkPCB;

	if (queue->LinkPCB == NULL)
	{
		in->next = queue->LinkPCB;
		queue->LinkPCB = in;
	}
	else
	{
		while (fst->next != NULL)
		{
			fst = fst->next;
		}

		in->next = fst->next;
		fst->next = in;
	}
}


void InsertFinish(PCB* in)
{
	PCB* fst;
	fst = finish;
	if (finish == NULL)
	{
		in->next = finish;
		finish = in;
	}
	else
	{
		while (fst->next != NULL)
		{
			fst = fst->next;
		}

		in->next = fst->next;
		fst->next = in;
	}
}


void RoundRun(ReadyQueue* timechip)
{
	int flag = 1;
	GetFirst(timechip);
	while (run != NULL)
	{
		while (flag)
		{
			run->count++;
			//run->cputime++;
			run->ServerTime++;

			run->NeedTime--;
			
			TimNum++;
			if (run->NeedTime == 0)
			{
				run->FinishTime = TimNum;		//完成时间
				run->TurnoverTime = run->FinishTime - run->ComeTime;//周转时间
				run->WeightedTurnoverTime = run->TurnoverTime / run->ServerTime;//带权周转时间
				run->state = 'F';
				InsertFinish(run);
				flag = 0;
			}
			else if (run->count == timechip->round)
			{
				run->state = 'W';
				run->count = 0;
				InsertLast(run, timechip);

				flag = 0;

				break;
			}
		}

		flag = 1;
		if (timechip->LinkPCB != NULL) // 就绪队列中的进程不为空
		{
			GetFirst(timechip);
			//Output();
		}
		else
		{

			//printf("查看当前全部进程的状态:\n");
			//printf("多级反馈队列调度算法模拟结束,所有进程执行完毕\n");
			endOutput();
			// printf("多级反馈队列调度算法模拟结束,所有进程执行完毕!\n");
			break;
		}
		

	}
}


void Output()
{
	ReadyQueue* q = Head;
	PCB* p;
	printf("进程名\t  时间片\t 已服务时间\t所需时间\t状态\t\n");

	p = run;
	while (p != NULL)
	{
		printf( " %c\t %d\t %d \t%d\t%c\n",p->name , p->round ,p->ServerTime, p->NeedTime,p->state );
		p = p->next;
	}

	while (q)
	{
		if (q->LinkPCB != NULL)
		{
			p = q->LinkPCB;
			while (p)
			{
				printf(" %c\t %d\t %d \t%d\t%c\n", p->name, p->round, p->ServerTime, p->NeedTime, p->state);
				p = p->next;
			}
		}
		q = q->next;
	}

	p = finish;
	while (p != NULL)
	{
		printf(" %c\t\t %d\t\t %d \t\t%d\t\t%c\n", p->name, p->round, p->ServerTime, p->NeedTime, p->state);
		p = p->next;
	}
	
}

void endOutput()
{
	PCB* p;
	printf("进程名\t到达时间\t已服务时间\t完成时间\t周转时间\t带权周转时间\n");

	p = finish;

	while (p != NULL)
	{
		/*p->TurnoverTime = p->FinishTime - p->ComeTime;
		p->WeightedTurnoverTime = p->TurnoverTime / p->ServerTime;*/

		printf("%c\t %d\t\t%d\t\t%d\t\t %d\t\t%.2f\n", p->name, p->ComeTime, p->ServerTime, p->FinishTime, p->TurnoverTime, p->WeightedTurnoverTime);


			p = p->next;
	}

}

void SortQueue(ReadyQueue* queue)
{

	ReadyQueue* m, * n;
	m = n = Head;
	if (Head == NULL)
	{
		queue->next = NULL;
		Head = queue;
	}
	else
	{
		if (queue->prio <= m->prio)
		{
			queue->next = m;
			Head = queue;
		}
		else
		{
			while (m->next != NULL)
			{
				n = m;
				m = m->next;
			}

			if (m->next == NULL)
			{
				queue->next = m->next;
				m->next = queue;
			}
			else
			{
				n->next = queue;
				queue->next = m;
			}
		}
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值