动态分区分配实验报告

 2016  11  30




西安工业大学实验报告


专业

计算机科学与工程学院

班级

14060xxx

姓名

xxx

学号

14060104xxx

实验课程

操作系统

指导教师

xxx

实验日期

2016.11.23

同实验者

 

实验项目

实验二 动态分区分配

实验设备及器材

PC机一台,编译环境VS2008


一、实验目的




了解动态分区分配方式中使用的数据结构和分配算法,并进一步加深对动态分区存储管理方式及其实现过程的理解


二、实验原理


1)分区的个数和大小不是固定不变的,而是可变的,随装入的作业动态划分,


且不会产生内部碎片。


(2)外部碎片:若存储块长度为N,在该系统所采用的调度算法下较长时间内无法选出一道长度不超过该块的进程,则称该块为外部碎片。


(3)首次适应算法FF:空闲分区链以地址递增的次序链接,进行内存分配时,从链首开始顺序查找。


(4)最佳适应算法:每次为作业分配内存时,总是把满足要求的,最小的空闲分区分配给作业。空闲分区由小到大形成空闲分区链,每次从链首找,找到的第一个,必然是最小的。


三、实验步骤、数据记录及处理


1.算法流程


1)抽象数据类型的定义


 (2)主程序的流程
typedef struct _Node//每个空闲分区的定义
{  int _Numble;  //区号
	int _M_Size;  //空闲区大小
	int _Address; //初始地址
	struct _Node *_Next;//下一个空闲分区}Node;
class lsTable
{	private:
	Node* _Xroot;//首次适应算法的空闲分区链
	Node* _Useroot;//实际占用内存的链
	Node* _Zroot;//最佳适应算



(3)各程序模块之间的层次(调用)关系。


最佳适应算法:




四、总结与体会

      主存的分配和回收的实现与主存储器的管理方式有关,通过做这次实验帮助我理解了可变分区管理方式下应怎样实现贮存空间的分配与回收。由于本次实验是模拟主存的分配,所以主存分配区给作业后并不实际启动装入程序装入作业,而用输出分配情况来代替。

附录:源程序

#include
#include
#include
//#include

using namespace std;
#define MemorySize 512

typedef struct _Node
{
	int _Numble;  //区号
	int _M_Size;  //空闲区大小
	int _Address; //初始地址
	struct _Node *_Next;
}Node;

class lsTable
{	
public:

	Node *_Buynode(int num,int msize,int address)
	{
		Node *s = (Node *)malloc(sizeof(Node));
		s->_Numble  = num;
		s->_M_Size  = msize;
		s->_Address = address;
		return s;
	}
	lsTable()
	{
		_Xroot   = _Buynode(0,0,0) ;
		Node *s = _Buynode(0,MemorySize,0) ;
		_Xroot->_Next = s;
		s->_Next = NULL;

		_Useroot   = _Buynode(0,0,0) ;
		_Useroot->_Next  = NULL;

		_Zroot   = _Buynode(0,0,0) ;
		Node *p = _Buynode(0,MemorySize,0) ;
		_Zroot->_Next = p;
		p->_Next = NULL;
	}

	~lsTable()
	{
		free(_Xroot);
		_Xroot = NULL;

		free(_Useroot);
		_Useroot = NULL;

		free(_Zroot);
		_Zroot = NULL;
	}
	void clear(Node *s)
	{
		Node *p = s;
		Node *q = s->_Next ;
		while(p->_Next != NULL)
		{
			p->_Next  = q->_Next ;
			free(q);
		}
	}
	void Insert_Use(Node *&s)
	{
		_Node *p = _Useroot;
		while(p->_Next != NULL)
		{
			p = p->_Next;
		}
		p->_Next  = s;
		s->_Next  = NULL;
	}

    bool Firstapply()
	{
        Node *node = (Node*)malloc(sizeof(Node));

		cout<<"_Numble  _M_Size"<>node->_Numble>>node->_M_Size;

		bool res = false;

		Node *p = _Xroot;
        Node *s = _Xroot->_Next;
		while(p->_Next != NULL)
		{
			if(s->_M_Size == node->_M_Size)
			{
				p->_Next = NULL;
				node->_Address  = s->_Address ;
				Insert_Use(node);
				res = true;
				break;
			}
			else if(node->_M_Size < s->_M_Size)
			{
				node->_Address  = s->_Address ;

				s->_Numble  = node->_Numble;
				s->_M_Size  = s->_M_Size- node->_M_Size;
				s->_Address = s->_Address+node->_M_Size;

				Insert_Use(node);
				res = true;
				break;
			}
			p = s;
			s = s->_Next;
		}
		return res;
	}

    bool FirstRelease()
	{
		//Node node;
		int numble;
		int msize;
		int address;
		cout<<"_Numble  _Address _M_Size"<>numble>>address>>msize;
		
		bool res = false;
		
		Node *p = _Useroot;
        Node *s = _Useroot->_Next;

		while(p->_Next != NULL)
		{
			if(numble == s->_Numble && msize == s->_M_Size && address == s->_Address)
			{
				p->_Next = s->_Next;
				break;
			}
			else
			{
				p = s;
				s = s->_Next;
			}
		}

		if(s == NULL)return res;

		p = _Xroot;
		Node *q = _Xroot->_Next;
		while(q != NULL )
		{
			if(p != _Xroot && p->_Address +p->_M_Size == s->_Address)
			{
				p->_M_Size = p->_M_Size + s->_M_Size;
				free(s);
				if(p->_Address +p->_M_Size == q->_Address)
				{
					p->_M_Size = p->_M_Size + q->_M_Size;
					p->_Next = q->_Next;
					free(q);
					q = p->_Next;
				}
				res = true;
				break;
			}
			else if(s->_Address +s->_M_Size == q->_Address)
			{
				q->_M_Size = q->_M_Size + s->_M_Size;
				q->_Address  = s->_Address ;
				free(s);
				res = true;
				break;
			}
			else if(p != _Xroot && p->_Address +p->_M_Size < s->_Address && s->_Address +s->_M_Size < q->_Address || p==_Xroot &&  s->_Address +s->_M_Size < q->_Address)
			{
				s->_Next = q;
				p->_Next = s;
				res = true;
				break;
			}
			else
			{
				p = q;
				q = q->_Next;
			}
		}
		return res;
	}
	
	void Print(Node *p)
	{
		cout<<"*************************************************"<_Next;
		if(s != NULL)
		{
			cout<<"_Numble    _Address     _M_Size"<_Next != NULL)
		{
			cout<_Numble<<"          "<_Address<<"          "<_M_Size<_Next ;
		}

		cout<<"已经分区表"<_Next;
		if(s != NULL)
		{
			cout<<"_Numble    _Address     _M_Size"<_Next != NULL)
		{
			cout<_Numble<<"          "<_Address<<"           "<_M_Size<_Next ;
		}
		cout<<"*************************************************"<>n;
			switch(n)
			{
			case 1:
				if(!Firstapply())
				{
				    cout<<"没有更大的分区"<_Next;
		while(q != NULL && s->_M_Size >q->_M_Size)
		{
			p =q;
			q = q->_Next ;
		}
		if(q == NULL)
		{
			s->_Next  = NULL;
			p->_Next  = s;
		}else
		{
			s->_Next  = q;
		    p->_Next  = s;
		}
	}
	bool Bestapply()
	{
		bool res = false;
	    Node *node = _Buynode(0,0,0);
		cout<<"_Numble  _M_Size"<>node->_Numble>>node->_M_Size;

		Node *p = _Zroot;
		Node *s = _Zroot->_Next ;
		while(p->_Next != NULL)
		{
			if(s->_M_Size == node->_M_Size)
			{
				p->_Next  = s->_Next ;
			
				node->_Address  = s->_Address ;
				Insert_Use(node);
				res = true;
				free(s);
				s = NULL;
				break;
			}
			else if(node->_M_Size < s->_M_Size)
			{
				p->_Next  = s->_Next ;
				node->_Address  = s->_Address ;
				Insert_Use(node);
				s->_Numble  = node->_Numble ;
				s->_M_Size  = s->_M_Size -node->_M_Size ;
				s->_Address  = s->_Address +node->_M_Size ;
				Insert_Zroot(s);
				res = true;
				break;
			}
			else
			{
				p = s;
				s = s->_Next;
			}
		}
	return res;
	}
	bool BestRelease()
	{
		int numble;
		int msize;
		int address;
		cout<<"_Numble  _Address _M_Size"<>numble>>address>>msize;
		
		bool res = false;
		
		Node *p = _Useroot;
        Node *s = _Useroot->_Next;

		while(p->_Next != NULL)
		{
			if(numble == s->_Numble && msize == s->_M_Size && address == s->_Address)
			{
				p->_Next = s->_Next;
				break;
			}
			else
			{
				p = s;
				s = s->_Next;
			}
		}

		if(s == NULL)return res;
		
		res = true;
		p = _Zroot;
		Node *q = _Zroot->_Next;
		Node *prev = NULL;
		Node *next = NULL;
		int tag = 0;
		while(p != NULL && p->_Next != NULL)
		{
			tag = 0;
			if(q->_M_Size +q->_Address == s->_Address)
			{
				prev = q;
				
				p->_Next  = q->_Next ;
				q = p->_Next ;
				tag = 1;
			}
			if(q != NULL && s->_Address +s->_M_Size == q->_Address)
			{
				next = q;
				
				p->_Next = q->_Next ;
				q = p->_Next ;
				tag = 1;
			}
			if(prev != NULL && next != NULL)break;
			if(tag == 0)
			{
				p = q;
			   if(q != NULL)
			   q = q->_Next ;
			}
		}
		if(p != NULL && p->_M_Size +p->_Address == p->_Address)
		{
			prev = p;
			
			p = NULL;
		}

		if(p != NULL && s->_M_Size +s->_Address == s->_Address)
		{
			next = p;
			
			p = NULL;
		}

		if(prev != NULL && next != NULL)
		{
			prev->_M_Size =prev->_M_Size +s->_M_Size +next->_M_Size ;
			Insert_Zroot(prev);
			free(s);
			free(next);
			next = NULL;
			s = NULL;
		}
		else if(prev != NULL)
		{
			prev->_M_Size =prev->_M_Size +s->_M_Size;
			Insert_Zroot(prev);
			free(s);
			s = NULL;
		}
		else if(next != NULL)
		{
			next->_Address = s->_Address ;
			next->_M_Size  = s->_M_Size + next->_M_Size;
			next->_Numble  = s->_Numble ;
			Insert_Zroot(next);
			free(s);
			s = NULL;
		}
		else
		{
			Insert_Zroot(s);
		}
		return res;
	}
	void BestAdaptArithmatic()
	{
		Print(_Zroot);
		int n;
		cout<<"最佳适应算法"<>n;
			switch(n)
			{
			case 1:
				if(!Bestapply())
				{
				    cout<<"没有更大的分区"<>n;
	switch(n)
	{
	case 1:
		LList.FirstAdaptArithmatic();break;
	case 2:
		LList.BestAdaptArithmatic();break;
	case 3:
		cout<<"退出"<






你可能感兴趣的:(算法)