操作系統實驗——動態分區分配方式的模擬

#include<iostream>
#include <vector>
#define MAX 640
using namespace std;
struct work
{
	int id;
	int size;
};
struct memory
{
	int front_number;  //前一個的序列號
	int number;  //序號
	int id;  //佔用程序的id 0爲未佔用
	bool flag;//0爲未被佔用,可被回收
	int size; //大小
	
};
work process1 = { 1,130 };
work process2 = { 2,60 };
work process3 = { 3,100 };
work process4 = { 4,200 };
work process5 = { 5,140 };
work process6 = { 6,60 };
work process7 = { 7,50 };
struct memory M[2] = 
{
	{0,0,0,1,0},
	{0,MAX,0,0,MAX}
};//{0,0,0,1,0}是爲了湊數,存儲的時候從M[1]開始存儲,輸出也從M[1]開始輸出。因爲數組是從0算起的,這樣的話可以讓數組從1算起,與id對應上,方便思考。flag設爲1表示本區域不可被回收
vector<memory> M_queue;
memory temp;
int chose;
void M_merge(int mer_id)  //合併
{
	if ((mer_id < M_queue.size() - 1) && (M_queue[mer_id + 1].flag == 0))//本區爲回收區,下面爲空閒區
	{
		M_queue[mer_id + 1].size += M_queue[mer_id].size;
		M_queue[mer_id + 1].front_number = M_queue[mer_id].front_number;
		M_queue.erase(M_queue.begin() + mer_id);//erase爲根據位置刪除元素
	}
	if (M_queue[mer_id - 1].flag == 0)//本區爲回收區,上面爲空閒區
	{
		M_queue[mer_id - 1].size += M_queue[mer_id].size;
		M_queue[mer_id - 1].number = M_queue[mer_id].number;
		M_queue.erase(M_queue.begin() + mer_id);
	}
}
void M_print()
{
	cout << "-----------------------------------" << endl;
	cout << "前序號\t序號\t程序ID\t內存大小\t標誌" << endl;
	for (int i = 1; i < M_queue.size(); i++)
		cout << M_queue[i].front_number << "\t" << M_queue[i].number << "\t" << M_queue[i].id << "\t" << M_queue[i].size << "\t\t" << M_queue[i].flag << endl;
	cout << "-----------------------------------" << endl;
	cout << endl << endl;
}
void alloc(work p1)
{
	if (chose == 1)   //首次適應算法
	{
		for (int i = 1; i < M_queue.size(); i++)
		{
			if ((M_queue[i].flag == 0) && p1.size < M_queue[i].size)
			{
				temp.flag = 1;
				temp.id = p1.id;
				temp.size = p1.size;
				temp.front_number = M_queue[i].front_number;
				temp.number = temp.front_number + temp.size;
				M_queue[i].front_number = temp.number;
				M_queue[i].size -= temp.size;
				M_queue.insert(M_queue.begin() + i, temp);//insert爲在指定位置插入元素
				break;
			}
		}

	}
	else if (chose == 2)   //最佳適應算法
	{
		int best_num = MAX;//記錄目標空閒區大小
		int best_i;//記錄目標空閒區編號
		for (int i = 1; i < M_queue.size(); i++)//找到只比p1.size大一點的空閒區,即目標空閒區
		{
			if ((M_queue[i].flag == 0) && (M_queue[i].size >= p1.size) && (M_queue[i].size <= best_num))
			{
				best_num = M_queue[i].size;
				best_i = i;
			}
		}
		temp.flag = 1;
		temp.id = p1.id;
		temp.size = p1.size;
		temp.front_number = M_queue[best_i].front_number;
		temp.number = temp.front_number + temp.size;
		M_queue[best_i].front_number = temp.number;
		M_queue[best_i].size -= p1.size;
		M_queue.insert(M_queue.begin() + best_i, temp);
	}
	M_print();
}
void free(work p2)
{
	int id;
	for (int i = 0; i < M_queue.size(); i++)
	{
		if (p2.id == M_queue[i].id)
		{
			M_queue[i].flag = 0;
			M_queue[i].id = 0;
			id = i;
			break;
		}
	}
	M_merge(id);
	M_print();
}
int main()
{
	M_queue.push_back(M[0]);
	M_queue.push_back(M[1]);
	cout << "初始空閒區"<<endl;
	M_print();
	cout << "1、首次適應算法\n2、最佳適應算法\n請輸入:";
	cin >> chose;
	if (chose != 1 && chose != 2)
	{
		cout << "error" << endl;
		return 0;
	}
	cout << "alloc(process[1])" << endl;
	alloc(process1);
	cout << "alloc(process[2])" << endl;
	alloc(process2);
	cout << "alloc(process[3])" << endl;
	alloc(process3);
	cout << "free(process[2])" << endl;
	free(process2);
	cout << "alloc(process[4])" << endl;
	alloc(process4);
	cout << "free(process[3])" << endl;
	free(process3);
	cout << "free(process[1])" << endl;
	free(process1);
	cout << "alloc(process[5])" << endl;
	alloc(process5);
	cout << "alloc(process[6])" << endl;
	alloc(process6);
	cout << "alloc(process[7])" << endl;
	alloc(process7);
	cout << "free(process[6])" << endl;
	free(process6);
	return 0;
}

首次適應算法

在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

最佳適應算法

在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章