Experiment Four simulation of allocation and recovery of main memory space
First, the purpose of the experiment
In order to allocate and use these storage spaces rationally, when the user requests the main memory space, the storage management must analyze the memory space and usage situation according to the request of the applicant, and find out enough free area for the applicant. When the job is withdrawn back to the main memory resource, the storage management needs to reclaim the occupied main memory space. The allocation and recovery of main memory is related to the management mode of primary storage, and through this experiment, we can understand how to realize the allocation and recovery of memory space in different storage management modes.
A high-level language to complete the allocation of a main memory space and recycling simulation program, in order to deepen the memory allocation method and its algorithm understanding.
Ii. contents of the experiment
2.1 Simulation consists of 3 parts:
1) Implement a specific memory allocation algorithm
2) Implement memory recovery simulation
3) The number of fragments per memory allocation policy corresponding to the statistics
2.2 Fixed partition Storage management
Assume that the memory capacity is 120KB and divided into blocks of 8,16,32,64kb size respectively.
The memory required for a process is 0 to 100 KB. Also assume that the amount of memory required for a process to run is constant.
Simulate five processes to reach the request allocation and run the recycle situation, output main memory allocation table.
2.3 Dynamic partition allocation storage Management
Using the dynamic partition allocation storage management of continuous distribution, the design is completed by the first adaptive algorithm, the next adaptive algorithm, the best adaptive algorithm and the worst-fit algorithm (4 optional two kinds of algorithms).
(1) in the process of running the program, the user specified the application and release.
(2) Design a occupied partition table to save the main memory space occupied at a certain time.
(3) An idle partition table is designed to save the remaining space of main memory at a certain time.
(4) The application and release of the memory required for each process is reflected by the change of two tables.
Test methods, steps and Results
1. SOURCE program
#include "stdio.h"
#include "stdlib.h" #define N 10//assumes that the maximum allowable job for the system is n, assuming that the N value in the simulation experiment is 10*/#define M 10/* Assumes the system allows a maximum of m for the idle area table, assuming that the M value in the simulation experiment is 10*/#define Minisize 100
#define MAX_LENGTH 32767//maximum memory space is 32767KB
typedef int Status;
int n=0;
typedef struct FREEAREA//defines an idle area description table structure
{
int ID; Partition code
Long size; Partition size
Long address; Partition address
int state; State
}elemtype;
Two-way linked list storage structure----------linear table------------
typedef struct DULNODE//double linked list
{
Elemtype data;
struct Dulnode *prior; Pre-trend pointer
struct Dulnode *next; Successor pointers
}dulnode,*dulinklist;
Dulinklist Block_first; Head Knot Point
Dulinklist Block_last; Tail knot Point
Status alloc (int);//Memory allocation
Status free (int); Memory Recycling
Status First_fit (int,int);//First adaptation algorithm
Status Best_fit (Int,int); Best Fit algorithm
void Show ();//View Assignments
Status InitBlock ();//Create Space table
Status InitBlock ()//Create a list of memory spaces for the lead node
{
Block_first= (dulinklist) malloc (sizeof (Dulnode));
Block_last= (dulinklist) malloc (sizeof (Dulnode));
block_first->prior=null;
block_first->next=block_last;
block_last->prior=block_first;
block_last->next=null;
block_last->data.address=0;
block_last->data.size=max_length;
block_last->data.id=0;
block_last->data.state=free;
return OK;
}
-----------------------Allocating main Memory-------------------------
Status alloc (int ch)
{
int id,request;
cout<< "Please enter a job (partition code):";
cin>>id;
cout<< "Please enter the main memory size to be allocated (in KB):";
cin>>request;
if (request<0 | | request==0)
{
cout<< "The allocation size is inappropriate, please try again!" <<endl;
return ERROR;
}
if (ch==2)//select best Fit algorithm
{
if (Best_fit (id,request) ==ok) cout<< "Assignment succeeded!" <<endl;
else cout<< "Insufficient memory, allocation failed!" <<endl;
return OK;
}
else//Default first-time adaptation algorithm
{
if (First_fit (id,request) ==ok) cout<< "Assignment succeeded!" <<endl;
else cout<< "Insufficient memory, allocation failed!" <<endl;
return OK;
}
}
------------------First adaptation algorithm-----------------------
Status first_fit (int id,int request)//Incoming job name and application volume
{
Open a new space for the job application and initialize
Dulinklist temp= (dulinklist) malloc (sizeof (Dulnode));
temp->data.id=id;
temp->data.size=request;
temp->data.state=busy;
Dulnode *p=block_first->next;
while (p)
{
if (P->data.state==free && p->data.size==request)
{//There are free blocks of the right size
p->data.state=busy;
p->data.id=id;
return OK;
Break
}
if (P->data.state==free && p->data.size>request)
{//There are free blocks to meet demand and there are remaining "
temp->prior=p->prior;
temp->next=p;
temp->data.address=p->data.address;
p->prior->next=temp;
p->prior=temp;
p->data.address=temp->data.address+temp->data.size;
p->data.size-=request;
return OK;
Break
}
p=p->next;
}
return ERROR;
}
--------------------Optimal Adaptive Algorithm------------------------
Status best_fit (int id,int request)
{
int ch; Record minimum remaining space
Dulinklist temp= (dulinklist) malloc (sizeof (Dulnode));
temp->data.id=id;
temp->data.size=request;
temp->data.state=busy;
Dulnode *p=block_first->next;
Dulnode *q=null; Record the best insertion position
while (p)//Initialize minimum space and best position
{
if (P->data.state==free && (p->data.size>request | | p->data.size==request))
{
Q=p;
ch=p->data.size-request;
Break
}
p=p->next;
}
while (p)
{
if (P->data.state==free && p->data.size==request)
{//Free block size exactly right
p->data.id=id;
p->data.state=busy;
return OK;
Break
}
if (P->data.state==free && p->data.size>request)
{//idle block greater than allocation requirement
if (p->data.size-request<ch)//The remaining space is smaller than the initial value
{
ch=p->data.size-request;//Update remaining minimum value
q=p;//Update best location point
}
}
p=p->next;
}
if (q==null) return error;//no free blocks found
Else
{//Find the best location and implement the assignment
temp->prior=q->prior;
temp->next=q;
temp->data.address=q->data.address;
q->prior->next=temp;
q->prior=temp;
q->data.address+=request;
q->data.size=ch;
return OK;
}
}
-----------------------Main Memory Recycling--------------------
Status free (int ID)
{
Dulnode *p=block_first;
while (p)
{
if (P->data.id==id)
{
p->data.state=free;
p->data.id=free;
if (P->prior->data.state==free)//connected to the previous free block
{
p->prior->data.size+=p->data.size;
p->prior->next=p->next;
p->next->prior=p->prior;
}
if (P->next->data.state==free)//connected to the free block behind
{
p->data.size+=p->next->data.size;
p->next->next->prior=p;
p->next=p->next->next;
}
Break
}
p=p->next;
}
return OK;
}
---------------shows the allocation of main memory------------------
void Show ()
{
cout<< "***********-----------------************\n";
cout<< "* * * * Main memory allocation situation ****\n";
cout<< "***********-----------------************\n";
Dulnode *p=block_first->next;
while (p)
{
cout<< "Partition number:";
if (p->data.id==free) cout<< "Free" <<endl;
else cout<<p->data.id<<endl;
cout<< "Start Address:" <<p->data.address<<endl;
cout<< "Partition size:" <<p->data.size<< "KB" <<endl;
cout<< "state:";
if (p->data.state==free) cout<< "Idle" <<endl;
else cout<< "Assigned!" <<endl;
cout<< "-----------------------" <<endl;
p=p->next;
}
}
-----------------------Main Function---------------------------
void Main ()
{
int ch,d=0;//Algorithm Selection tag
cout<< "1. First adaptation algorithm 2. Best Fit algorithm 0. exit \ n";
cout<< "Please select the allocation algorithm:";
cin>>ch;
if (ch==0| | ch==1| | ch==2) d++;
while (d==0)
{
cout<< "Please select the correct number 0, 1 or 2" <<endl;
cin>>ch;
if (ch==0| | ch==1| | ch==2) d++;
}
if (ch==0) exit (0);
if (n==0) InitBlock (); Create a space table
int choice; Action selection Tag
while (1)
{
cout<< "********************************************\n";
cout<< "* * 1: Allocate memory 2: Reclaim memory **\n";
cout<< "* * 3: View assignment 0: return **\n";
cout<< "********************************************\n";
cout<< "Please enter your operation:";
cin>>choice;
if (choice==1)
{
Alloc (CH); Allocating memory
n++;
}
else if (choice==2)//Memory reclamation
{
int ID;
cout<< "Please enter the partition number you want to release:";
cin>>id;
Free (ID);
n++;
}
else if (choice==3)
{
Show ();//Display main memory
n++;
}
else if (choice==0)
{
Main (); Exit
n++;
}
else/Wrong input operation
{
cout<< "wrong input, please retry!\n" <<endl;
Continue
}
}
}
2. Results
3. Results analysis
This experiment I refer to the online code and the code of the students, in the process of understanding is still a bit laborious, but simple to understand the theoretical knowledge, but the hands-on ability is relatively weak, in the future study to be strengthened!
Experiment four main memory space allocation and recycling simulation