Experiment four, main memory space allocation and recovery simulation

Source: Internet
Author: User

Experiment four, the allocation of main memory space and the recycling model

Internet of Things engineering Wang Yu 201306104128

First, 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.

Second, experimental content and requirements

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.

Third, test methods, procedures and results

1. SOURCE program Name: Compressed package file (RAR or Zip) source program name Zxl4.c

Executable Program Name: Zxl.exe

2. Principle Analysis and flowchart

①. First set up a structure-type memory partition that holds the starting address, length, task name, and the pointer to the next free partition of the memory;

②. Set the Showmemory function to show the current memory allocation;

③.memoallocate function for allocating memory;

④.minsert function, function Insert task to free partition;

⑤.memoreturn function, used to reclaim memory; Mreturn function, function to reclaim memory;

⑥.main function, the user chooses, the system responds.

3. Main procedural paragraphs and their explanations:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PROCESS
{
int number;
Char name[20];
int begin;
int size;
char status;
}pro;
Main ()
{
int sys=512;
char s;
int i=0;
int count1=0;
int count2=0;
int flag=1;
Char j[10];
Pro A[10];
strcpy (A[0].name, "system");//a[0] is a system process
a[0].begin=0;
a[0].size=100;
A[0].status= ' F ';

printf ("Memory size%dk\n", SYS);
printf ("Occupy%dk\n\n", a[0].size);

printf ("Idle area table free:\n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
printf ("\tno.1\t-----\t\t%d\t%d\t f\n\n", a[0].size,sys-a[0].size);

printf ("==============================================\n");
printf ("Allocated partition table:: \ n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
printf ("\tno.1\t%s\t\t%d\t%d\t%c\n", a[0].name,a[0].begin,a[0].size,a[0].status);
printf ("\ n");

printf ("Memory usage: \ n");
printf ("printf sorted by address::\n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
printf ("\ t---------------------------------------------\ n");
printf ("\tno.1\t%s\t\t%d\t%d\t u\n", a[0].name,a[0].begin,a[0].size);
printf ("\tno.2\t-----\t\t%d\t%d\t f\n\n", a[0].size,sys-a[0].size);

printf ("\ n");
I=1;
while (1)
{
printf ("Please select: Assign press 1 to recycle press 2 to exit by pressing e\n");
scanf ("%s", &s);
Switch (s)
{
Case ' 1 ':
printf ("Input process name:");
scanf ("%s", &a[i].name);
printf ("Input process size:");
scanf ("%d", &a[i].size);
a[1].begin=100;
a[i].status= ' u ';
flag++; Current number of processes

printf ("Insert complete!\n");
printf ("Idle area table free:\n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
for (i=0;i<flag;i++)
{
a[i+1].begin=a[i].begin+a[i].size;
Count1=a[i+1].begin;
count2= Sys-count1;
}
printf ("\tno.1\t-----\t\t%d\t%d\t f\n\n", count1,count2);
a[0].status= ' u ';
printf ("============== ================================\n ");
printf ("Allocated partition table: \ n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
for (i=0;i<flag;i++)
{
a[i+1].begin=a[i].begin+a[i].size;
printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", I+1,a[i].name,a[i].begin,a[i].size,a[i].status);
}
printf ("\ n");

printf ("Memory usage: \ n");
printf ("printf sorted by address:\n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
printf ("\ t--------------------------------------------\ n");
for (i=0;i<flag;i++)
{
printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i+1,a[i].name,a[i].begin,a[i].size,a[i ].status);
}
printf ("\tno.%d\t-----\t\t%d\t%d\t f\n\n", flag+1,count1,count2);
break;
Case ' 2 ':
printf (" Enter the process name: ");
scanf ("%s", &j);
printf ("Recycle succeeded!\n");
for (i=0;i<flag;i++)
{
if (strcmp (j,a[i].name) ==0)
{
strcpy (a[i].name, "-----");
A[i]. Status= ' F ';
}
}
printf ("Idle area Table free:\n"),
printf ("\tno.\t proname\tbegin\t size\t status\n");
for (i=0;i<flag; i++)
{
if (a[i].status== ' F ') {
printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i,a[i].name,a[i].begin,a[i].size,a[ I].status);}
}
printf ("\tno.%d\t-----\t\t%d\t%d\t f\n\n", flag-1,count1,count2);

printf ("==============================================\n");
printf ("Allocated partition table:: \ n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
for (i=0;i<flag;i++)
{
if (a[i].status== ' u ') {
printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i,a[i].name,a[i].begin,a[i].size,a[i].status);}
}
printf ("\ n");

printf ("Memory usage: \ n");
printf ("printf sorted by address:\n");
printf ("\tno.\t proname\tbegin\t size\t status\n");
printf ("\ t--------------------------------------------\ n");
for (i=0;i<flag;i++)
{
printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i+1,a[i].name,a[i].begin,a[i].size,a[i].status);
}
printf ("\tno.%d\t-----\t\t%d\t%d\t f\n\n", Flag+1,count1,count2);
Break
Case ' E ':
Exit (0);
Break
}
}
}

4. Operation Result:

Memory usage

Allocation process

Recycling process

Exit interface

5. Experience:

The difficulty of the experiment is generally difficult, part of the code from the network, but most can understand, and then try to write a time. Attention should be paid to independent thinking in programming.

Experiment four, main memory space allocation and recovery simulation

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.