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

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

进程结点的数据元素有:

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

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

#include 
#include
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### <" <

      

 

你可能感兴趣的:(算法,数据结构,操作系统,C++,C++,数据结构,操作系统,算法)