Experiment four, main memory space allocation and recovery simulation
Internet of Things engineering Liu Ye 201306104146
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.
Second, the contents and requirements of the experiment
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 120KBanddivided into blocks of 8,16,32,64kb size respectively.
The memory required for a process is 0 to one 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
Dynamic partition allocation storage management with continuous allocation, using the first adaptive algorithm, the next adaptation algorithm, the best adaptive algorithm and the worst-fit algorithm to complete the design of 4 algorithms ( optional two 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 reflectedby the change of two tables .
- SOURCE program Name: Experiment Two 1.c
Executable Program Name: 1.exe
- Main program sections and their explanations:
#include "stdio.h"
#include "Stdlib.h"
#define N 10
#define M 10
#define MINISIZE 100
struct{
float address; /* partitioned start address * /
float length; /* partitioned length in bytes * /
int flag;
}used_table[n]; /* allocated Area table * /
struct{
float address; /* idle area start address * /
float length; /* Idle area length, in bytes per unit * /
int flag;
}FREE_TABLE[M]; /* idle Area table * /
void Main ()
{
int i,a;
void allocate (char str,float leg);// allocating main memory space functions
void Reclaim (char str);// Recycle main memory function
float XK;
Char j;/* FREE partition Table initialization:* /
free_table[0].address=10240; /* start address * /
free_table[0].length=102400; /* Address length * /
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 the feature item (0- exit , the - allocated main memory , the recovered main memory , 3- Show main memory) \ n ");
printf (" Select work Item (0~3):");
scanf ("%d", &a);
Switch (a)
{
Case 0:exit (0);
Case 1:
printf (" Enter the job name and the required length of the job :");
scanf ("%*c%c%f", &j,&xk);
Allocate (J,XK);/* allocate main memory space * /
Break
Case 2:
printf (" Enter the job name to reclaim the partition ");
scanf ("%*c%c", &j); reclaim (J);/* reclaim main memory space * /
Break
Case 3:
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);
Else
printf ("%6.0f%9.0f%6d\n", Used_table[i].address,used_table[i].length, Used_table[i].flag);
Break
default:printf (" no option \ n");
}
}
}/* main function End * /
int uflag;// Allocation table Flag
int fflag;// idle table Flag
float uend_address;
float fend_address;
void allocate (char str,float leg)
{
int k,i;float ressize;
uflag=0;fflag=0;
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
}
}
void Reclaim (char str)
{int k,i;
uflag=0;fflag=0;
for (k=0;k<n;k++)
{
if (USED_TABLE[K].FLAG==STR)
{
Uflag=1;break;
}
}
if (uflag==0)
printf ("\ n cannot find the job !\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)// up adjacent
{
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 recovered !\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 recovered !\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 recovered !\n");
}
}
}
Iv. Summary of the experiment:
The allocation and recovery of main memory space, the variable partitioning method is divided by the size of the main memory space required by the job. When you want to load a job, depending on the main memory capacity required for the job to see if there is enough free space, if any, then on demand, otherwise, the job cannot be loaded. In the programming encountered the problem of the algorithm, and later after consulting students and search resources on the Internet to obtain results.
Simulation of allocation and recovery of main memory space in experiment four