My review of the operating system - Memory Management

  Part blog describes the knowledge processor scheduling - my review of the operating system - processor scheduling herein, this began to talk about dealing with the largest processor of computer components - memory. Often said memory includes memory and external memory. Memory management, memory management generally refers to. External memory also belongs to memory, but should be counted as document management.

First, the memory hierarchy classification

  According to the storage memory can be divided into three levels, namely, registers, main memory, secondary memory. Register is located within the CPU, main memory, also known as memory, i.e., secondary storage hard disk. Carefully divided, it can also be divided into main memory cache, main memory, disk caching. As shown below, the higher up the hierarchy, storage medium access faster, more expensive price, storage capacity is relatively more expensive. Besides the main memory and register here to talk about, secondary memory (external memory) to stay to the file system of the time.

  

  (1) Register

  Register is located within the CPU, the CPU is an integral part. It is the computer system CPU to access the fastest storage unit, fully coordination with the CPU. But the price is too expensive, can only be made very small. Registers are used to store the most frequently accessed data, e.g., an instruction register for storing an instruction read from memory is being executed, the program counter where the storage address of the next instruction unit. Its essence is used to store data for a number of the most frequently visited CPU. Register is to solve the CPU to access main memory speed is too slow. Typically, the CPU reads the stored data from the master, into the register, for frequently accessed.

  (2) main memory

  I.e., main memory memory. CPU can be directly accessed by an instruction data in the main memory, the CPU main memory for fast access speed, but this speed is much lower than the execution speed of the CPU. To solve this problem, the introduction of registers and caches. What caches are? Cache memory also belongs, but it is usually implemented in the form of different main memory, it is generally a static memory chips (SRAM) composition, much higher than the access speed of main memory, the CPU is close to the speed. And the main memory is generally used MOS dynamic random access memory consisting of DRAM, much faster than SRAM. The role of the cache is stored in the main memory of some frequently accessed information. Nature of disk cache is a main memory divided small regions, in order to reduce the number of CPU machine read by the disk I / O, to enhance the efficiency of disk I / O, and a region with more frequent access for storing disk content.

 

Second, the program is loaded and links

  Is the program loader programs and data into memory. Program nor is it a start, there's. Herein refers to the final program is a module running in memory - load module. Then the source code is how to become a program that can run it? Learned C, C ++ most of the students of this understanding. First, the source code is compiled into compiled object modules, each one corresponding to a source file object module. The program will then link object modules and procedures needed for library functions are linked into a program that can be run. This can run the program, in essence compiled links to machine instructions, CPU can run the machine instructions. Runtime, load it into the memory module and run. Wherein the machine instructions will repeat itself into memory resources directed three ways:

  (1) Load:

    1) absolute load mode (Absolute Loading Mode)

  Addresses used in the program is absolute addresses directly to memory, then the program into memory when the program does not require any changes to the address, this is called absolute load into the way the way. Absolute load only way the program is loaded into memory location specified, it is only suitable for single-channel processing environment, so there is not a memory conflict.

    2) loaded relocatable manner (Relocation Loading Mode)

  Relocatable loaded manner means that when the program will be loaded into memory, the program memory address of the current address are offset with respect to. Then the program addresses are relative addresses. It is worth noting that the modification instructions and data during program called a relocation address is loaded.

    3) dynamic fashion into the operating mode (Dynamic Run-time Loading)

  If the program needs to be changed at run-time position, you should run fashion into the dynamic mode. Dynamic run fashion into the way refers to the relative address is not on the program is loaded into memory at the time of conversion of the absolute address, but will not run until the real conversion.

  (2) link:

  And program load corresponding link is how the program. The program links and there are also three ways, namely dynamic link static linking, dynamic linking is loaded and running. 3 correspond to the time when the program is linked. Which is statically linked program object modules before loading on the link is good, but when loaded dynamic link, by definition, an object module when it is loaded into memory dynamically linked, in this way linked program object modules are separated stored, if the target module requires a link to a number of other modules is very convenient. In the statically linked in a way to achieve this, we need a number of other modules contain a copy of the module.

 

