Memory management of the operating system

Source: Internet
Author: User
Tags compact

hierarchical structure of memory

The level of memory is as follows:

, registers and primary memory are called executable memory. The role of caching is to mitigate the difference in speed between CPU and memory, which is primarily hardware-implemented. The disk cache appears due to insufficient memory capacity, the need to introduce disk, but the disk I/O speed is much lower than the main memory access speed, in order to mitigate the difference in speed between the two, set the disk cache. Unlike cache, the disk cache itself is not the actual memory, it is the use of some space in main memory to temporarily hold the read-write information from the disk.

Loading and linking of programs

The user program needs to be run, it must be loaded into memory before it is turned into a program that can be executed, usually going through a few steps:

    • Compiling: Compiling the user source program by the compiler, forming several target modules.
    • Link: A linked program links the compiled set of target modules and the library functions they need to form a complete loading module.
    • Mount: Loads the module into memory by the loader. Specific processes such as:
1. Loading of the program 1) Address space
    • The program compiles without loading the main memory, and is not sure of its actual location in main memory, so it starts at 0.
    • Addresses that begin with a 0-bit position are called logical addresses, also known as relative addresses. The address space refers to a collection of logical addresses.
2) Storage space
    • The actual location of a program in main memory is called the physical address. The collection of physical addresses is the storage space.

There are three ways to load a program:

1) Absolute loading mode
    • At compile time, if you know where the program will reside in memory, the compiler will generate the target code for the absolute address. The absolute loader loads the program and data into memory according to the address in the loading module.
    • After the loading module is loaded into memory, the address of the program and data is not modified because the logical address in the program is exactly the same as the address in the actual memory.
2) relocatable loading mode
    • The address transformation is completed once in the load, and is no longer changed (static relocation).
3) Load mode for dynamic runtime
    • When the loaded module is loaded into memory, the relative address in the loaded module is not immediately converted to an absolute address, but the address translation is deferred until the program actually executes.
    • Therefore, all addresses that are loaded into memory are still relative addresses.
continuous allocation of storage management methods 1) Single continuous distribution
    • Divides the memory into the system area and the user area two parts, the system area provides to the OS to use, usually puts in the low address part, but in the user area, only then has one user program, namely the entire memory user space by the program exclusive.
    • Can only be used in single-user, single-tasking operating systems.
2) fixed partition allocation
    • Divides the user space of the memory into several fixed-size areas, with only one job in each partition.
    • The simplest way to run a multi-channel storage management program.
3) Dynamic partition allocation
    • The size of the partition is not delineated beforehand, and the entire process is dynamically allocated a contiguous memory space based on the size of the process.
(1) Dynamic partitioning algorithm based on sequential search
    • First-time Adaptive Algorithm (FF):
      • free partitions are linked in ascending order of address .
      • Find the first partition that satisfies the size from the top of the chain.
      • Draw out the required memory, leaving the rest in the idle chain.
      • Advantage: The large idle area of the high-address section is retained.
      • Cons: The low-address section leaves a lot of debris.
    • Cyclic first-time adaptation algorithm (NF):
      • Start with the next partition of the last found free partition until the first partition that satisfies the size is found.
      • Draw out the required memory, leaving the rest in the idle chain.
      • Advantages: The distribution of the free partition is more uniform.
      • Cons: Lack of large free partitions.
    • Best Fit Algorithm (BF):
      • the free partition is sorted by capacity from small to large .
      • Allocate the smallest free partition that satisfies the requirement as a job.
      • Advantages: Can avoid "overqualified".
      • Cons: It leaves a lot of debris.
    • Worst-fit Algorithm (WF)
      • sort the free partitions by their size from the largest to the small .
      • Look for as long as you see whether the first partition meets the job requirements.
      • Advantage: The rest of the spare area is not too small.
      • Disadvantage: There is a lack of large free partitions in the memory.
