Simulation of allocation and recovery of main memory space

Source: Internet
Author: User

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

Internet of Things Project 201306104105 tour poem Wei

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.

Second, experimental content and requirements

2.1 simulations include 3 section:

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

Main program sections and their explanations:

#include <stdio.h> #include <malloc.h>struct fbt{int startdress;//partition header address int size;//partition occupies memory size int processid;/ /application Memory partition process number int state;//partition state struct FBT *next;};  struct FBT *create ()//free block table {struct FBT *head,*p,*q;head=null;p= (struct FBT *) malloc (sizeof (struct FBT)); q= (struct FBT *) malloc (sizeof (struct FBT)), head=p;p->size=5;//system partition, 5p->processid=-1;//-1 indicates the fixed partition of the operating system p->startdress= 0;//start address is 0p->state=1;//status assigned, 1p->next=q;q->size=115;//remaining partition size q->processid=0;//process number is 0, unassigned q-> startdress=5;//start address is 5q->state=0;q->next=null;return head;} struct FBT *distribute (struct fbt*head)//Allocate memory sub-function {int id,needsize;struct FBT *pretail,*tail,*p;printf ("Enter the process number to request memory:") ///Enter the process number scanf ("%d", &id) of the application memory;p rintf ("Please enter the requested memory size:");//Enter the requested memory size scanf ("%d", &needsize);p Retail=tail=head; while (Tail!=null) {if (tail->state==0&&tail->size>=needsize)//If this partition is not allocated and the size is greater than or equal to the requested memory size {if ( Tail->size>needsize)//If this partition size is larger than the size to be requested, allocate, and then split the remainder into one partition {p= (struct FBT *) malloc (sizeof (strUCT FBT));p->size=needsize;p->processid=id;p->startdress=tail->startdress;p->state=1;  if (head!=tail) {pretail->next=p;  p->next=tail;  Pretail=p;  } else {p->next=tail;  Head=p;  tail->next=null;  } tail->startdress=p->startdress+p->size;  tail->size=tail->size-needsize;     break;}     if (tail->size==needsize)//If this partition is equal to the size to be requested, direct allocation can be {tail->processid=id;     tail->state=1;     Break   }}else//Otherwise, point to the next node to continue to judge {Pretail=tail; Tail=tail->next;}}  if (tail==null)//If no appropriate partition allocation is found on the linked list, the following information is displayed printf ("\ n Not enough space!\n"); return head;   struct FBT *callback (struct FBT *head)//Reclaim memory sub-function {int id;   struct FBT *pretail,*tail;   printf ("Please enter the process number:");   scanf ("%d", &id); Pretail=tail=head;while (Tail!=null)//traversal of the list {if (Tail->processid==id)//if the partition corresponding to the process number to reclaim the process number of the partition, then do the following action {if (tail- >next!=null)//If the partition is not the last partition, make the following action {if (pretail->state==1&&tail->next->state==1)// The previous partition is allocated and the latter partition is also allocated {Tail->state=0;      Break } if (pretail->state==0&&tail->next->state==1)//The previous partition is unallocated and the latter partition is also assigned {pretail->next=t      ail->next;      pretail->size=tail->size+pretail->size;      Free (tail);      Break        } if (pretail->state==1&&tail->next->state==0)//The previous partition is allocated and the latter partition is unassigned {if (pretail!=tail)          {pretail->next=tail->next;          tail->next->size=tail->next->size+tail->size;          tail->next->startdress=tail->startdress;          Free (tail);        Break        } else {head=tail->next;        tail->next->startdress=0;        tail->next->size+=tail->size;        Break }} if (pretail->state==0&&tail->next->state==0)//The previous partition and the latter partition are unassigned {Pretail->next=tail     ->next->next;     pretail->size=pretail->size+tail->size+tail->next->size;  Free (tail->next);   Free (tail);     Break    }} else//if the partition is the last partition, make the following action {if (pretail->state==1)//If the previous partition is assigned {tail->state=0;    Break    } else//If the previous partition is unassigned {pretail->next=null;    pretail->size=pretail->size+tail->size;    Free (tail);    Break }}}pretail=tail;tail=tail->next;//traverse the next partition node}return head;//return the head pointer} void print (struct FBT *head)//display FBT content to customer {Struc T FBT *tail=head; printf ("startdress\tsize\tstate\t\tprocessid\t\n"); Tail=head; while (Tail!=null) {if (tail->state==1) printf ("%5d\t\t%5d\t%5d\t\t%5d\n", Tail->startdress,tail->size,tai    L-&GT;STATE,TAIL-&GT;PROCESSID);    else printf ("%5d\t\t%5d\t%5d\n", tail->startdress,tail->size,tail->state); tail=tail->next;    }}void Main ()//main function {int choice=1;struct FBT *head;head=create (); while (choice!=0) {printf ("\t1. Request memory \ n");    printf ("\t2. Reclaim memory \ n");    printf ("\t3. Exit \ n");    printf ("Please select:");    scanf ("%d", &choice);     Switch (choice) {Case 1:   Head=distribute (head);        Print (head);    Break        Case 2:head=callback (head);        Print (head);    Break    Case 0:break; default:printf ("Input Error!    \ n "); }}}

Operation Result:

Iv. Summary

Every experiment has the actual meaning, through the operation of different experiments to grasp the different knowledge points, for the requirements of this experiment said I feel I have not reached the requirements, but in the future I will continue to redouble their efforts, in a limited time to fully play to learn with more knowledge, constantly enrich themselves, improve themselves.

Simulation of allocation and recovery of main memory space

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.