Third, memory allocation - continuous distribution

  The memory allocated to the program, the most typical way is a contiguous memory space allocated to the program, which is a continuous distribution. This distribution can be divided into a single continuous segment allocation, a fixed partition assignment, dynamic partition allocation and relocation dynamic partition allocation. To be understood that both the foregoing program into memory process is a typical memory allocation. That is, memory allocation may often be dynamic, the program is running, usually accompanied by a dynamic memory creation and garbage collection, which also involves a lot of cache optimization strategies and the like. During the various memory allocation and recovery, it will produce a lot of free fragments. Memory allocation is to take advantage of the memory space as much as possible, avoid wasted memory.

  (1) a single continuous distribution

  This assignment is simply divided into the system area and a user memory area, a system area to the operating systems, a user with a user area. This allocation is very simple, does not consider the case of multi-user and multi-tasking memory conflicts memory conflicts, it only applies to single-user, single-task OS. It is worth noting that the system area is usually assigned in low-access part of the memory.

  (2) fixing the partition assignment

  This memory allocation is divided into several fixed-size area, size of the area is a good pre-divided, each region load a job program, so that the problem of multitasking memory conflict will be solved. This classification method is suitable for multi-batch system - multi-task concurrent. However, due to the waste of each partition size is fixed, the storage space is inevitable.

  (3) dynamic partition allocation

  This allocation is based on the actual needs of the process, dynamic allocation of memory space. This distribution has three problems that need attention. 1, there is a need to describe the data structure allocated free partitions and partitions case. 2, needs to be allocated according to a certain selection of free partitions of the allocation algorithm. 3, the partition needs to have an appropriate memory allocation and recovery operations:

    1) the data structure describing the idle partition:

    There are two data structures may describe the data structure of the free partition, the partition table are idle and free partition chain. Wherein the partition table is easily understood by referring to the partition chain set two pointers point to the other free end to the idle partition of the partition, free partition forming a chain, used to record the partition idle.

    2) partition allocation algorithms:

    • First fit algorithm (first fit): Partition chain to address increasing order link; when allocating memory, starting from the chain first, find a size to meet the requirements of the free partition stops. This algorithm Plainly the low part of the address on the first memory allocation, redistribution high address section.
    • Cycle first fit algorithm (next fit): the difference between the first-fit allocation algorithm and the algorithm is that when it allocates memory from the chain not start looking first, but start looking for a partition from the free partition previous finds.
    • Best-fit algorithm (best fit): Partition chains ascending order link; when allocating memory, starting from the chain first, find one that meets the requirements of the free partition stops.
    • The worst adaptation algorithm (worst fit): Chain partitions connected in descending order; contrary to best fit algorithm, to pick each time the maximum allocated free area.
    • Fast adaptation algorithm (quick fit): the free zone are classified according to the size of each category to establish a separate list. Meanwhile, the index table with a management to manage these lists. So when allocating memory only need to query the index management table on the line, you do not need to traverse the list, very fast. The disadvantage is that this algorithm has been required to maintain the index table lists and management, requires a certain overhead.

    3) memory allocation and recovery:

    When dispensing free partition, it is worth noting that there is usually a free partition "is not subdivided remaining partition" attribute specifies, when the remaining free partition is less than its properties, the partition must no longer be divided , the partition will also benefit from the free list sub partition is removed.

    Memory recovery time, it is worth noting that, if the recovery of the memory area adjacent to a free partition, you need to merge them. Otherwise, you need to create a new empty partition for recovery area. 

    4) The Buddy System:

    We know that 1G of memory is 2 20 bytes, there are two 24- word. So according to the index, divided into up to 24 free partition list. Consider an application program application 2MB RAM, 2MB that is, 2 15 the size of the word, this time to find a size of 2 15 free partition list, if found, then look for size 2 16 free partition the list, when the 2- 16 's free partition list exists, then it is divided into two, a is assigned to request allocation of another 2 15 free partition of the list, Ruoruo 2 16 free partitions list does not exist, then continue to the next search, and so on.

  (4) the partition assignment relocatable

    Because there will be a lot of debris between programs, resources, a waste of memory space, relocatable partition assignment is to solve this problem, it can be moved in memory, resource directly, the memory becomes compact, while not affecting the normal procedure run. Relocatable partition assignment request procedure is loaded manner into the dynamic operating mode fashion. After the program into memory, all addresses still is a relative address, it will be transformed into an absolute address until runtime. Program has a relocation register to hold the address of the actual address of the first program in the hard disk in the register. The program then absolute address in memory of the mobile, the mobile need only change the value to a relocation register. That we often use "Disk defragmentation" is the same effect.

  (5) the swapping

    Swap is a need to look at the concept. Remember earlier when we talk about the process of scheduling, there is a special type of scheduling, called mid-level scheduling. Intermediate scheduling is to let the process run suspend temporarily unable to free memory resources, and moved them to the external memory up to wait for this operation, it seems in memory, called the swap. Process-units called swapping swapping process. The case of exchange, the external memory must be allocated a certain area of ​​memory used to store resources on the exchange, called the swap area. The essence of swap space is the virtual memory, this will speak later.

 

