Allocation and recycling of memory

Source: Internet
Author: User

Experiment Three allocation and recycling of memory

Internet of Things engineering Rai Yanfei 201306104147

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.

Second, the contents and requirements of the experiment

Requirements:

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

A. Fixed partition storage management;

B. Dynamic partition allocation Storage Management

Content:

1) 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) 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 name: gu (rar or zip) source program name 1.c,2.c

    1. Executable Program Name: 1.exe,2.exe

Principle Analysis and flowchart

1. Fixed partition memory management

Main design ideas:

1. Fixed partition memory management

The program defines an array home, due to the fixed allocation of 5 fixed-size memory space, the first 5 fixed-size memory space of the individual properties of the first initialization, and then remind the user, enter the name and size of the process to be executed, when the size of the memory is larger and closest to the size of the process, and is not occupied, The state of the memory is changed to the name of the process, and the next time the user enters the process, it does not consume chunks of memory already occupied by other processes.

2. Dynamic Partition allocation Storage Management

The program defines two arrays that hold the start address, length, and end address of the idle and job areas, respectively. First, the number of idle areas is initialized to 1, then remind the user to enter the size of the process, the corresponding new data in the job, in the idle area is one less data.

Main Program section and its interpretation:

1. Fixed partition memory management

typedef struct save{

int sort;

int size;

int start;

Char state;

}node;

Node home[10];

Initialize the individual properties of a block of memory

void Inflate ()

{

Home[1].sort=1;

home[1].size=8;

home[1].start=30;

home[1].state= ' 0 ';

home[2].sort=2;

home[2].size=16;

home[2].start=38;

home[2].state= ' 0 ';

home[3].sort=3;

home[3].size=32;

home[3].start=54;

home[3].state= ' 0 ';

home[4].sort=4;

home[4].size=64;

home[4].start=118;

home[4].state= ' 0 ';

}

Display information for each block of memory

void Show ()

{

int i;

printf ("Show The Message\n");

for (i=1;i<5;i++) {

printf ("%5d%5d%5d%5c", home[i].sort,home[i].size,home[i].start,home[i].state);

printf ("\ n");

}

}

Main ()

