操作系統——進程調度之時間片輪轉

 實驗源代碼:

#include <iostream>
#include<ctime>
using namespace std;

//共有n個進程就緒,m個進程阻塞
//經過t個時間片釋放系統資源,喚醒處於阻塞隊列隊首的進程
/*每個時間片大小爲1,進程所需CPU時間爲整數*/

struct PCB_type {
	char name;//進程名
	int statr;//進程狀態  2執行,1就緒,0阻塞
	int cpu_time;//運行需要CPU時間
};
struct QueueNode {
	PCB_type PCB;
	QueueNode* next;
};
const int T = 5;//每過五個時間片,將阻塞隊列中的進程放入就緒隊列中
const int Timeslice = 2;//一個時間片大小
const int N = 5;//N爲所有PCB進程的個數,0<N<26,對PCB進程命名爲26個英文字母
double cpu_whole = 0;//總共用了多少個時間片
double cpu_free = 0;//空閒的時間片
QueueNode* ready_head = new QueueNode();//就緒隊列頭節點
QueueNode* blocked_head = new QueueNode();//阻塞隊列頭節點
QueueNode* node_tail = new QueueNode();//兩個隊列的尾部節點
//向隊列尾部插入節點
void addNode(PCB_type pcb)
{
	QueueNode* Queue=node_tail;
	if (pcb.statr == 1)
	{
		Queue = ready_head;//就緒
	}
	else if (pcb.statr == 0)
	{
		Queue = blocked_head;//阻塞
	}
	//暫時不處理進程處於執行狀態
	else
	{
		cout << "進程正在執行" << endl;
	}
	while (true)
	{
		if (Queue->next == node_tail)
		{
			break;
		}
		Queue = Queue->next;
	}
	QueueNode* Q = new QueueNode();
	Q->PCB = pcb;
	Q->next = Queue->next;
	Queue->next = Q;
}
//取出節點
//n=1取出就緒隊列中節點執行後放入就緒隊列末尾或者直接釋放,
//n=2取出阻塞隊列中節點放入就緒隊列末尾
void delNode(int n)
{
	//PCB_type PCB;
	if (n == 1)
	{
		//取出就緒隊列中節點,判斷進程是否執行完,
		//未執行完則放入就緒隊列末尾,否則刪除
		//判斷就緒隊列是否爲空,爲空時不做操作
		if (ready_head->next != node_tail)
		{
			//減去一個時間片大小
			if ((ready_head->next->PCB.cpu_time-Timeslice) > 0)
			{
				//在時間片執行完後剩餘運行所需CPU時間>0,將進程放入就緒隊列末尾
				ready_head->next->PCB.cpu_time -= Timeslice;
				addNode(ready_head->next->PCB);
				ready_head->next = ready_head->next->next;
			}
			else
			{
				//在時間片執行完後進程結束,銷燬進程
				ready_head->next = ready_head->next->next;
			}
		}
		else
		{
			cpu_free++;
		}
	}
	else if(n==2)
	{
		//將阻塞隊列頭部的進程放入就緒隊列末尾
		//阻塞隊列頭部後移一個
		//判斷阻塞隊列是否爲空,若爲空則不操作
		if (blocked_head->next != node_tail)
		{
			blocked_head->next->PCB.statr = 1;
			addNode(blocked_head->next->PCB);
			blocked_head->next = blocked_head->next->next;
		}
	}
	else
	{
		cout << "參數錯誤" << endl;
	}
}
void out()//輸出兩個隊列中所有的進程
{
	QueueNode* Queue = new QueueNode();
	cout << "就緒隊列" << endl;
	Queue = ready_head;
	while (Queue->next!=node_tail)
	{
		cout << Queue->next->PCB.name <<"剩餘長度"<<Queue->next->PCB.cpu_time<< endl;
		Queue = Queue->next;
	}
	cout << "阻塞隊列" << endl;
	Queue = blocked_head;
	while (Queue->next != node_tail)
	{
		cout << Queue->next->PCB.name << endl;
		Queue = Queue->next;
	}
	cout << endl;
}
void start()
{
	ready_head->next = node_tail;
	blocked_head->next = node_tail;
	//對PCB進行初始化
	//使用PCB數組進行存儲全部進程
	srand(time(0));//隨機數種子
	PCB_type pcb[N];//定義N個PCB進程
	char Name = 'A';
	string temp = "阻塞";
	cout << "全部PCB進程如下" << endl;
	//創建PCB進程
	for (int i = 0;i < N;i++)
	{
		pcb[i].cpu_time = rand() % 10 + 1;
		pcb[i].statr = rand() % 2 + 0;//進程狀態不考慮執行狀態即狀態碼不爲2
		pcb[i].name = Name;
		Name++;
		if (pcb[i].statr == 1)
		{
			temp = "就緒";
		}
		cout << "進程名稱" << pcb[i].name << "進程所需CPU時間" << pcb[i].cpu_time << "進程狀態" << temp << endl;
	}
	for (int i = 0;i < N;i++)
	{
		addNode(pcb[i]);
	}
	out();
}
int main()
{
	int a = 0;
	start();
	while (ready_head->next != node_tail && blocked_head != node_tail)
	{
		for (int i = 0;i < 5;i++)
		{
			delNode(1);
			if (ready_head->next == node_tail && blocked_head == node_tail)
			{
				//就緒隊列和阻塞隊列都空
				break;
			}
			cpu_whole++;
		}
		delNode(2);
		out();
	}
	cout << "CPU總佔用時間" << cpu_whole << "CPU空閒時間" << cpu_free << endl;
	cout << "CPU利用率" << 1 - (cpu_free / cpu_whole) << endl;
}

 

發佈了3 篇原創文章 · 獲贊 0 · 訪問量 667
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章