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 allocation, the design is completed by the first adaptive algorithm, the first cycle adaptive algorithm, the best adaptive algorithm and the worst-fit algorithm of 4 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
You can use Visual C + + as your 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.
4. Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE_MIN 2
#define MEMSIZE_MAX 1024
#define FALSE 0
#define TRUE! FALSE/* Use best allocation method */
typedef int BOOL;
typedef struct _MEM_LINK
{
Char CName; /* Job Name */
int istartaddr; /* Partition start Address */
int imemsize; /* Partition Size */
BOOL istate; /* Partition status, 1 for allocated, 0 for unassigned */
struct _mem_link* next;
}
Mem_link, *pmem_link;
Pmem_link G_pslnkhead; /* Initialize Memory usage */
void Init ()
{
G_pslnkhead= (Pmem_link) malloc (sizeof (Mem_link));
memset (g_pslnkhead, 0, sizeof (mem_link));
G_pslnkhead->imemsize = Memsize_max;
}
int menu ()
{
int i;
printf ("\n\n1. Allocate memory \ n ");
printf ("2. Reclaim memory \ n ");
printf ("3. Show memory usage \ n ");
printf ("4. Exit \ n ");
printf ("\ n Please enter select:");
scanf ("%d", &i);
GetChar ();
return (i);
}
/* Allocates memory functions, C is the job name, Usize is the size to allocate */
int My_malloc (char c,int usize)
{
Pmem_link Psnewmem = NULL,
Plnktmp = NULL;
BOOL brepeatname = FALSE;
int itmp = g_pslnkhead->imemsize-usize*size_min;
if (itmp <= 0)/* If there is not enough space allocated */
return FALSE;
Plnktmp = G_pslnkhead;
while (plnktmp! = NULL)
{
if (plnktmp->cname = = c)
{
Brepeatname = TRUE;
Break
}
Plnktmp = plnktmp->next;
}
if (brepeatname)/* If the job name repeats */
{
return FALSE;
}/* Create a new node */
Psnewmem = (pmem_link) malloc (sizeof (Mem_link)); /* Structure SET 0 */
memset (psnewmem, 0, sizeof (mem_link)); /* Set node Contents */
Psnewmem->cname = C;
Psnewmem->imemsize = Usize*size_min;
Psnewmem->istartaddr= memsize_max-g_pslnkhead->imemsize;
Psnewmem->istate = TRUE;
Plnktmp = G_pslnkhead; /* Find the tail node of the list */
while (plnktmp->next! = NULL)
Plnktmp = plnktmp->next; /* Add the newly created node to the list */
Plnktmp->next = Psnewmem; /* Remove the allocated portion of the overall memory */
G_pslnkhead->imemsize-= usize*size_min;
return TRUE;
}/* Reclaims the memory function, C is the job name of the revoked process;
int My_free (char c)
{
Pmem_link PLNKBK = g_pslnkhead,/* keep the last searched node */
Plnktmp = g_pslnkhead->next;
BOOL bfind = FALSE;
int ifreesize = 0; /* Search Linked list */
while (plnktmp! = NULL)
{
if (plnktmp->cname = = c)
{/* If the node is found, exit the loop */
bfind = TRUE;
Break
}
PLNKBK = plnktmp;
Plnktmp = plnktmp->next;
}
if (bfind)
{/* Remove the found node from the list and release it */
G_pslnkhead->imemsize + = plnktmp->imemsize;
Plnkbk->next = plnktmp->next;
/* Keep the size of the memory to be freed */
Ifreesize = plnktmp->imemsize; /* Release */
Free (plnktmp); /* Prevent memory fragmentation by advancing the start address of the non-freed memory */
Plnktmp = plnkbk->next;
while (plnktmp! = NULL)
{
PLNKTMP->ISTARTADDR-= ifreesize;
Plnktmp = plnktmp->next;
}
}
return bfind;
}
void disp ()
{
Pmem_link ptmp;
int i = 0;
Ptmp = G_pslnkhead;
printf ("\ n area code job name start address partition size state");
while (PTMP)
{
printf ("\n%4d%c%4d%4d%4d", I, Ptmp->cname, ptmp->istartaddr, Ptmp->imemsize, ptmp->istate);
Ptmp = ptmp->next;
i++;
}
}
void Main ()
{
int i;
char c;
Init ();
i = menu ();
while (i!=4)
{
if (i==1)
{
printf ("\ n Job name (one character):");
scanf ("%c", &c);
printf ("Job takes up memory size:");
scanf ("%d", &i);
if (My_malloc (c,i))
printf ("\ n Assignment succeeded!!! ");
else printf ("\ nthe allocation failed!!! ");
}
else if (i==2)
{
printf ("\ n Enter the job name (one character) to reclaim the partition (s):");
scanf ("%c", &c);
if (my_free) printf ("\ nthe recycle succeeded!!! ");
else printf ("\ nthe recycle failed!!! "); }
else if (i==3)
Disp ();
i = menu ();
}
}
"Operating System" experiment four allocation and recycling of main memory space deadline submitted: 2016.6.17