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

Source: Internet
Author: User

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

2. Experimental Content

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.

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.

4. Experiment Code and
#include <stdio.h> #include <stdlib.h> #include <string.h> const int canuse = 1;const int cantuse = 0;cons  t int msize = 128;        Memory partition struct mzone{//idle zone start address int begin_addr;              Length of a contiguous idle area int length;       status int state;           In-memory task name Char task_name[32];    Point to the next free partition struct Mzone *next; }; Memory head pointer struct Mzone *mhead = NULL;      The Showmemory function, which shows the current memory allocation condition void showmemory () {struct Mzone *mpoint = mhead;     printf ("Use of memory \ n");          printf ("beginaddr\tlength\tstate\ttask\n");         while (Null!=mpoint) {printf ("%dk\t\t", mpoint->begin_addr);         printf ("%dk\t", mpoint->length);         mpoint->state?printf ("canuse\t"):p rintf ("cantuse\t");         printf ("%s\n", mpoint->task_name);     Mpoint = mpoint->next;      } system ("Pause");     The//memoallocate function is used to allocate the inner pure void memoallocate (void) {struct Mzone *mnew = (struct mzone*) malloc (sizeof (struct mzone)); printf ("Enter the memory size to allocate (KB): \ n"));     scanf ("%d", &mnew->length);     printf ("Input task Name: \ n");     scanf ("%s", &mnew->task_name); Minsert (mnew)? printf ("Allocate memory succeeded \ n"):p rintf ("There is no free partition with size, memory allocation fails.      \ n ");     System ("pause"); Free (mnew);}        Minsert function, function Insert task to free partition int minsert (struct mzone* mnew) {struct Mzone *zinsert = mhead;      int flag = 1; Flag is used to indicate that zinsert to null, neither memory can be allocated while (Zinsert->length<mnew->length | |!              Zinsert->state) {if (null!=zinsert->next) {Zinsert = zinsert->next;                 } else {Zinsert = zinsert->next;             Break     }} if (Null==zinsert) {return 0;          } if (msize = = zinsert->begin_addr+mnew->length) {zinsert->state = Cantuse;          strcpy (Zinsert->task_name, mnew->task_name);          Zinsert->next = NULL;     return 1;     } else {    struct Mzone *ztail = (struct Mzone *) malloc (sizeof (struct mzone));         Zinsert->state = Cantuse;         strcpy (Zinsert->task_name, mnew->task_name);         Zinsert->length = mnew->length;                   Zinsert->next = Ztail;         memset (ztail, 0, sizeof (char) *32);         ZTAIL-&GT;BEGIN_ADDR = zinsert->begin_addr + mnew->length;         Ztail->state = Canuse;         Ztail->length = msize-ztail->begin_addr;                   Ztail->next = NULL;     return 1;     }}//memoreturn function to reclaim memory void Memoreturn (void) {char tname[32];     printf ("Enter the task name to retract \ n");     scanf ("%s", tname);      Mreturn (Tname); System ("pause");    }//mreturn function, function reclaim memory int Mreturn (char taskname[]) {struct Mzone *front = NULL;    struct Mzone *position = mhead;         struct Mzone *tail = mhead->next;           while (0!=STRCMP (Position->task_name,taskname)) {front = position;        if (Null!=position->next) {       Position = position->next;               } else {position = NULL;           Break    } tail = position->next; } if (null==position) {printf ("This task is not in memory!")       "); } else {//cannot be used with Cantuse if (Null!=tail&&null!=front) {if (FRONT-&GT;STATE&A mp;&tail->state) {front->length = front->length + position->length + tail->                Length                Front->next = tail->next;                Free (position);            Free (tail); } else if (front->state&&!tail->state) {front->length = front->                Length + position->length;                Front->next = position->next;            Free (position); } else if (!front->state&&tail->state) {position->length = Positio N->length + tail->length;                memset (position->task_name, 0, sizeof (char) *32);                Position->next = tail->next;                Position->state = Canuse;            Free (tail); } else if (!front->state&&!tail->state) {memset (Position->task_na                Me, 0, sizeof (char) *32);               Position->state = Canuse; }} else if (Null!=tail&&null==front) {if (!tail->state) {Me             Mset (position->task_name, 0, sizeof (char) *32);          Position->state = Canuse;             } else {position->length = position->length + tail->length;             Position->next = NULL;          Free (tail); }} else if (Null==tail&&null!=front) {if (front->state) {f   Ront->length = Front->length + position->length;           Front->next = NULL;          Free (position);              } else {memset (position->task_name, 0, sizeof (char) *32);          Position->state = Canuse; }} else if (Null==tail&&null==front) {memset (position->task_name, 0, sizeof (CH            AR) *32);        Position->state = Canuse; } printf ("Memory Recycle succeeded!")   \ n ");              }} int main (void) {int func_ = 0;     Mhead = (struct mzone*) malloc (sizeof (struct mzone));//Initialize Mhead mhead->begin_addr = 0;     Mhead->length = msize;     Mhead->state = Canuse;     memset (mhead->task_name, 0, sizeof (char) *32);         Mhead->next = NULL;           while (1) {printf ("****************** first adaptation algorithm implements main memory allocation and recovery System (Msize) ***************");           printf ("| |: view memory allocations \ n");           printf (": Application for allocating memory \ n");           printf ("|3: request to reclaim memory \ n");           printf ("|4: Exit program \ n"); printf ("********************************************************************************");           scanf ("%d", &func_);                   Switch (FUNC_) {case 1:showmemory ();                   Case 2:memoallocate ();                    Case 3:memoreturn ();           Case 4:return 1;     } system ("CLS"); }       }

  

Five experimental experiences

This test is more troublesome, which encountered many problems, on the internet to find a lot of information and other people's program code to compare and reference.

The initial contact is still relatively new, but it is not very well understood in this respect, it may strengthen the study in this area.

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.