Use C language for memory management simulation experiment and C language memory management simulation

Source: Internet
Author: User

Use C language for memory management simulation experiment and C language memory management simulation

Simulate a simple fixed (variable) Partition Storage Management System

Lab content

(1) Establish relevant data structures, Job control blocks, allocated partitions, and unallocated partitions
(2) implement a partition allocation algorithm, such as the first adaptive algorithm, the best or the worst adaptive distribution algorithm.
(3) implement a partition recycling Algorithm
(4) For a job/process, select an allocation or recycling algorithm to simulate the management of partition storage.

Lab procedure

First, the initialization function initial () initializes the partition table and creates a list of idle partitions. The length of the first partition in the idle partition is 30, and the length of each partition in the future is 20 longer than that in the previous one.
Frees [0]. length = 30
The length of the second part is 20 longer than that of the first part, and that of the third part is 20 longer than that of the second part, and so on.
Frees [I]. length = frees [I-1]. length + 20;
The first address of the next free zone is the sum of the first address of the last free zone and the length of the last free zone. Frees [I]. front = frees [I-1]. front + frees [I-1]. length;
The first address and length of the allocation area are initialized to zero occupys [I]. front = 0; occupys [I]. length = 0;
The show () function is used to display the specific class capacities of the current idle Partition Table and the current allocated table. The partitions have the starting address, length, and status, and are output cyclically using the for statement. Some formats make the output more beautiful.
The assign () function uses the adaptive allocation algorithm for the first time to perform partitioning, starting from the beginning of the chain for sequential search until a free partition of sufficient size is found. Then, based on the job size, allocate a piece of memory space from the partition to the requester, and the remaining idle partitions remain in the idle chain. If you cannot find a partition that meets the requirements from the beginning to the end of the chain, the memory allocation fails and return. This algorithm tends to prioritize the use of idle partitions on the low-address part of the memory, thus retaining the large idle areas on the High-address part. Creates a condition for allocating large memory space for large jobs that will arrive in the future. Its disadvantage is that the low-address part is constantly divided, which leaves a lot of idle partitions that are difficult to use, and each search starts from the low-address part, this will undoubtedly increase the overhead of searching for available free partitions.
Allocate memory and find the required size partition from the idle partition table. Set the size of the requested partition to job_length. The size of each idle partition in the table can be expressed as free [I]. length. If frees [I]. length> = job_length, that is, the length of the idle space I is greater than or equal to the job length. The idle flag is set to 1. If this condition is not met, the output is: "Sorry, there is no idle memory that meets your request length. Please try again later! ". If frees [I]. length> = job_length the length of the free space I is not greater than the job length, add 1 to the value of I to determine whether the space in the next free zone is greater than the job length. Pay the first address of unused free space to the first address of used space. The length of used space is the length of the job, and the number of used space is increased by 1. If (frees [I]. length> job_length), the space length is greater than the job length, frees [I]. front + = job_length; start address of the idle Space = start length of the original idle interval plus job length frees [I]. length-= job_length; length of the idle interval = length of the original idle interval-job length. If the length of the space is equal to the job length, the Free Zone moves one bit forward, and the number of free zones is also reduced by one. In this way, the memory space is allocated successfully after all the situations are determined and allocated accordingly.
The second operation is to cancel the job. In this operation, the following steps are performed:
(1) follow the system prompts to enter the name of the job to be withdrawn;
(2) Determine whether the job exists
If the job name does not exist, enter the job name again ";
If yes, use flag, start, And len to save the position I, the first address, and the length of the memory space of the job in the allocation area table. Then, based on the first address of the recycle area, that is, the first address of the job, find the corresponding insertion point from the idle area table and add it to the idle table. One of the following three situations may occur:
1. the recycle zone is connected only to F1, the first idle partition before the insertion point (frees [I]. front + frees [I]. length) = start). In this case, determine whether it is adjacent to the last idle shard F2. There are two cases:
If adjacent, the three partitions are merged to modify the first address and length of the new idle partition. The new first address is the first address of F1, And the length is the sum of the three partition lengths. The corresponding code is: frees [I]. length = frees [I]. length + frees [I + 1]. length + len; and the idle partition table.
If there is no adjacent connection, that is, the first partition after the merger should be merged between the recycle zone and the previous partition of the insertion point, no new table items must be allocated to the recycle partition, you only need to modify the size of the previous partition. This is the sum of F1 and current job size. Frees [I]. length + = len;
2. The recycle partition and the idle partition before the insertion point are not adjacent but are adjacent to the F2 of the last idle partition. In this case, the current job partition and F2 partition should be merged. The first address of the merged partition is the start address of the current job, And the length is the sum of the two partition lengths. Code: frees [I]. front = start; frees [I]. length + = len;
3. The recycle partition is not connected to the first idle partition of the insertion point, nor to the last idle partition. In this case, you only need to insert the recycle partition into the idle partition table. In this case, the number of free zones is increased by 1.
After adding a recycle area to a free partition table, you must modify the content of the partition table. Specifically: Modify the pointer of each region after the recycle area (area 1) in the allocation area table to move it one by one, that is, occupys [I] = occupys [I + 1]; at the same time, the number of allocated partitions is reduced by 1, that is, occupy_quantity -;
The output memory space has been recycled! This completes the Undo job and reclaim the corresponding space.

The source code is attached.

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Const int MAXJOB = 5; typedef struct node {int front; int length; char data [20];} job; job frees [MAXJOB]; int free_quantity; job occupys [MAXJOB]; int occupy_quantity; int initial () {int I; frees [0]. front = 0; frees [0]. length = 30; occupys [0]. front = 0; occupys [0]. length = 0; strcpy (frees [0]. data, "free"); for (I = 1; I
      
        = Job_length) {flag = 1 ;}} if (flag = 0) {printf ("Sorry, there is no idle memory that meets your request length. Please try again later! \ N ") ;}else {t = 0; I = 0; while (t = 0) {if (frees [I]. length >=job_length) {t = 1;} I ++;} I --; occupys [occupy_quantity]. front = frees [I]. front; strcpy (occupys [occupy_quantity]. data, job_name); occupys [occupy_quantity]. length = job_length; occupy_quantity ++; if (frees [I]. length> job_length) {frees [I]. front + = job_length; frees [I]. length-= job_length;} else {for (j = I; j <free_quantity-1; j ++) {= "" frees [j] =" Frees [j + 1]; "} =" "free_quantity --; =" "printf (" memory space allocated successfully! \ N "); =" "void =" "show () {=" "int =" "I; =" "printf (" --------------------------- \ n "); = "" printf ("the current idle partition table is as follows: \ n"); = "" printf ("START address =" "length =" "status \ n "); = "" for (I = "0; I
       
        

Related Article

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.