Fourth, memory allocation - Discrete distribution

  Continuous distribution will have a lot of debris. Discrete distribution is the process, not the resources into the adjacent memory allocation multiple partitions. This allocated according to the allocation unit is "page" or "segment", is divided into page memory management, storage management, and the segment storage management page segment.

 (1) paged memory management

  Paged memory management program is based on the work of the "page" for the management to allocate memory discrete units.

  1) Page (page).

  Memory allocation unit is paged memory management page. What is a page? Page is a specified memory block size. Paged memory management is in accordance with a certain size to the logical address space of a process is divided into several parts, each one is a page, put their number. Then according to the size of the page memory is divided into several physical blocks, and numbered. The page size is usually between 512B to 8KB.

  2) page table.

  Each process has a page table, the page number corresponding to the physical block numbers for recording process. When the process is running, CPU execution find the actual physical block address and the actual physical address from the page table according to a logical page number and size of the program. Page table is frequently accessed CPU, CPU is often need to access memory page table and then access the address of the page table, it is usually set up a "Legend register", also known as "block list", page tables to store frequently accessed recently. If the memory system is particularly large, the logical page address in the page table will be particularly large, it is necessary with a multilayer structure of page tables to corresponding physical block number. In this case, CPU execution find the specified page table page table from the outside of the multilayer according to the logical address and page size of the program, to find the actual physical address of the physical block and the page table.

(2) Storage Management segment

  Segment storage management program according to the job in the "segment" is a discrete memory allocation management unit.

  1) segment.

  Section refers to a logical set of program information, job. For example: the global variables may be set to a segment; local variable may be set to a function of each section; code section for each function may be provided to a segment. What significance does this have to do? This corresponds to the logic information of the program in accordance with the size of a discrete stored in memory, and for the logical information itself, they are contiguous in memory, not divided, it is a good processing logic information, such as information sharing, information protection.

  2) segment table.

  Similar to the page table, each process has a segment table to record the program corresponding to the physical location of each segment. Each segment table records the physical address and records the length of segment segment. Also, since the segment table needs to be accessed frequently, some systems will be placed in the segment table register.

  (PS: It is worth noting that the run-time dynamic linking memory requirements using segmented memory management.)

(3) paragraph paged memory management

  Section on page storage management is based on the "segment" as a unit, then the "segment" is subdivided into "pages", use this as a discrete unit to allocate memory management. Similar principles and paging, segmentation storage management.  

 

Fifth, the virtual memory management

   For a continuous distribution of memory, there is a "swap" concept, is to temporarily unused memory resources removed from memory, external memory into the swap area to above. When the required memory resources, the need for timely able to put the resources into memory from a memory external memory. In fact, here's swap space is the virtual memory. Virtual memory talked about the need to look at the principle of locality of program execution, conclude that:

  • During the execution of the program, most of the instruction is executed once or rarely executed, CPU mainly in the implementation of a small number of instructions.
  • During the execution of the program, most of the resources are seldom accessed.

  So, a one-time program into memory, but in fact most of the memory resources are being wasted. Based on this situation, no need to have all the resources one-time into memory. Only need to run the program currently required in paragraph (p) can be loaded into memory. Access to the segment if the program is running in memory that does not exist (p), a phenomenon called "short period" (but page), this time in need of immediate resources into memory according to a certain algorithm from a virtual storage area of ​​the external memory will be deleted .

  There is a supplement knowledge, see http://zhidao.baidu.com/question/86215203.html:

   As for the page table is such that, when the system is initialized, direct physical memory access, and not through the page table, which is the operating mode called real mode, etc. page table is set up in the memory, then switch the Protected mode, it appeared in protected mode virtual address translation process to a physical address. 

  The CPU has two operating modes, a real-mode, direct access to physical memory is not paged. Another is protected mode, that is, paging, and there is a virtual address. Privileged user mode and protected mode there are two modes. Relationship is like this.

  I tell you, as long as the missing page interrupt, will fall into the kernel, but entered the privileged mode, control to the operating system, hardware series of processes are completed. As for the software to complete feed, the operating system is responsible for paging. MMU is only responsible for the virtual address into a physical address, he can only do this, pure hardware implementation. Operating system paging algorithm is to find out in a free page, the content needs to read out from the disk into memory, and then let the process re-run that instruction. Everything continues as if no missing pages as before. If not free, do not put the one most frequently used to replace.

 

 Reference: "Computer Operating System (Tang Ziying)"

 

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/4195976.html

Guess you like

Origin blog.csdn.net/weixin_33753845/article/details/93248566