Experiment Four allocation and recovery of main memory space
1.Purpose and requirements
1.1. Purpose of the experiment
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.
1.2. Experimental 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.
2.Experimental content
Complete the design, coding and commissioning work according to the assigned experimental project and complete the experiment report .
3.Experimental environment
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.
Source:
#include <stdio.h>
#include <stdlib.h>
#define OK 1//complete
#define ERROR 0//error
typedef int STATUS;
typedef struct FREE_TABLE//Defines an idle area description table structure
{
int num; Partition number
Long address; Start Address
Long length; Partition size
int state; Partition status
}elemtype;
A doubly linked list storage structure of typedef struct NODE//linear table
{
Elemtype data;
struct Node *prior; Pre-trend pointer
struct Node *next; Successor pointers
}node,*linklist;
Linklist first; Head Knot Point
Linklist end; Tail knot Point
int flag;//Record the partition number to delete
Status InitBlock ()//Create a list of memory spaces for the lead node
{
First= (linklist) malloc (sizeof (Node));
End= (linklist) malloc (sizeof (Node));
first->prior=null;
first->next=end;
end->prior=first;
end->next=null;
end->data.num=1;
end->data.address=40;
end->data.length=600;
end->data.state=0;
return OK;
}
void sort ()//reordering of partition ordinals
{
Node *p=first->next,*q;
q=p->next;
for (;p!=null;p=p->next)
{
for (Q=p->next;q;q=q->next)
{
if (p->data.num>=q->data.num)
{
q->data.num+=1;
}
}
}
}
Show main memory allocation status
void Show ()
{
The int flag=0;//is used to record the partition ordinal
Node *p=first;
p->data.num=0;
p->data.address=0;
p->data.length=40;
p->data.state=1;
Sort ();
printf ("\n\t\t" main memory space allocation situation "\ n");
printf ("**********************************************************\n\n");
printf ("Partition ordinal \ t" start address \ t partition size \ t partition state \ n ");
while (p)
{
printf ("%d\t\t%d\t\t%d", p->data.num,p->data.address,p->data.length);
if (p->data.state==0) printf ("\t\t idle \ n");
else printf ("\t\t has been assigned \ n \ nthe");
p=p->next;
}
printf ("**********************************************************\n\n");
}
First-time Adaptive algorithm
Status first_fit (int request)
{
Open a new space for the job application and initialize
Node *p=first->next;
Linklist temp= (linklist) malloc (sizeof (Node));
temp->data.length=request;
temp->data.state=1;
p->data.num=1;
while (p)
{
if ((p->data.state==0) && (p->data.length==request))
{//There are free blocks of the right size
p->data.state=1;
return OK;
Break
}
else if ((p->data.state==0) && (p->data.length>request))
{//have free blocks to meet demand and have surplus
temp->prior=p->prior;
temp->next=p;
temp->data.address=p->data.address;
temp->data.num=p->data.num;
p->prior->next=temp;
p->prior=temp;
p->data.address=temp->data.address+temp->data.length;
p->data.length-=request;
p->data.num+=1;
return OK;
Break
}
p=p->next;
}
return ERROR;
}
Best Fit algorithm
Status best_fit (int request)
{
int ch; Record minimum remaining space
Node *p=first;
Node *q=null; Record the best insertion position
Linklist temp= (linklist) malloc (sizeof (Node));
temp->data.length=request;
temp->data.state=1;
p->data.num=1;
while (p)//Initialize minimum space and best position
{
if ((p->data.state==0) && (p->data.length>=request))
{
if (q==null)
{
Q=p;
ch=p->data.length-request;
}
else if (Q->data.length > P->data.length)
{
Q=p;
ch=p->data.length-request;
}
}
p=p->next;
}
if (q==null) return error;//no free blocks found
else if (q->data.length==request)
{
q->data.state=1;
return OK;
}
Else
{
temp->prior=q->prior;
temp->next=q;
temp->data.address=q->data.address;
temp->data.num=q->data.num;
q->prior->next=temp;
q->prior=temp;
q->data.address+=request;
q->data.length=ch;
q->data.num+=1;
return OK;
}
return OK;
}
Worst-fit algorithm
Status worst_fit (int request)
{
int ch; Record Maximum remaining space
Node *p=first->next;
Node *q=null; Record the best insertion position
Linklist temp= (linklist) malloc (sizeof (Node));
temp->data.length=request;
temp->data.state=1;
p->data.num=1;
while (p)//Initialize maximum space and best position
{
if (p->data.state==0 && (p->data.length>=request))
{
if (q==null)
{
Q=p;
ch=p->data.length-request;
}
else if (Q->data.length < p->data.length)
{
Q=p;
ch=p->data.length-request;
}
}p=p->next;
}
if (q==null) return error;//no free blocks found
else if (q->data.length==request)
{
q->data.length=1;
return OK;
}
Else
{
temp->prior=q->prior;
temp->next=q;
temp->data.address=q->data.address;
temp->data.num=q->data.num;
q->prior->next=temp;
q->prior=temp;
q->data.address+=request;
q->data.length=ch;
q->data.num+=1;
return OK;
}
return OK;
}//Allocate main memory
Status allocation (int a)
{
int request;//Request Memory Size
printf ("Please enter the main memory size (in KB) for the application allocation:");
scanf ("%d", &request);
if (request<0 | | request==0)
{
printf ("The allocation size is inappropriate, please try again!") ");
return ERROR;
}
Switch (a)
{
Case 1://Default first-time adaptation algorithm
if (First_fit (request) ==ok) printf ("\t**** assignment succeeded!") ****");
else printf ("\t**** Insufficient memory, allocation failed!") ****");
return OK;
Break
Case 2://select best Fit algorithm
if (Best_fit (request) ==ok) printf ("\t**** assignment succeeded!") ****");
else printf ("\t**** Insufficient memory, allocation failed!") ****");
return OK;
Break
Case 3://Select worst Fit algorithm
if (Worst_fit (request) ==ok) printf ("\t**** assignment succeeded!") ****");
else printf ("\t**** Insufficient memory, allocation failed!") ****");
return OK;
Break
}
}
Status Deal1 (Node *p)//Processing Reclaim space
{
Node *q=first;
for (; q!=null;q=q->next)
{
if (q==p)
{
if (q->prior->data.state==0&&q->next->data.state!=0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=q->prior;
q->data.state=0;
q->data.num=flag-1;
}
if (q->prior->data.state!=0&&q->next->data.state==0)
{
q->data.length+=q->next->data.length;
q->next=q->next->next;
q->next->next->prior=q;
q->data.state=0;
q->data.num=flag;
}
if (q->prior->data.state==0&&q->next->data.state==0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=q->prior;
q->data.state=0;
q->data.num=flag-1;
}
if (q->prior->data.state!=0&&q->next->data.state!=0)
{
q->data.state=0;
}
}
}
return OK;
}
Status Deal2 (Node *p)//Processing Reclaim space
{
Node *q=first;
for (; q!=null;q=q->next)
{
if (q==p)
{
if (q->prior->data.state==0&&q->next->data.state!=0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=p->prior;
q->data.state=0;
q->data.num=flag-1;
}
if (q->prior->data.state!=0&&q->next->data.state==0)
{
q->data.state=0;
}
if (q->prior->data.state==0&&q->next->data.state==0)
{
q->prior->data.length+=q->data.length;
q->prior->next=q->next;
q->next->prior=q->prior;
q=q->prior;
q->data.state=0;
q->data.num=flag-1;
}
if (q->prior->data.state!=0&&q->next->data.state!=0)
{
q->data.state=0;
}
}
}
return OK;
}
Main Memory Recycling
Status recovery (int flag)
{
Node *p=first;
for (;p!=null;p=p->next)
{
if (P->data.num==flag)
{
if (P->prior==first)
{
if (p->next!=end)//The next that the current P points to is not the last one
{
if (p->next->data.state==0)
Connected to the free block behind
{
p->data.length+=p->next->data.length;
p->next->next->prior=p;
p->next=p->next->next;
p->data.state=0;
p->data.num=flag;
}
else p->data.state=0;
}
if (p->next==end)//The next that the current P points to is the last time
{
p->data.state=0;
}
}//End of If (P->prior==block_first) case
else if (P->prior!=first)
{
if (p->next!=end)
{
Deal1 (P);
}
Else
{
Deal2 (P);
}
}//End of If (P->prior!=block_first) case
}//End of If (P->data.num==flag) case
}
printf ("\t**** recovery Success");
return OK;
}
Main function
void Main ()
{
int i; Action selection Tag
int a;//Algorithm Selection tag
printf ("**********************************************************\n");
printf ("\t\t in the following three ways to achieve the allocation of main memory space \ n");
printf ("\ t (1) First adaptation algorithm \ t (2) best fit algorithm \ t (3) worst-fit algorithm \ n");
printf ("**********************************************************\n");
printf ("\ n");
printf ("Please enter the memory allocation algorithm used:");
scanf ("%d", &a);
while (a<1| | A>3)
{
printf ("Input error, re-enter the memory allocation algorithm used: \ n");
scanf ("%d", &a);
}
Switch (a)
{
Case 1:printf ("\n\t**** using first-time adaptation algorithm: ****\n");
Case 2:printf ("\n\t**** using best fit algorithm: ****\n");
Case 3:printf ("\n\t**** uses the worst-fit algorithm: ****\n");
}
InitBlock (); Create a space table
while (1)
{
Show ();
printf ("\T1: Allocating memory \t2: Reclaim memory \t0: Exit \ n");
printf ("Please enter your actions:");
scanf ("%d", &i);
if (i==1)
Allocation (a); Allocating memory
else if (i==2)//Memory reclamation
{
printf ("Please enter the partition number you want to release:");
scanf ("%d", &flag);
recovery (flag);
}
else if (i==0)
{
printf ("\ nthe exit program \ n");
Break Exit
}
else/Wrong input operation
{
printf ("Wrong input, please try again!") ");
Continue
}
}
}
Experiment four allocation and recovery of main memory space