进程调度模拟

#include<iostream>
#include<stack>
#include<string>
#include<iomanip>
using namespace std;
//enum status{ready,run,finish};
struct PCB {
	string process_tag;
	PCB* next;
	int process_priority;
	int cputime;
	int process_time;
	string process_status;
	PCB()
	{
		this->cputime = 0;
		this->process_priority = (int)(rand() % (10 - 1 + 1)) + 1;//1到10之间的随机数
		this->process_time= (int)(rand() % (10 - 1 + 1)) + 1;//1到10之间的随机数
		this->process_status = "ready";//准备队列
		this->next = NULL;
	}
};
struct PCBList {
	PCB* run;//运行队列的头指针
	PCB* ready;//准备队列的头指针
	PCB* finish;//结束队列的头指针
	PCBList()
	{
      this->run = NULL;
	  this->ready = NULL;
	  this->finish = NULL;
	}
};
/*********将就绪队列中的PCB按优先数的大小从高到低排序***********/
void sort_PCBList (PCBList* p) {

	PCB* q;
	PCB* q1;
	for (q= p->ready;q;q = q->next)
	{
		for (q1 = q->next;q1;q1 = q1->next)
		{
			if (q->process_priority < q1->process_priority)
			{
				swap(q->cputime, q1->cputime);
				swap(q->process_priority, q1->process_priority);
				swap(q->process_tag, q1->process_tag);
				swap(q->process_time, q1->process_time);
			}
		}
	}

}
void dis_PCBList(PCBList* q) {
	PCB* head_ready = q->ready;
	PCB* head_run = q->run;
	PCB* head_finish = q->finish;
	PCB* cur_ready=head_ready;
	PCB* cur_run=head_run;
	PCB* cur_finish=head_finish;
	cout <<setw(9)<< "tag" <<setw(13)<<"cputime" <<setw(13)  << "time" << setw(13)  << "priority" << setw(13)  << "status"  << endl;
	while (cur_run)
	{
		cout << cur_run->process_tag << setw(13) << cur_run->cputime << setw(13)  << cur_run->process_time << setw(13) << cur_run->process_priority << setw(13)  << cur_run->process_status << endl;
         cur_run = cur_run->next;
	}
	while (cur_ready)
	{
		cout << cur_ready->process_tag << setw(13) << cur_ready->cputime << setw(13) << cur_ready->process_time << setw(13) << cur_ready->process_priority << setw(13) << cur_ready->process_status << endl;
		cur_ready = cur_ready->next;
	}
	while (cur_finish)
	{
		cout << cur_finish->process_tag << setw(13) << cur_finish->cputime << setw(13) << cur_finish->process_time << setw(13) << cur_finish->process_priority << setw(13) << cur_finish->process_status << endl;
		cur_finish = cur_finish->next;
	}
}
/************优先数算法**********/
void  PSA(PCBList* p){

	PCBList* q = p;
	sort_PCBList(q);
	PCB* cur_run=q->run;
	PCB* cur_ready=q->ready;
	PCB* cur_finish=q->finish;
	while (q->ready != NULL)
	{
		q->run = q->ready;
		q->ready = q->ready->next;
		q->run->next = NULL;
		q->run->process_status = "run";
		q->run->cputime++;
		dis_PCBList(q);
		q->run->process_priority -= 3;
		q->run->process_time -= 1;
		if (q->run->process_time == 0)
		{
			if (q->finish==NULL)
			{
				q->finish = q->run;
				q->finish->process_status = "finish";
				q->finish->next = NULL;
				cur_finish = q->finish;
			}
			else
			{
				cur_finish->next = q->run;
				cur_finish = cur_finish->next;
				cur_finish->process_status = "finish";
				cur_finish->next = NULL;
			}
		}
		else
		{
			if (q->ready != NULL)
			{
				cur_ready = q->ready;
				while (cur_ready)
				{
					if (cur_ready->process_priority < q->run->process_priority)
					{
						break;
					}
					cur_run = cur_ready;
					cur_ready = cur_ready->next;
				}
				/*********把此时的q->run插入到ready中*****/
			/*	if (cur_ready == q->ready)
				{
					q->run->process_status = "ready";
					q->run->next = q->ready;
					q->ready = q->run;
				}
				else
				{
					q->run->process_status = "ready";
					q->run->next = cur_run->next;
					cur_run->next = q->run;
				}
			}
			else
			{
				q->ready = q->run;
			}
		}

	}
	q->run = NULL;
	dis_PCBList(q);
}
/*********轮盘调度算法*******/
void RR(PCBList* p){
	PCBList* q = p;
	PCB* cur_run = q->run;
	PCB* cur_ready = q->ready;
	PCB* cur_finish = q->finish;
	while (q->ready != NULL)
	{
		q->run = q->ready;
		q->ready = q->ready->next;
		q->run->next = NULL;
		q->run->process_status = "run";
		q->run->cputime++;
		dis_PCBList(q);
		q->run->process_time -= 1;
		if (q->run->process_time == 0)
		{
			if (q->finish == NULL)
			{
				q->finish = q->run;
				q->finish->process_status ="finish";
				q->finish->next = NULL;
				cur_finish = q->finish;
			}
			else
			{
				cur_finish->next = q->run;
				cur_finish = cur_finish->next;
				cur_finish->process_status = "finish";
				cur_finish->next = NULL;
			}
		}
		else//把q->run插入ready队列的尾部
		{
			if (q->ready != NULL)
			{
				cur_ready = q->ready;
				while (cur_ready->next)
				{
					cur_ready = cur_ready ->next;
				}

				cur_ready->next = q->run;
				q->run->process_status = "ready";
				q->run->next= NULL;

			}
			else
			{
				q->ready = q->run;
				q->run->process_status = "ready";
				q->run->next = NULL;
			}
		}
	}
	q->run = NULL;
	dis_PCBList(q);
}



int main()
{
	PCBList* list = new PCBList();
	PCB* head = new PCB();
	head->process_tag = "process_" + to_string(0);
	list->ready = head;
	//cout << head->process_tag << " " << head->cputime << " " << head->process_time << " " << head->process_priority << " " << head->process_status;
	for (int i = 1;i <= 9;i++)
	{
		head->next = new PCB();
		(head->next)->process_tag = "process_" +to_string(i);
		head = head->next;
	}
	dis_PCBList(list);
	sort_PCBList(list);
	//dis_PCBList(list);
	PSA(list);
	//cout << "hello! world!" << endl;
	//RR(list);
	//getchar();
	return 0;

}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值