Allocation and recovery of main memory space in experiment four

Source: Internet
Author: User

Experiment Four allocation and recovery of main memory space

Major: Business Soft one class name: Dong Tingting No.: 201406114105

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.Reference data structure:

#include <stdio.h>

#include <conio.h>

#include <string.h>

#define MAX 24

struct partition{

Char pn[10];

int begin;

int size;

int end; ////////

char status; //////////

};

typedef struct partition part;

The First step: (Section - Week completed)

Complete the creation of the program data structure, initialize the memory allocation, create the free partition table and the allocated partition table.

Step Two: (14th Week completed)

Complete allocating memory space for a job.

    1. user input job name;
    2. Determine if the job name already exists and require the user to re-enter it if it exists;
    3. The amount of space occupied by the user in the input job;
    4. Determine if a piece of the job can be found in the remaining idle area, and if not, ask the user to re-enter it;
    5. Displays the menu, which is used by the user to choose which allocation algorithm to use:

1) First adaptation algorithm

2) cyclic first adaptation algorithm

3) Best Fit algorithm

4) worst-fit algorithm

6. Allocate memory space for the job, and the allocation process flowchart is as follows (the value of size is set to 1K):

7. The screen shows the memory partition after allocation.

Step Three: (15th Week completed)

Complete memory space reclamation;

    1. The user enters the job ID, determines the job to be terminated;
    2. Determine if there is an ID entered by the user, if there is a termination, otherwise the prompt job does not exist;
    3. Determine whether there are idle areas before and after the terminated job, and if not, the space occupied by the job becomes a free block, adding one item to the unallocated area table;

(Thinking: How can I tell if there are free blocks before and after?) )

    1. A situation in which there are free blocks before and after the space occupied by the job is terminated: (x represents the job that is about to be terminated, black represents a free block in memory)

The structure of the memory block represented in the program is as follows:

struct Partition {

Char pn[10];

int begin;

int size;

int end;

char status;

};

Therefore, the method to determine whether there is an idle block in front of a job that is about to be terminated is whether the start address of the job space a.begin equals the end address of an idle block b.end, and if it is equal, there is a free block before it, and if not equal, then determine if there are any free blocks behind it.

Answer: How to judge?

Make the judgment of four cases, and then make the corresponding block recovery operation respectively.

Answer: How do I handle recycling?

Displays the memory usage after recycling.

SOURCE program:
#include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct process{int number;    Char name[20];    int begin;    int size; char status;}    Pro;main () {int sys=1000;    char s;    int i=0;    int count1=0;    int count2=0;    int flag=1;    Char j[10];    Pro A[10];    strcpy (A[0].name, "system");    a[0].begin=0;    a[0].size=100;    A[0].status= ' F ';    printf ("Initialize, set the total memory capacity to%dk\n", SYS);    printf ("The system starts with the low address section, occupies%dk\n\n", a[0].size);    printf ("Idle area table: \ n");    printf ("\tno.0\t proname\tbegin\t size\t status\n");    printf ("\tno.1\t------\t\t%d\t%d\t f\n\n", a[0].size,sys-a[0].size);    printf ("-------------------------------------------------------\ n");    printf ("Allocated partition table: \ n");    printf ("\tno.0\t proname\tbegin\t size\t status\n");    printf ("\tno.1\t%s\t\t%d\t%d\t%c\n", a[0].name,a[0].begin,a[0].size,a[0].status);    printf ("\ n");    printf ("Memory usage, sort by initial growth: \ n");    printf ("printf sorted by address:\n"); printf ("\tno.\t proname\tbegin\t size\tStatus\n ");    printf ("\ t-----------------------------------------------\ n");    printf ("\tno.1\t%s\t\t%d\t%d\t u\n", a[0].name,a[0].begin,a[0].size);    printf ("\tno.2\t------\t\t%d\t%d\t f\n\n", a[0].size,sys-a[0].size);    printf ("\ n");    I=1;        while (1) {printf ("Please select: Assign a recycle R exit e\n");        scanf ("%s", &s);            Switch (s) {case ' a ': printf ("Please enter the process name:");            scanf ("%s", &a[i].name);            printf ("Please enter the process size:");            scanf ("%d", &a[i].size);            a[1].begin=100;            a[i].status= ' u ';   flag++;            The current number of processes printf ("Insert complete!\n");            printf ("Idle area table: \ n");            printf ("\tno.0\t proname\tbegin\t size\t status\n");                for (i=0;i<flag;i++) {a[i+1].begin=a[i].begin+a[i].size;                Count1=a[i+1].begin;            Count2=sys-count1;            } printf ("\tno.1\t------\t\t%d\t%d\t f\n\n", Count1,count2); A[0].status= ' u ';            printf ("-------------------------------------------------------\ n");            printf ("Allocated partition table: \ n");            printf ("\tno.0\t proname\tbegin\t size\t status\n");                for (i=0;i<flag;i++) {a[i+1].begin=a[i].begin+a[i].size;            printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i+1,a[i].name,a[i].begin,a[i].size,a[i].status);            } printf ("\ n");            printf ("Memory usage, sort by initial growth: \ n");            printf ("printf sorted by address:\n");            printf ("\tno.0\t proname\tbegin\t size\t status\n");            printf ("\ t-----------------------------------------------\ n"); for (i=0;i<flag;i++) {printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i+1,a[i].name,a[i].begin,a[i            ].size,a[i].status);            } printf ("\tno.%d\t------\t\t%d\t%d\t f\n\n", Flag+1,count1,count2);        Break                Case ' R ': printf ("Input process name:"); scanf ("%s", &j);                printf ("Recovery success!\n");                        for (i=0;i<flag;i++) {if (strcmp (j,a[i].name) ==0) {                        strcpy (A[i].name, "------");                    A[i].status= ' F ';                }} printf ("Idle area table: \ n");                printf ("\tno.0\t proname\tbegin\t size\t status\n"); for (i=0;i<flag;i++) {if (a[i].status== ' F ') {printf ("\tno.%d\                T%s\t\t%d\t%d\t%c\n ", i,a[i].name,a[i].begin,a[i].size,a[i].status);}                } printf ("\tno.%d\t------\t\t%d\t%d\t f\n\n", Flag-1,count1,count2);                printf ("-------------------------------------------------------\ n");                printf ("Allocated partition table: \ n");                printf ("\tno.0\t proname\tbegin\t size\t status\n");   for (i=0;i<flag;i++) {if (a[i].status== ' u ') {                     printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i,a[i].name,a[i].begin,a[i].size,a[i].status);}                } printf ("\ n");                printf ("Memory usage, sort by initial growth: \ n");                printf ("printf sorted by address:\n");                printf ("\tno.0\t proname\tbegin\t size\t status\n");                printf ("\ t-----------------------------------------------\ n"); for (i=0;i<flag;i++) {printf ("\tno.%d\t%s\t\t%d\t%d\t%c\n", i+1,a[i].name,a[i].be                Gin,a[i].size,a[i].status);                } printf ("\tno.%d\t------\t\t%d\t%d\t f\n\n", Flag+1,count1,count2);         Break            Case ' E ': Exit (0);        Break }    }}
Operation Result:

5.Experimental summary

Through this experiment to the main memory space allocation and recovery has a deeper understanding, the key is to find a large block, how to divide the chunk into two pieces, one for distribution, the other is still unallocated. Exercise our ability to deal with the problem and the programming language to implement the principle of the algorithm, the ability to better exercise our programming ability.

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.