Simulation of allocation and recovery of main memory space in experiment four

Source: Internet
Author: User

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 <iostream.h> 2 #include <stdlib.h> 3 #define FREE 0//Idle status 4 #define BUSY 1//Used status 5 #defin    E OK 1//complete 6 #define ERROR 0//error 7 #define MAX_LENGTH 32767//maximum memory space is 32767KB 8 typedef int STATUS;    9 int n=0;   The ten typedef struct FREEAREA//defines a table structure for the idle zone description, one for each {int ID;   Partition code a long size; Partition size: Long address;   Partition address: int state;   Status:}elemtype; A two-way list storage structure for the linear table of the----------------------typedef struct DULNODE//double linked list {Elemtype    Data a struct dulnode *prior; The precursor pointer is Dulnode *next;   Follow-up pointer of}dulnode,*dulinklist; Dulinklist Block_first; Head node Dulinklist block_last; The tail node is the status alloc (int);//memory allocation in the status free (int); Memory recovery of the status First_fit (Int,int);//first-time adaptation algorithm, Best_fit (int,int);     The best adaptive algorithm is void Show ();//view assigned the status InitBlock ();//Create a space table of InitBlock status ()//Create a Memory space link table for the lead node 37 {38Block_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; $//-----------------------allocated main memory-------------------------alloc (int ch)   Id,request;    cout<< "Please enter the work (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; (ch==2)//Select the best Fit Algorithm (Best_fit (id,request) ==ok) cout<< "allocated successfully!"   <<endl; Or else Cout<< "Insufficient memory, allocation failed!"   <<endl;   The return OK; (+})-The default first-time adaptation algorithm (First_fit (id,request) ==ok) cout<< "allocated successfully!"   <<endl; cout<< else-"Insufficient memory, allocation failed!"   <<endl;   The return OK; +//------------------first-time adaptation algorithm-----------------------first_fit Status (int id,int request)//Incoming job name and    Amount of Application 80 {81//open up new space for the application and initialize the Dulinklist temp= (dulinklist) malloc (sizeof (Dulnode));    temp->data.id=id;   temp->data.size=request;         temp->data.state=busy;   Dulnode *p=block_first->next; (p) p->data.state==free {p->data.size==request) 90 {//have size   Exactly the right free block of the p->data.state=busy;   p->data.id=id;   The return OK;   94 break;    (P->data.state==free && p->data.size>request) 97     {//There are free blocks to meet demand and there are remaining "98 temp->prior=p->prior;        temp->next=p;  temp->data.address=p->data.address;   101 p->prior->next=temp;  102 p->prior=temp;  103 p->data.address=temp->data.address+temp->data.size;  104 p->data.size-=request;  return OK;  106 break;  107} 108 p=p->next;  109} return ERROR;     111}//--------------------best fit Algorithm------------------------113 Status best_fit (int id,int request) 114 {115 int ch;   Record minimum remaining space of Dulinklist temp= (dulinklist) malloc (sizeof (Dulnode));   117 temp->data.id=id;  118 temp->data.size=request;  119 temp->data.state=busy;  Dulnode *p=block_first->next; 121 Dulnode *q=null; Record the best insertion position 122 while (p)//Initialize minimum space and best position 123 {124 if (P->data.state==free && (p->data.siz E&gT;request | |  p->data.size==request)) (126 q=p;  127 ch=p->data.size-request;  ;    129} p=p->next; 131} while (p) 133 {134 if (P->data.state==free && p->data.size==request) 13  5 {//free block size exactly right 136 p->data.id=id;  137 p->data.state=busy;  138 return OK;  139 break; 141 if (P->data.state==free && p->data.size>request) 142 {//idle block greater than allocation requirement 14 3 if (P-&GT;DATA.SIZE-REQUEST&LT;CH)//The remaining space is smaller than the initial value 144 {145 ch=p->data.size-requ   est;//Update remaining minimum 146 q=p;//update best location point to 147} 148} 149 p=p->next; 151 if (Q==null) return error;//did not find the free block of else 153 {//found the best location and implemented the assignment 154 temp->prior=   q->prior;   155 temp->next=q;     156    temp->data.address=q->data.address;  157 q->prior->next=temp;  158 q->prior=temp;  159 q->data.address+=request;  q->data.size=ch; 161 return OK;     162} 163} 164//-----------------------main memory Recycle--------------------165 Status free (int ID) 166 {167   Dulnode *p=block_first;  168 while (p) 169 {P->data.id==id) 171 {172 p->data.state=free;  173 p->data.id=free; 174 if (P->prior->data.state==free)//connected to the previous free block 175 {176 P->prior->da ta.size+=p->data.size;  177 p->prior->next=p->next;  178 p->next->prior=p->prior;                 179} if (P->next->data.state==free)//connected to the free block behind 181 {182  p->data.size+=p->next->data.size;  183 p->next->next->prior=p;184 p->next=p->next->next;   185} 186 break;  187} 188 p=p->next;  189} The return OK; 191} 192//---------------show main memory allocations------------------193 void Show () 194 {195 cout<< "***********---   --------------************\n ";   196 cout<< "* * * * main memory distribution ****\n";    197 cout<< "***********-----------------************\n";   198 Dulnode *p=block_first->next;  199 while (p) @ $ {201 cout<< "Partition number:";  202 if (p->data.id==free) cout<< "Free" <<endl;  203 Else cout<<p->data.id<<endl;  204 cout<< "Start Address:" <<p->data.address<<endl;  205 cout<< "Partition size:" <<p->data.size<< "KB" <<endl;  206 cout<< "state:";  207 if (p->data.state==free) cout<< "Idle" <<endl; 208 Else cout<< "Assigned!" << Endl;  209 cout<< "-----------------------" <<endl;  p=p->next; 211} 212} 213//-----------------------main function---------------------------214 void Main () 215 {216 int ch,    d=0;//Algorithm Selection Tag 217 cout<< "1. First adaptation algorithm 2. Best Fit algorithm 0. exit \ n";    218 cout<< "Please select the allocation algorithm:";    219 cin>>ch; if (ch==0| | ch==1| |    ch==2) d++;  221 while (d==0) 222 {223 cout<< "Please select the correct number 0, 1 or 2" <<endl;    224 cin>>ch; 225 if (ch==0| | ch==1| |   ch==2) d++;   226} 227 if (ch==0) exit (0); 228 if (n==0) InitBlock (); Create spatial table 229 int choice;    Operation selection mark (1) 231 {232 cout<< "********************************************\n";   233 cout<< "* * 1: Allocate memory 2: Reclaim memory **\n";   234 cout<< "* * 3: View assignment 0: return **\n";  235 cout<< "********************************************\n";         236cout<< "Please enter your operation:";  237 cin>>choice;   238 if (choice==1) 239 {alloc (ch);//Allocate memory 241 n++;    242} 243 Else if (choice==2)//Memory reclaim 244 {245 int ID;    246 cout<< "Please enter the partition number you want to release:";    247 cin>>id;   248 free (ID);    249 n++;   251 else if (choice==3) 252 {253 show ();//display main memory 254 n++;   255} (choice==0) 257 {258 main ();//exit 259 n++;   260} 261 else//Input error 262 {263 cout<< "wrong input, please retry!\n" <<endl;   Continue; 265} 266} 267}

  

Simulation of allocation and recovery of main memory space in experiment four

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.