优先级调度法处理机调度算法实现(C++)

假设系统有n个进程,每个进程用一个进程控制块(PCB)来代表。

进程结点的数据元素有:

  • 进程号
  • 进程到达时间进程状态
  • 优先数
  • 进程运行时间
  • 进程运行时间

C++实现模拟时间片轮转法进程调度源代码:

#include <iostream>
#include<string.h>
using namespace std;
#define num 50
typedef struct node {	//创建进程结点
	int runtime;	//进程运行时间
	int name;		//进程号
	int arrivaltime;	//进程到达时间
	char state;		//进程状态 N未到达 R就绪 F结束
	int pri;		//进程优先级
	struct node *next;
}node, *L;
int cal[num][5];
void createL(L &l1,L &l2,int n) {		//初始化进程链表
	
	l1 = (L)malloc(sizeof(node));//就绪队列
	l2 = (L)malloc(sizeof(node));//未到达队列
	if (!l1||!l2)
		cout<<"error!";
	else {
		l1->next = NULL;
		l2->next = NULL;
	}
	L p1, q1 , p2 , q2;
	q1 = l1;
	q2 = l2;
	cout << "请分别输入进程的进程号、到达时间、运行时间、优先级:" << endl;
	for (int i = 0; i < n; i++) {
		int name, arrivaltime, runtime, pri;
		p1 = (L)malloc(sizeof(node));
		p2 = (L)malloc(sizeof(node));
		cin >> name >> arrivaltime >> runtime >> pri;
		cal[i][0] = name;
		cal[i][1] = arrivaltime;
		cal[i][2] = runtime;
		cal[i][3] = pri;
		if (arrivaltime == 0) {
			p2->name = name;
			p2->arrivaltime = arrivaltime;
			p2->runtime = runtime;
			p2->pri = pri;
			p2->state = 'R';

			p2->next = q2->next;
			q2->next = p2;
			q2 = p2;
		}
		else {
			p1->name = name;
			p1->arrivaltime = arrivaltime;
			p1->runtime = runtime;
			p1->pri = pri;
			p1->state = 'N';
			p1->next = q1->next;
			q1->next = p1;
			q1 = p1;
		}
	}
}
void traL(L &l) {	//显示进程队列
	if (!l->next)
		return;
	L p;
	p = l->next;
	cout << "进程号\t" << "到达时间\t" << "运行时间\t" << "优先数\t" << "状态" << endl;
	while (p) {
		cout << p->name << "\t" << p->arrivaltime << "\t\t" << p->runtime << "\t\t" << p->pri << "\t" << p->state << endl;
		p = p->next;
	}
}
void Sort1(L &l){	//优先级排序冒泡排序
	L tail = NULL;
	while (l->next != tail)
	{
		L pre = l;
		L cur = pre->next;
		while (cur != tail && cur->next != tail)
		{
			if (cur->pri > cur->next->pri)//大->小
			{
				pre->next = cur->next;
				cur->next = cur->next->next;
				pre->next->next = cur;
			}
			pre = pre->next;
			cur = pre->next;
		}
		tail = cur;
	}
}
void Sort2(L &l) {	//未到达进程 arrivaltime排序 冒泡排序
	L tail = NULL;

	while (l->next != tail)
	{
		L pre = l;
		L cur = pre->next;
		while (cur != tail && cur->next != tail)
		{
			if (cur->arrivaltime > cur->next->arrivaltime)//小-》大
			{
				pre->next = cur->next;
				cur->next = cur->next->next;
				pre->next->next = cur;
			}
			pre = pre->next;
			cur = pre->next;
		}
		tail = cur;
	}
}
void run(L &l1,L &l2) {
	cout << endl << endl;
	cout << "#######################进程运行状态(R:就绪状态,C:进程完成)#######################" << endl;
	cout << endl;
	Sort1(l1);	//优先级排序
	Sort2(l2);	//到达时间排序
	L p, r, q,p2,q2;
	p = NULL;
	q = l1;

	p2 = NULL;//未到达队列
	q2 = l2;
	int time = 0;//时间片
	if (l1->next != NULL)
		p = l1->next;
	if (l2->next != NULL)
		p2 = l2->next;
	
	while(p!=NULL||p2!=NULL){		//存在进程
		cout << "--------------------《" << time+1 << "》时刻进程中的队列情况--------------------" << endl;
		cout <<"进程号\t" << "运行时间\t" << "优先数\t" << "状态" << endl;
		if (p != NULL) {
			time++;
			L lshow = p;
			for (int i = 0; lshow != NULL;i++) {
				if (i == 0) {
					cout << lshow->name << "\t" << lshow->runtime << "\t\t" << lshow->pri << "\t运行中";
				}
				else {
					cout << lshow->name << "\t" << lshow->runtime << "\t\t" << lshow->pri << "\t" << lshow->state;
				}
				cout << endl;
				lshow = lshow->next;
			}
			p->runtime--;
			p->pri++;
			if (p->runtime == 0) {
				p->state = 'C';//运行时间为0,进程结束,释放p结点(进程),状态为C(complete)
				//cout << time << "\t" << p->name << "\t" << p->runtime << "\t\t" << p->pri << "\tR->" << p->state;
				cout << "\t <" << time << "> 时刻【" << p->name << "】进程完成!" << endl;
				cal[p->name-1][4] = time;	//存储进程完成时间
				r = p;
				if (p->next != NULL)
				{
					l1->next = p->next;
					free(r);
					p = l1->next;
				}
				else
					break;
			}
			else {
				Sort1(l1);
				if (l1->next != NULL)
					p = l1->next;
			}
		}
		while (p2 != NULL && time == p2->arrivaltime) {//存在未到达队列不为空,判断到了time时刻是否有进程到达
			cout << "\t\t\t\t### <" <<time << "> 时间片【" << p2->name << "】进程到达:No arrival->Ready ###" << endl;
			p2->state = 'R';
			L temp = p2->next;
			if (p == NULL) {
				p = p2;
				p->next = NULL;
			}
			else {
				p2->next = p->next;
				p->next = p2;
			}
			p2 = temp;
			Sort1(l1);
		}
		if (p == NULL && p2 != NULL) {//判断时间片空转
			if (time < p2->arrivaltime)
				time++;
			cout << "#### <" << time << "> 时间片 空转  ####" << endl << endl;
		}
		cout << endl;
	}
	cout << endl << "====================================END====================================" << endl << endl;
}
void cal_Cycltime(int sum) {
	cout << endl << "##########################################性能测试##########################################" << endl;
	//double crightcyctime;
	double avg_cyctime=0.0;
	double avg_crightcyctime=0.0;
	cout << "进程号\t" << "到达时间\t" << "运行时间\t" << "优先数\t" <<"完成时间\t"<<"周转时间\t"<<"带权周转时间"<< endl;
	for (int i = 0; i < sum; i++) {	
		int cyctime = cal[i][4] - cal[i][1];
		double crightcyctime = (double)cyctime / cal[i][2];
		avg_cyctime += cyctime;
		avg_crightcyctime += crightcyctime;
		cout << cal[i][0] << "\t" << cal[i][1] << "\t\t" << cal[i][2] << "\t\t" << cal[i][3] << "\t" << cal[i][4] << "\t\t" << cyctime << "\t\t" << crightcyctime << endl;
	}
	cout << "平均周转时间:\t" << avg_cyctime / sum << "\t平均带权周转时间:\t" << avg_crightcyctime / sum << endl;
}
int main() {
	L l1,l2;//l1:未到达	l2:就绪
	int n;
	cout << "#######################设置进程#######################" << endl;
	cout << "请输入进程的数目n:" << endl;
	cin >> n;
	createL(l1,l2,n);	//初始化各个进程
	cout << "#######################未到达队列#######################" << endl;
	traL(l1);	//显示未到达进程队列
	cout << endl;
	cout << "#######################就绪队列#######################"<< endl;
	traL(l2);	//显示就绪进程队列
	run(l2,l1);		//运行进程队列
	cal_Cycltime(n);	//测试调度性能
}

      

猜你喜欢

转载自blog.csdn.net/weixin_40849588/article/details/86524369