{

char name;

int m;

int j,flag=1;

Inflate ();//Initialize block of memory

Show ();

Defines a flag bit flag, if flag 1 keeps reminding the user to enter the process, otherwise the memory block is fully occupied and cannot allocate memory space for other processes.

while (flag)

{

printf ("Please input the process of name and size\n");

scanf ("%c%d", &name,&m);

flag=0;

for (j=1;j<5;j++) {

if (home[j].state== ' 0 ' && home[j].size>m) {

Home[j].state=name;

flag=1;

Break

}

}

if (flag==0) {

printf ("Cannot allocate \ n");

}

for (j=1;j<5;j++)

{

printf ("%5d%5d%5d%5c", home[j].sort,home[j].size,home[j].start,home[j].state);

printf ("\ n");

if (home[j].state== ' 0 ')

{

flag=1;

}

}

GetChar ();

}

    1. Dynamic Partition allocation Storage Management

#define N 10000

int xian;//number of free partitions

int zuo;//The number of work areas

struct Kongxian

{

int start; Starting address

int end; End

int length; Length

}kongxian[n];

struct Zuoye

{

int start; Starting address

int end; End

int length; Length

}zuoye[n];

int cmp1 (const void *a,const void *b)

{

return (* (struct Kongxian *) a). start-(* (struct Kongxian *) b). Start;

}

int cmp2 (const void *a,const void *b)

{

return (* (struct Zuoye *) a). start-(* (struct Zuoye *) b). Start;

}

void Init ()

{

Xian=1; Initially there is only one free zone

zuo=0; No jobs initially

kongxian[0].start=0;

kongxian[0].end=1023;

kongxian[0].length=1024;

}

void Print1 ()//Print free partition

{

int i;

for (i=0;i<xian;i++)

printf ("Idle partition id:%d:%d end:%d length:%d\n", i,kongxian[i].start,kongxian[i].end,kongxian[i].length);

}

void Print2 ()//print job partition

{

int i;

for (i=0;i<zuo;i++)

printf ("Job partition id:%d:%d end:%d length:%d\n", i,zuoye[i].start,zuoye[i].end,zuoye[i].length);

}

int main ()

{

int i,j,k,t,len,flag,id;

int Front,middle, behind;

int t1,t2;

Init ();

Print1 ();

printf ("Input 1 load new job, enter 0 Recycle job, enter-1 end \ n");

while (scanf ("%d", &t)!=eof)

{

if (t==1)//Load New job

{

printf ("Please enter the length of space occupied by the job");

scanf ("%d", &len);

flag=0;

for (i=0;i<xian;i++)

{

if (Kongxian[i].length>=len)//first-time adaptation algorithm

{

flag=1;

Break

}

}

if (!flag)

{

printf ("Memory allocation failed \ n");

}

Else

{

Add the job to the job area

Zuoye[zuo].start=kongxian[i].start;

Zuoye[zuo].end=zuoye[zuo].start+len;

Zuoye[zuo].length=len;

zuo++; Number of jobs plus 1

if (Kongxian[i].length==len)//The partition is all used for allocation, delete the free partition

{

for (j=i;j<xian-1;j++)

{

Kongxian[j].start=kongxian[j+1].start;

Kongxian[j].end=kongxian[j+1].end;

Kongxian[j].length=kongxian[j+1].length;

}

xian--;

}

else//The free partition portion is used for allocation, leaving the remaining in the free partition

{

Kongxian[i].start+=len;

Kongxian[i].length-=len;

}

}

}

else if (t==0)

{

printf ("Enter the job ID to be recycled");

scanf ("%d", &id);

Front=middle=behind=0;

for (i=0;i<xian;i++)

{

if (kongxian[i].start>zuoye[id].end)

Break

if (Kongxian[i].end==zuoye[id].start)//The job to be recycled has an idle partition on it

{

Front=1;

T1=i;

}

if (kongxian[i].start==zuoye[id].end)//The job to be recycled has an idle partition below it

{

behind=1;

T2=i;

}

}

if (!front&&!behind)//The job to be recycled does not have an idle partition up or down

{

Kongxian[xian].start=zuoye[id].start;

Kongxian[xian].end=zuoye[id].end;

Kongxian[xian].length=zuoye[id].length;

xian++; The free partition adds a

Qsort (kongxian,xian,sizeof (struct Kongxian), CMP1); Sort after inserting an idle partition

for (j=id;j<zuo-1;j++)//delete the job in the job partition

{

Zuoye[j].start=zuoye[j+1].start;

Zuoye[j].end=zuoye[j+1].end;

Zuoye[j].length=zuoye[j+1].length;

}

zuo--;

}

if (front &&behind)//The job to be recycled has free partitions up and down

middle=1;

if (front&&!middle)//merge the jobs to be reclaimed and the above free partitions

{

Kongxian[t1].end+=zuoye[id].length;

Kongxian[t1].length+=zuoye[id].length;

for (j=id;j<zuo-1;j++)//delete the job in the job partition

{

Zuoye[j].start=zuoye[j+1].start;

Zuoye[j].end=zuoye[j+1].end;

Zuoye[j].length=zuoye[j+1].length;

}

zuo--;

}

if (middle)//merge the jobs to be reclaimed and the upper and lower free partitions

{

Kongxian[t1].end=kongxian[t2].end;

kongxian[t1].length+= (zuoye[id].length+kongxian[t2].length);

Delete an idle partition t2

for (j=t2;j<xian-1;j++)

{

Kongxian[j].start=kongxian[j+1].start;

Kongxian[j].end=kongxian[j+1].end;

Kongxian[j].length=kongxian[j+1].length;

}

xian--;

for (j=id;j<zuo-1;j++)//delete the job in the job partition

{

Zuoye[j].start=zuoye[j+1].start;

Zuoye[j].end=zuoye[j+1].end;

Zuoye[j].length=zuoye[j+1].length;

}

zuo--;

}

if (behind &&!middle)//merge the jobs to be reclaimed and the following partitions

{

Kongxian[t2].start-=zuoye[id].length;

Kongxian[t2].length+=zuoye[id].length;

for (j=id;j<zuo-1;j++)//delete the job in the job partition

{

Zuoye[j].start=zuoye[j+1].start;

Zuoye[j].end=zuoye[j+1].end;

Zuoye[j].length=zuoye[j+1].length;

}

zuo--;

}

}

Else

{

printf ("End of operation \ n");

Break

}

Print1 ();

Print2 ();

}

return 0;

}

    1. 2. operation results and Analysis

1. Fixed partition memory management

2. Dynamic Partition allocation Storage Management

Iv. Summary of the experiment

Through this experiment, from the very beginning do not understand how to implement memory allocation management, to now probably understand the process memory allocation management algorithm. In the programming process, through the exchange of thoughts with the students, as well as their own after-school thinking and online access to relevant program fragments, pick up the online program, and understand the online program algorithm implementation process. This experiment is I picked the program on the Internet, coupled with their own understanding, the implementation of a simple algorithm. The most profound feeling of the experiment is their C language Foundation thin, of course, there is a great harvest is the ability to exercise their own debugging programs and programming thinking to get exercise. Another important experience is to be good at using some useful resources on the web, which can be a useful tool for you to learn.

Allocation and recycling of memory

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.