Experiment four: allocation and recovery of main memory space
Major: Network engineering Name: Tang No. 201306114127
First, Experimental Purpose
In the high-level language to complete a main memory space allocation and recycling program, in order to deepen the dynamic partition allocation method and its algorithm understanding.
Second, experimental content and requirements
Using the dynamic partition allocation storage management of continuous distribution, the design is completed by the first adaptive algorithm, the first cycle adaptive algorithm, the best adaptive algorithm and the worst-fit Algorithm (4) (optional two algorithms).
(1) * * Design a job application queue and the completion of the release sequence, to achieve the allocation and recovery of main memory. Use the partition description table.
(2) or in the process of running the program, the user specifies the application and release.
(3) Design a table of idle area to save the space of main memory at a certain moment. The changes of the spare Area description table and the application and release status of each job are shown.
Complete the design, coding and commissioning work according to the assigned experimental project and complete the experiment report .
Third, test methods, procedures and results
#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/*assigned Area table Registration column flag, with "0" for empty column*/}used_table[n]; /*Allocated Extents Table*/struct{ floatAddress/*Scratch area Start address*/ floatLength/*the length of the idle area, in bytes*/ intFlag/*Idle Area table Registration bar flag, with "0" for empty columns, "1" for unassigned*/}free_table[m]; /*Table of idle area*/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"); } }}
Four, Experimental Summary
The experiment is very difficult, at first do not know what to do, through the Baidu and ask students to write out.
Experiment four: allocation and recovery of main memory space