(2) dynamic partitioning algorithm based on index search
  • Fast Adaptation algorithm:
    • , also known as categorical search, classifies free partitions according to capacity size, and sets up a separate list of idle partitions for each class of free partitions with the same capacity. The
    • sets up an administrative index table in memory, each of which corresponds to an idle partition type and logs pointers to the table headers of the free partitioned list. The classification of the
    • free partition is divided by the amount of space commonly used by the process, such as 2 KB, 4 KB, 8 KB, and so on, for other partitions, such as 7 KB, that can be placed in a linked list of 8 KB or in a special list of idle areas.
    • Advantage: The lookup is highly efficient and does not produce fragmentation.
    • Disadvantage: partition return main memory algorithm is complex, the system overhead.
  • Partner system:
    • Linux uses the partner (Buddy) algorithm for managing memory free space.
    • The buddy algorithm divides all the page frames in memory by 2n, where n=0~9. Divide the memory space by 1 page frames, 2 page frames, 4 page frames, 8 page frames, 16 page frames, 32 page frames, ..., 512 page frames. After partitioning, a storage block of varying size is formed, called a page frame block, or a page block. A page block that contains 1 page frames is called a 1-page block that contains 2 page frames, called 2-page blocks, and so on. Linux divides physical memory into 1, 2, 4, 8 、...、 5,126 blocks of pages.
    • Assume that the allocated block has a size of 128 pages (a block of multiple pages is called a block of pages). This algorithm is first found in a linked list with a block size of 128 pages to see if there is such a free block. If so, assign directly; if not, the algorithm looks for the next larger block, specifically, to find a free block in a linked list with a block size of 256 pages. If such a free block exists, the kernel divides the 256 pages into two equal parts, one for distribution, and the other to a list of 128 pages in the block size. If no free page block is found in the linked list with a block size of 256 pages, continue looking for a larger block, which is a block of 512 pages. If such a block exists, the kernel will separate 128 pages from the 512-page block to satisfy the request, and then remove 256 pages from 384 pages into a linked list with a block size of 256 pages. Then insert the remaining 128 pages into a linked list with a block size of 128 pages. If there are no free blocks in the list of 512 pages, the algorithm discards the allocation and emits an error signal.
    • The two blocks that meet the following criteria are called partners:
      • Two blocks of the same size
      • Two blocks of physical address continuous
  • Hashing algorithm:
    • The hashing algorithm constructs a hash table with the free partition size as a keyword, and each table entry in the table records a corresponding free partition list header pointer.
    • When the allocation of free partition, according to the desired free partition size, through the hash function calculation, that is, the location in the hash table, the corresponding free partition list, to achieve the best allocation strategy.
4) Dynamic relocatable partition allocation
    • Reposition: Converts a logical address to a physical address.
    • Static relocation: Before the program is loaded into memory, it is known that it will be loaded into the start address of the memory, so that the address can be converted beforehand to convert the relative address to an absolute address.
    • Dynamic relocation: All addresses are still relative addresses after the job is loaded into memory, and the process of converting a relative address to an absolute address is deferred until the program instruction is actually executed.
(1) Compact
    • "Compact" to make the program move, just replace the original start address with the new start address.
(2) Dynamic relocation
    • The relocation register, which is used to store the start address of the program data in memory.
    • After the system has a compact memory, it does not need to make any changes to the program, just use the new start address of the program in-store to replace the original start address.
    • Dynamic relocation is as follows:

(2) algorithm for dynamic relocation of partitions
    • On the basis of dynamic partition allocation algorithm, the "compact" function is added, the flowchart is as follows:

Discrete allocation memory management method

Continuous distribution forms Many "fragments", although many fragments can be connected into usable chunks of space through compactness, but the cost of compactness is too high. By loading a process directly into many nonadjacent partitions, you can make the most of your memory space.

1) How to manage the paging storage

Divides the address space of a user program into several fixed-size areas, called pages or pages. The size of the specific page has the system specification, corresponding, also divides the memory space into several physical blocks or the page box, the page and the block size is the same. This allows any page of the user program to be placed in any physical block for discrete allocation.

    • Page. When allocating memory for a process, several pages in a process are loaded in blocks into multiple physical blocks that can not be contiguous. Because the last page of the process often does not have a piece of it, it forms an unusable fragment called "in-page fragmentation."
    • Logical address structure such as:
    • Page table. Usually stored in memory, the function of the page table is to implement the address mapping of the page number to the physical block number.
(1) Basic address change mechanism
    • A page table register is set up in the system, which holds the page table at the start address of the memory and the length of the page table. When the process is executed, the start address of the page table and the length of the page table are stored in the process's PCB. These two data are loaded into the page table register when the scheduler is dispatched to a process.
    • Requires access to two memory, a physical block number at a time to obtain the physical address, the second time to obtain the required data based on the physical address.
    • The address transformation mechanism is as follows:
(2) An address transformation mechanism with a fast table
    • Every time to access two times the memory speed is too slow, in order to improve the speed of the address change, add a high-speed buffer register, and applause table.
    • After the CPU gives a valid address, the address changer automatically feeds the page number p into the buffer register and compares the page number to all the page numbers in the fast table.
    • The transformation mechanism is as follows:
2) Segmented storage Management method
    • Paging is stored to improve memory utilization.
    • Segmented storage is designed to meet the needs of users in many aspects of programming and use. Easy programming, information sharing, information protection, dynamic growth, dynamic linking.
    • Fragmentation: Each segment defines a set of logical information.
    • A segmented address has the following logical structure:
(1) Address Translation Agency
    • Address transformation Mechanism:

    • To access two times of memory.

(2) main differences between pagination and segmentation
    • A page is a physical unit of information, and a segment is a logical unit of information. Paging is to reduce the amount of memory, improve the utilization of memory, is the need for system management. The purpose of segmentation is to better meet the needs of users.
    • The size of the page is fixed and determined by the system, and the length of the segment is not fixed, which is determined by the user's written program.
    • The job address space for paging is one-dimensional, while the segment's job address space is two-dimensional (segment number + segment base).
3) section page Storage Management method
    • is the combination of segmentation and paging: The user program is divided into several segments, and then divided into a number of pages.
    • The job logical address structure is as follows:

    • Address transformation Process:

    • Requires access to three memory times.

Memory management of the operating system

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.