"Operating System" experiment four allocation and recycling of main memory space

Source: Internet
Author: User
Tags clear screen

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.

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 release sequence after the completion of the job, realize 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.

Ii. contents of the experiment

Write and debug a simulated memory allocation and recycling program, using the first adaptive algorithm, loop first adaptation algorithm to the memory space allocation and recovery.

Test methods, steps and Results

1. source program name:a.cpp

executable program Name: A.exe

2. Principle Analysis

(1) Write the program first to give a certain amount of space memory, that is, to request the maximum space, and to define the space of each partition of the job label, partition start address, partition length, in bytes, the partition table state bit, forward pointer, back pointer, allocated partition table, idle partition, etc.

(2) After defining the spatial partition, we also define the spatial partition list and initialize it, make a list access to the free partition and the allocated partition, and for the free partition can be allocated to the new incoming process, for the allocated partition, and so on after the process execution end in Reclaim space, recover the idle area. The allocation and recycling of the entire spatial partition is achieved through the access of the linked list.

3. Main procedural paragraphs and their explanations:

#include <stdio.h> #include <stdlib.h> #include <string.h> const int canuse = 1;   Unassigned State const int cantuse = 0;   Null-table-state const int SIZE = 128;    Initialize memory//memory partition struct att{int begin_addr;      Idle area start address int length;   The length of a contiguous idle area int state;    Status Char Task_name[32];    In-memory task name struct ATT *next; Point to the next free partition}; Memory head pointer struct ATT *head = NULL;           Show current memory allocation void Showsave () {struct Att *mpoint = head;     printf ("Use of memory \ n");            printf ("begin_addr\tlength\tstate\ttask_name\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");     Output Press any key to exit}//Insert task to free partition int Ainsert (struct att* anew) {struct Att *zinsert = head; Flag is used to indicate that the ZinsertTo null, neither memory can be assigned int flag = 1; while (Zinsert->length<anew->length | |!              Zinsert->state) {if (null!=zinsert->next) {Zinsert = zinsert->next;                 } else {Zinsert = zinsert->next;             Break     }} if (Null==zinsert) {return 0;          if (SIZE = = zinsert->begin_addr+anew->length)//Allocate the last chunk of memory {zinsert->state = Cantuse;          strcpy (Zinsert->task_name, anew->task_name);          Zinsert->next = NULL;     return 1;         } else {struct att *ztail = (struct att *) malloc (sizeof (struct att));         Zinsert->state = Cantuse;         strcpy (Zinsert->task_name, anew->task_name);         Zinsert->length = anew->length;                   Zinsert->next = Ztail;         memset (ztail, 0, sizeof (char) *32); Ztail-&GT;BEGIN_ADDR = zinsert->begin_addr + anew->length;         Ztail->state = Canuse;         Ztail->length = size-ztail->begin_addr;                   Ztail->next = NULL;     return 1;     }}//Allocate memory void memoallocate (void) {struct att *anew = (struct att*) malloc (sizeof (struct Att));     printf ("Enter the memory size to allocate (KB): \ n");     scanf ("%d", &anew->length);     printf ("Input task Name: \ n");     scanf ("%s", &anew->task_name); Ainsert (anew)? printf ("Allocate memory succeeded \ n"):p rintf ("There is no free partition with size, memory allocation fails.      \ n ");     System ("pause"); Free (anew);}    Reclaim memory function int Mreturn (char taskname[]) {struct ATT *front = NULL;    struct Att *position = head;         struct ATT *tail = head->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 {if (Null!=tail&&null!=front) {if (front->state&&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 "); } return 0;}     Reclaim memory void Memoreturn (void) {char tname[32];     printf ("Enter the task name to retract \ n");     scanf ("%s", tname);      Mreturn (Tname); System ("pause");           } int main (void) {int f = 0;     Initialize head head = (struct att*) malloc (sizeof (struct Att));     head->begin_addr = 0;     Head->length = SIZE;     Head->state = Canuse;     memset (head->task_name, 0, sizeof (char) *32);           Head->next = NULL;           while (1) {printf ("-----------main memory allocation and recovery system (first adaptation algorithm)------------");           printf ("\n1: view memory allocations \ n");           printf ("2: Application for allocating memory \ n");           printf ("3: request to reclaim memory \ n");           printf ("4: Exit program \ n");           printf ("\ n");           scanf ("%d", &f);       Switch (f) {        Case 1:showsave ();               Case 2:memoallocate ();                Case 3:memoreturn ();           Case 4:return 1;    } system ("CLS"); Clear screen Operation}}

4. Run Results

Four , Experimental Summary

In this experiment, I learned a detailed understanding of the allocation of each memory, I used the list to simulate the storage and elimination of each memory, but currently only completed an algorithm, planning to use the rest of the semester to learn the rest of the algorithm, to realize more memory allocation method

"Operating System" experiment four allocation and recycling 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.