Simulation of allocation and recovery of main memory space in experiment four

Source: Internet
Author: User

1. 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.

2. Experimental content
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.

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.

3. Source code

#include <stdio.h>

#include <iostream.h>

#include <iomanip.h>

#include <malloc.h>

#include <string.h>

#include <stdlib.h>


int bitmap[8][8]={

{1,1,0,0,1,1,1,0},

{0,1,0,1,0,1,0,0},

{0,0,0,0,0,0,0,0},

{1,0,0,0,0,0,0,1},

{0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0}

};

int freecount=54;


typedef struct YEBIAO

{

int Yehao; Page number

int Kuaihao; Block number

struct Yebiao *next;

}yebiao;


typedef struct PROGRESS

{

Char name[30];

int size;

struct Progress *next;

struct Yebiao *yb;

}progress;


/* Allocates memory functions, allocates the required memory for the process and establishes the page table */

Yebiao *allocation (int size)

{

Yebiao *head;

Yebiao *tem;

int n=0;

tem= (Yebiao *) malloc (sizeof (Yebiao));

Head=tem;

for (int i=0;; i++) {

if (bitmap[i/8][i%8]==1) continue;

if (bitmap[i/8][i%8]==0)

{

Bitmap[i/8][i%8]=1; tem->yehao=n; tem->kuaihao=i;

tem->next=null;

n++;

}

if (n==size) break;

Else

{

tem->next= (Yebiao *) malloc (sizeof (Yebiao));

tem=tem->next;

}

}

return head;

}


/* Reclaim Memory */

void recovery (Yebiao *a)

{

Yebiao *b;

while (A->next!=null)

{

bitmap[a->kuaihao/8][a->kuaihao%8]=0; b=a->next;

Free (a);

A=b;

}

bitmap[a->kuaihao/8][a->kuaihao%8]=0;

Free (a);


}

void Outputyebiao (Yebiao *a)

{

cout<< "-------------------------" <<endl;

Do

{

COUT<<A->YEHAO<<SETW (4) <<A->KUAIHAO<<SETW (4) <<endl;

a=a->next;

}while (A!=null); cout<< "-------------------------" <<endl;

}


void Outputbit ()

{

cout<< "Main memory allocation bits view as follows:" <<endl;

cout<< "------------------------------" <<endl;

for (int i=0;i<8;i++)

{

for (int j=0;j<8;j++)

{

COUT<<BITMAP[I][J]<<SETW (4); }

cout<<endl;

}

cout<< "------------------------------" <<endl;

}


PROGRESS *insert (PROGRESS *head,progress *b)

{

PROGRESS *tem;

if (head==null)

{

Head=b;

b->next=null;

}

Else
{

Tem=head;

while (Tem->next!=null)

{

tem=tem->next;

}

tem->next=b;

}

return head;

}


PROGRESS *getprosess (PROGRESS *head)

{

PROGRESS *a;

Char na[30];

int size;

cout<< "Please enter the process name:";

cout<< "Please enter the process, cin>>size;;if (size>freecount)//such as;cout<<" process required memory; returnhead;;a= (PROGRESS*) malloc (size;a->size=size;;freecount-=size;


cout<< "Please enter the amount of memory the process occupies:";

cin>>size;

if (Size>freecount)

{

cout<< "The size of the memory required for the process is larger than the idle memory and the process cannot be added. "<<endl;

return head;

}

A= (PROGRESS *) malloc (sizeof (PROGRESS)); strcpy (A->name,na);

a->size=size;

Freecount-=size;

A->yb=allocation (size);

cout<< "Process created successfully! "<<endl;

a->next=null;

return insert (HEAD,A);

}


void Outputprosess (PROGRESS *head)

{

if (head==null)

{

cout<< "No process at this moment!" <<endl;

Return

}

Do

{

cout<< "process name:" <

The page table for the cout<< "process is as follows:" <<endl;

Outputyebiao (HEAD->YB);

head=head->next;

}while (Head!=null);

}


PROGRESS * delecteprosess (PROGRESS *head)

{

Char na[30];

PROGRESS *a,*b;

cout<< "Please enter the name of the process to be deleted:";

if (head==null) {

cout<< "process linked list is empty, no process can be deleted. "<<endl;

return NULL;

}

if (strcmp (Head->name,na) ==0)

{

Recovery (HEAD->YB); freecount+=head->size;

a=head->next;

Free (head); cout<< "Process" <<na<< "Delete succeeded!" <<endl;

return A;

}

a=head->next;

B=head;

while (A!=null) {

if (strcmp (A->name,na) ==0)

{

Recovery (A->YB);

freecount+=a->size;

b->next=a->next;

Free (a);

cout<< "Process" <<na<< "Delete succeeded!" <<endl;

return head;

}

B=a;

a=a->next;

}

cout<< "The process name you entered is not correct, the delete process failed. "<<endl;

return head;

}


void Outbiaoti ()

{

cout<< "\t\t\t main memory space allocation and recycling simulation" <<endl;

cout<< "You can do the following: 1. Check the memory allocation situation; 2. Review the process; 3. Add process 4. Delete process "<<endl;

cout<< "\t\t available Memory" <<freecount<<endl;

}


void Main ()

{

int n;

PROGRESS *head; Head=null;

System ("Color 2f");

Outbiaoti ();

for (;;)

{

cin>>n;

Switch (n)

{

Case 1:outputbit (); Outbiaoti ();

Case 2:system ("CLS"), Outputprosess (head); Outbiaoti (); break; Case 3:system ("CLS"); Head=getprosess (head); Outbiaoti (); break; Case 4:system ("CLS"); Head=delecteprosess (head); Outbiaoti (); break; Default:break;

}

}

}

Simulation of allocation and recovery of main memory space in experiment four

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.