simulation of allocation and recovery of main memory space in experiment four
Huangnan 201306104125
1.
Experimental Purpose
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.
2.
Experimental Content
2.1 simulations include 3 section:
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.
3. Experimental environment
Complete the design, coding and commissioning work according to the assigned experimental project and complete the experiment report .
Turbo C can be chosen as the development environment. You can also use VB,CB or other visual environments under Windows to make it easier to take advantage of a variety of controls. Choose the experimental environment independently.
4. Experiment Code:
#include"stdio.h"#include"stdlib.h"#defineN 10/* assumes that the maximum allowable job for the system is n, assuming that the N value in the simulation experiment is 10*/#defineM 10/* assumes that the system allows a maximum of m for the idle area table, assuming that the M value in the simulation experiment is 10*/#defineMinisize 100struct{ floatAddress/*Partitioned start Address*/ floatLength/*partition length, in bytes*/ intFlag;} Used_table[n]; struct{ floataddress; floatlength; intFlag;} FREE_TABLE[M]; voidMain () {intI,a;voidAllocateCharStrfloatLEG);//allocating main memory space functions voidReclaimCharSTR);//Recycle main memory function floatxk;CharJ/*free partition Table initialization:*/free_table[0].address=10240; free_table[0].length=102400; free_table[0].flag=1; for(i=1; i<m;i++) Free_table[i].flag=0;/*Allocated Table initialization:*/ for(i=0; i<n;i++) Used_table[i].flag=0; while(1) {printf ("\ n Select feature Item (0-Exit, 1-Allocate main memory, 2-main memory, 3-show main memory) \ n"); printf ("Select a work item (0~3):"); scanf ("%d",&a); Switch(a) { Case 0: Exit (0);/*a=0 Program End*/ Case 1:/*a=1 allocating main memory space*/printf"Enter the job name J and the required length of the job XK:"); scanf ("%*c%c%f",&j,&xk); Allocate (J,XK);/*Allocating main memory space*/ Break; Case 2:/*a=2 Reclaim main memory space*/printf"Enter the name of the job to reclaim the partition"); scanf ("%*c%c", &j); reclaim (J);/*Reclaim main Memory space*/ Break; Case 3:/*a=3 display of main memory*//*output the contents of the Idle area table and the allocated table*/printf ("Output idle area table: \ n Start address partition length flag \ n"); for(i=0; i<m;i++) printf ("%6.0f%9.0f%6d\n", Free_table[i].address,free_table[i].length, Free_table[i].flag); printf ("Press any key to output the allocated area table \ n"); GetChar (); printf ("Output allocated Area table: \ n Start address partition length flag \ n"); for(i=0; i<n;i++) if(used_table[i].flag!=0) printf ("%6.0f%9.0f%6c\n", Used_table[i].address,used_table[i].length, Used_table[i].flag); Elseprintf ("%6.0f%9.0f%6d\n", Used_table[i].address,used_table[i].length, Used_table[i].flag); Break; default:p rintf ("without this option \ n"); }/* Case*/ }/* while*/}/*end of main function*/ intUflag;//Allocation Table FlagsintFflag;//Idle Table Flagfloatuend_address;floatfend_address;voidAllocateCharStrfloatleg) {Uflag=0; fflag=0; intK,i;floatressize; for(i=0; i<m;i++) { if(free_table[i].flag==1&& free_table[i].length>=leg) {Fflag=1; Break; } } if(fflag==0) printf ("no idle area satisfies the condition \ n"); Else{ressize=free_table[i].length-leg; for(k=0; k<n;k++) { if(used_table[k].flag==0) { if(ressize<minisize)//The remaining block is too small{used_table[k].length=free_table[i].length; Used_table[k].address=free_table[i].address; Used_table[k].flag=str; Free_table[i].length=0; Free_table[i].flag=0; Break; } Else{used_table[k].address=free_table[i].address+ressize; Used_table[k].flag=str; Used_table[k].length=leg; Free_table[i].length=ressize; Break; } } }//For End }}voidReclaimCharstr) {Uflag=0; fflag=0; intk,i; for(k=0; k<n;k++) { if(used_table[k].flag==str) {Uflag=1; Break; } } if(uflag==0) printf ("\ nthe job could not be found!\n"); Else { for(i=0; i<m;i++) {uend_address=used_table[k].address+used_table[k].length; Fend_address=free_table[i].address+free_table[i].length; if(used_table[k].address==fend_address)//Upper Neighbor{Fflag=1; Free_table[i].length=free_table[i].length+used_table[k].length; Free_table[i].flag=1; Used_table[k].flag=0; Used_table[k].length=0; Used_table[k].address=0; printf ("\ n has recycled!\n"); Break; } Else { if(free_table[i].address==uend_address)//Next Neighbor{Fflag=1; Free_table[i].address=used_table[k].address; Free_table[i].length=free_table[i].length+used_table[k].length; Free_table[i].flag=1; Used_table[k].flag=0; Used_table[k].length=0; Used_table[k].address=0; printf ("\ n has recycled!\n"); Break; } } }//For End if(fflag==0) {i=0; for(i=0; i<m;i++) { if(free_table[i].flag==0) {free_table[i].address=used_table[k].address; Free_table[i].length=used_table[k].length; Free_table[i].flag=1; Used_table[k].length=0; Used_table[k].flag=0; Used_table[k].address=0; Break; }} printf ("\ n has recycled!\n"); } }}
5. Experiment:
6. Experiment Summary:
This is the last experimental lesson, although there are many questions about spatial dynamic storage at the end. will continue to understand in the future study.
Simulation of allocation and recovery of main memory space in experiment four