memory management

The task of the OS system memory management is to dynamically implement the management of the user area, so as to load as many processes as possible into the memory and increase the utilization of the memory. The goal of memory management is to provide users with convenient, safe and sufficiently large storage space.

Storage management should have the following functions:

(1) Distribution and recycling. The allocation and management of memory space are completed by the OS, so that programmers can get rid of the trouble of memory space allocation and improve programming efficiency. For this purpose, the system should remember the memory space usage, implement memory allocation, and reclaim the space freed by the system or application.

(2) Abstraction and mapping. Main memory is abstracted so that a process thinks it is allocated address space. In a multiprogramming system, it is impossible for logical addresses and physical addresses in the address space to be identical, so storage management must convert physical addresses to virtual addresses.

(3) Expansion of memory. With the help of overlay technology and virtual storage technology, it provides users with a larger address space than the memory space, so as to achieve the purpose of expanding the memory capacity logically.

(4) Protection of storage. Each job that protects the memory runs in its own memory space without interfering with each other. It is necessary to prevent one operation from destroying other operations due to errors, and also to prevent system programs from being destroyed. This protection is generally completed by hardware and software.

Basic Concepts of Storage Management

program loading

In a multiprogramming environment, a program cannot run until a process is first created for that program. To create a process, you first need to load the program and data into memory. Turning a user source program into a program that can be executed in memory usually requires the following steps:

(1) Compile. The user source code is compiled into several object modules by the compiler.

(2) LINKS. The compiled object modules and their required function libraries are linked together by the linker to form a load module.

(3) Loading. Load modules are loaded into memory by the loader.

In order to improve utilization, a dynamic loading method is introduced, and the above steps are often intertwined.

1. Absolute loading method.

If it is known at compile time where the program resides in memory, the compiled program will produce object code with absolute addresses. Absolute loader loads program and data into memory according to the address of the load module. After the load module is loaded into the memory, since the logical address in the program is the same as the actual memory address, there is no need to modify the program and data.

The characteristics of this loading method are:

(1) Knowing the location of the program in memory, so the absolute address is generated when compiling.

(2) After the module is loaded, since the logical address and the actual address in the program are exactly the same, there is no need to modify the program and data.

(3) Only the target module can be loaded into the pre-specified location in the memory, which is only applicable to the single-channel program environment.

(4) Usually symbolic addresses are used in the program, and then these symbolic addresses are converted to absolute addresses during assembly and compilation.

2. Relocatable loading method.

After an application program is compiled, several object module programs are usually formed, and these modules are then linked to form a loadable and executable program. The addresses of these programs are all addressed starting from "0", and other addresses in the program are calculated relative to this address; the address range formed by these addresses is called the address space, and the addresses in it are called logical addresses. Logical addresses are also called virtual addresses and relative addresses. Storage space, also known as memory space, refers to a collection of physical units in memory that store information, called physical addresses, also known as absolute addresses and real addresses. In order to ensure the correctness of the program, it is necessary to transform the addresses in the program. This process of converting a logical address to a physical address is also called address relocation, also called address mapping or address transformation. This kind of address conversion is only completed once when loading, and will not be changed later, so it is called static relocation. The characteristic of static relocation is that when a job is loaded into memory, all the memory space it requires must be allocated. If there is not enough memory space free, it cannot be loaded into memory. Once a job is loaded into memory, it cannot be moved in memory, nor can it apply for memory space.

Features of Relocatable Loading

(1) The logical address loaded into the module is generally different from the physical address actually loaded into the memory.

(2) The physical address actually loaded into the memory = logical address + program start address.

3. Dynamic runtime loading method

When the program is loaded dynamically at runtime, after the load module is loaded into the memory, the logical address of the load module is not converted into a physical address immediately, but the address conversion is postponed until the program is actually executed. Therefore, all addresses after being loaded into memory are still logical addresses. In order not to affect the execution speed of the instruction, this method requires special hardware support, which is called dynamic relocation. Dynamic relocation refers to converting the logical address of the instruction or data to be accessed into a physical address whenever an instruction or data is accessed during program execution. Since the relocation process is completed with the step-by-step execution of instructions during program execution, it is called dynamic relocation. Dynamic relocation needs to rely on the hardware address translation mechanism, and the last simple method is to use a relocation register. When a job starts executing, the OS is responsible for sending the starting address of the job in the memory into the relocation register. After that, during the entire execution process of the job, whenever the memory is accessed, the system automatically relocates the contents of the register. Add to the logical address to obtain the physical address corresponding to the logical address. The characteristic of dynamic relocation is that the program can be allocated to discontinuous storage areas, and only part of its code can be loaded before the program is run, and then the memory space can be dynamically allocated according to the needs during the running of the program; it is convenient for the program to run. The sharing of segments can provide users with an address space that is much larger than the memory storage space.

program link

There are three ways to achieve linking: static linking, load-time dynamic linking and runtime dynamic linking.

(1) Static link method

After compiling and forming the target module, its starting address is "0", and the address of each module is relative to 0. When linking object modules into a load module, some modules no longer start at address 0 and are therefore modified. A complete load module formed by this first linking is called an executable file. Static linking is performed when an executable file is generated. After the executable file is formed, it is usually not disassembled, and it can be directly loaded into memory when it is to be run. This method of connecting in advance and not disassembling it later is called static linking.

(2) Dynamic linking at load time

Using dynamic linking when loading, the target module obtained after compiling the user source program is linked when loading into the memory. That is, when loading an object module, if an external module call occurs, it will cause the loader to find the corresponding external module, load it into the memory, and modify the relative address in the object module.

The advantages of dynamic linking when loading are: (1) It is convenient to modify and update the software version. (2) It is convenient to realize the sharing of target modules.

3. Runtime dynamic linking

During the execution process, when an object module is found not yet loaded into the memory, the OS will find the module, load it into the memory, and link it to the caller module. This linking method is called runtime dynamic linking .

Coupling: Also known as inter-block connection. Refers to a measure of the degree of interconnection between modules in a software system structure. The closer the connection between the modules, the stronger the coupling, and the worse the independence of the modules. The level of coupling between modules depends on the complexity of the interface between modules, the way of calling and the information passed. Cohesion: Also known as intra-block linkage. Refers to a measure of the functional strength of a module, that is, a measure of the closeness of each element within a module combined with each other. If the elements in a module (between language names and between program segments) are more closely related, the higher its cohesion is.
memory protection.

In a multiprogramming environment, the OS must provide a storage protection mechanism. The storage protection mechanism is to prevent a process from destroying a user process or a system process intentionally or unintentionally. Common storage protection methods are:

(1) Boundary protection (boundary memory). The purpose of storage protection is achieved by setting a pair of bounded memories for each process to prevent out-of-bounds access. There are two ways to realize storage protection by using the limit memory: the upper and lower register method and the base address and limited length memory method. The upper and lower bound memories are used to store the start address and end address of the process storage space, respectively. In the process of running, the address of each access memory is compared with the contents of these two registers. Under normal circumstances, this address should be greater than the start address and less than the end address. If it exceeds the range of the upper and lower bound memory, an out-of-bounds interrupt signal will be generated, and the running of the process will be stopped. Use the base address and the field register to store the initial address of the process space and the space length of the process address, respectively. When the logical address accessing the memory exceeds the limit when the process is executing, an out-of-bounds interrupt signal is generated and the process is stopped.

(2) Access mode protection (protection key). Whether the storage access method is legal is determined by matching the protection key. For the storage area that is allowed to be shared by multiple processes, each process has its own access rights. The storage protection key method is to assign a separate protection key to each storage block, which is equivalent to a lock. Each process that enters the system is also given a protection key, which is equivalent to a key. Check whether the key and the lock match. If they do not match, the system sends a protective interrupt signal to stop the process.

(3) Ring protection. The processor state is divided into multiple rings, each with different storage access privilege levels. Generally, the smaller the number of the ring, the higher the privilege level.

In addition, there are four storage permissions: prohibit any operation, execute only, read only, read/write.

single sequential assignment

One of the simplest storage management methods, usually only used in single-user, single-task operating systems, this storage management method divides memory into two continuous storage areas, one of which is fixedly allocated to the OS, and the other is allocated to the OS. A storage area is allocated for use by user jobs. The single contiguous memory method mainly adopts static allocation, that is, once the job enters the memory, the memory cannot be released until it finishes execution. With this storage allocation method, only one job can be loaded into memory, thereby reducing resource utilization.

partition storage management

It is the simplest storage management method to meet the needs of multiprogramming. In the partition storage management, the memory is divided into several partitions, and the size of the partitions can be equal or unequal. Except that the operating system occupies one partition, each other partition can accommodate one user job. Partition storage management can be further divided into fixed partition storage management and dynamic partition storage management according to job changes.

Fixed partition

The fixed partition storage management method is a storage management method that can run multiple programs. It divides the memory into several fixed-size partitions in advance. The size of the partitions can vary, but it must be determined in advance and cannot be changed during runtime. In order to realize fixed partition allocation, the system needs to establish a partition description table to record the usage of the partition, including the partition number, partition size, partition address and status. When a user program is to be loaded into the memory, the memory allocation program retrieves the partition description map, finds a free partition that meets the requirements from the map and assigns it to the program, and then modifies the state information of the corresponding entry in the partition description map. When the program is executed, release the partition occupied by the program, and set the corresponding partition status information to unallocated. The size of a job is not necessarily equal to a partition size.

dynamic partition

Also known as variable partition allocation, it is a partition method that dynamically divides the memory. This allocation method does not preset the number of partitions, but dynamically creates partitions according to the size of the job when the job is loaded into the memory, so that the partition is just right. meet the needs of the job. Therefore, the size of the partitions can be variable, and the number of partitions can also be variable.

1. Data structures in partition allocation

The system needs to set up a corresponding data structure to record the memory usage. Commonly used data structures include free partition table and free partition chain.

(1) Free partition table. Each free partition in the memory occupies an entry, and each entry includes the partition number, the starting address of the partition, the size and status of the partition, and so on.

(2) Idle partition chain. Use the linked list pointer to link the free areas in the memory. For this reason, the relevant information of the partition should be stored in the first bytes of each free partition, including the size of the free partition and the pointer to the next free partition. pointer.

2. Partition allocation algorithm. In order to load a job into memory, a partition that meets the job requirements should be selected from the free partition table (or free allocation chain) according to a certain allocation algorithm and allocated to the job. If the capacity of this free partition is greater than the space capacity requested by the job, then The partition is divided into two parts, one part is allocated to the job, and the remaining part remains in the free partition table (or free allocation chain), and the relevant information in the free partition table (or free allocation chain) needs to be modified. The currently used partition allocation algorithms are: first-fit algorithm, circular first-fit algorithm, best-fit algorithm and worst-fit algorithm.

(1) The first adaptation algorithm. Also known as the first-fit algorithm, this algorithm requires that free partitions be arranged in increasing address order. Search sequentially from the beginning of the free partition table (or free allocation chain) until the first memory space that can meet its size requirements.

(2) The loop first adaptation algorithm. The next adaptation algorithm. Continue to search backward from the free partition found last time to the first free partition that satisfies the space requested by the job.

(3) The best adaptation algorithm. The free partitions are required to be arranged in the order of increasing capacity. When performing memory allocation, search sequentially from the beginning of the free partition table (or free allocation chain) until the first free partition that can satisfy the job application space is found.

(4) Worst adaptation algorithm. The requirements are arranged in descending order of capacity size. When memory allocation is performed, the first partition of the free partition table (or free allocation chain) is checked first. If the first free partition is smaller than the size required by the job, the allocation fails. Otherwise, a block of memory space equal to the size of the job is allocated from the free partition to the requester, and the remaining free partitions are left in the free partition table (or free allocation chain).

Partition recycling

When the job execution ends, the used partition should be reclaimed. The system checks whether there is any adjacent free partition in the free partition table (or free allocation chain) according to the size and starting address of the reclaimed partition. Status information for the partition.

There are four adjacent situations between a reclaimed partition and an existing free partition:

(1) A free partition is adjacent to the reclaimed partition r. (2) A partition is adjacent to the recovery partition r. (3) Reclaim the upper and lower adjacent free partitions of partition r. (4) The reclaimed partition has no adjacent free partitions. At this time, a new entry should be created for the reclaimed partition, fill in the partition size and starting address and other information, and add it to the free partition table (or free allocation chain). appropriate location.

Relocatable Partition Allocation

In partitioned storage management, jobs must be loaded into a contiguous segment of memory.

1. Splicing technology.

Unusable memory spaces in memory are called fragments or fractions. In the partition storage management mode, after the system runs for a period of time, the fragments in the memory will occupy a considerable amount of space. Fragments can be divided into internal and external fragments based on where they appear. Internal fragmentation refers to the unused portion of the allocation to a job, and external fragmentation refers to unutilized blocks of storage in the system. For example, there is internal fragmentation in fixed partition allocation and external fragmentation in dynamic partition allocation. One of the ways to solve the fragmentation problem is to move all allocated areas in the memory to one end of the memory, so that the originally scattered free areas are connected into a large free area. The method of splicing multiple small free partitions into one large free partition by moving is called splicing or compaction, and can also be called compaction. During the splicing process, the process needs to move in memory, so dynamic relocation technology support is used.

2. Relocatable partition allocation technology

In the relocatable partition allocation algorithm, if there are idle partitions that meet the job requirements in the system, the memory is allocated in the same way as dynamic partition non-allocation; if the system cannot find an idle partition that meets the job requirements, and the total system partition If the capacity and the memory space are larger than the job application, splicing is performed.

buddy system

Fixed partition storage management limits the number of short distances in memory, and there may be a large number of fragments. Although dynamic partitions can better solve the fragmentation problem through splicing technology, splicing will bring a large system overhead. A more practical way of dynamic storage management—partner system.

The partner system combines the characteristics of computer binary to divide the storage space, so that the length of each storage block is limited to the power of 2 k, and k is a natural number. The buddy system uses the buddy algorithm to manage the memory space. This method obtains small memory blocks by continuously dividing large memory blocks. When the occupied space is released after the process is executed, the system should check whether the partner of the released block is a free block, and if so, merge it into a large free block, and then continue to search and merge until no free partner is found. Only partners can merge, non-partners cannot. Disadvantage: When allocating and reclaiming, partners need to be split and merged, and there is a certain waste of storage space.

Override and swap

The so-called coverage technology is to divide a large program into a series of coverages, each coverage is a relatively independent program unit, and the coverages that are not required to be loaded into memory at the same time when the program is executed are grouped into a group, called the coverage segment. The segments are allocated to the same storage area, which is called the coverage area, which corresponds to the coverage segment one-to-one. The size of the coverage segment shall be determined by the largest coverage in the coverage segment. Covering technology requires programmers to divide a program into different program segments, and specify their execution and coverage sequences. The OS completes the coverage between program segments according to the coverage structure provided by the programmer. The overlay technology goal is to run larger memory in smaller available memory.

Swapping technology is also called swapping technology. Swapping refers to exchanging part (or all) of a process that cannot run temporarily from memory to external memory, or transferring a process that is ready to run again into memory, and transfers the control process. A technology that gives it the power to run on a system. The swap process consists of two processes: swap in and swap out. The characteristics of switching technology: increase the number of concurrently running processes, and provide users with appropriate response time, without affecting the program structure when writing programs.

Paging storage management

Also known as paged storage management.

In paging storage management, the address space of a job is divided into several equal-sized areas, called pages or pages, each page has a number, called a page number, and the page number starts from zero. Correspondingly, the memory space is also divided into areas equal to the page size, which are called blocks or physical blocks. Similarly, each physical block also has a number called a block number, which is also arranged from zero. When allocating memory space for a job, it is always allocated in blocks, and a certain page of the job can be placed in a certain free block of the memory. The size of the job is usually not an integer multiple of the page size, so the last page is often not full, and this wasted space is called in-page fragmentation. When a scheduled job runs, all pages of the job must be loaded into memory at one time, and if there is not enough space in the memory, the job waits. Such a storage management method is called a paging storage management method, also known as a simple paging storage management method, and a pure paging storage management method. The logical address in the paging storage management system includes two parts, the former part is the page number P, and the latter part is the intra-page displacement W, also called the intra-page address. In general, the address structure of a particular machine is deterministic.

Page table and memory block table

In the paging storage management system, each page of a process is scattered in the memory. In order to find the physical block corresponding to each page of the process in the memory, the system establishes a page map table for each process, referred to as a page table, which records Each page corresponds to the physical block number stored in the memory. In a paging storage management system, it is also very important to buy your size choice. In order to implement memory management, the OS should also know which physical blocks in the memory are used, which physical blocks are free, and how many physical blocks there are in total. This information is stored in a data structure called the Memory Block Table (MBT). The entire system creates only one MBT to record the usage of physical memory blocks. There are two implementations of the memory block table. Bitmap method: A binary number is used to represent the status of the physical block, 1 means that the physical block is allocated, and 0 means that the physical block is not allocated (idle). Free storage block chain: Link all free storage blocks in the system with linked list pointers, and use the unit in the free physical block to store the pointer to the next physical block.

Basic Address Translation Mechanism

In the paging storage management system, the conversion from logical addresses to physical addresses needs to be implemented with the help of page tables. Since the page table is usually placed in the memory, for the convenience of implementation, a page table register is set in the system to store the address of the page table in the memory and the length of the page table. When the process is not executing, the starting address and length of the page are stored in the PCB. When the process is executed, the page table start address (page table address) and length are stored in the page table register. When a process wants to access an instruction or data in a logical address, the address translation mechanism of the paging system automatically divides the address into page numbers and intra-page displacements, and then uses the page number as an index to retrieve the page table. Before searching, compare the page number with the length of the page table. If the length of the page table is exceeded, it means that the address accessed this time has exceeded the process address space, and an out-of-bounds interrupt is issued. If no out-of-bounds occurs, the position of the corresponding page table entry is calculated from the page table address and page number, and the physical block number storing the page is obtained from it. Finally, the physical block number and the in-page displacement are spliced ​​together to form the physical address for accessing the memory.

Address Translation Mechanism with Fast Table

If the page tables are all stored in the memory, accessing a data or an instruction requires at least two accesses to the memory, one is to access the page table to find the physical address to be accessed, and the second is to access according to the obtained physical address. instruction or data. In order to improve the speed of address conversion, a cache memory (also called associative memory or fast table) with parallel search capability can be added to the address conversion mechanism, and the page table is placed in this fast table. Cache memory is generally realized by semiconductor memory, and its working cycle is roughly the same as that of CPU, but the cost is higher. In order to reduce costs, the page table entries currently accessed by the running job are usually stored in the fast table, and the rest of the page table is still stored in memory. The ratio of the number of times the specified page table entry is found in the fast table to the total number of searches is called the fast table hit rate. Due to the cost, the space of the fast table can no longer be set too large, which generally consists of dozens of units.

Multilevel page tables and inverted page tables.

Divide the page table into several smaller segments and store them discretely in memory; or store only some of the table entries currently needed in memory, and store the rest on disk, and then dynamically transfer them when needed.

Multi-level page table: In a system with two-level page table, the process of address conversion is: use the first-level page number p1 in the logical address as an index to access the first-level page table, and find the address of the second-level page table; The level page number p2 finds the specified page table entry, and takes out the physical block number from it and connects it with the in-page address w to form a physical address.

Inverted page table (inverted page table, reverse page table): Usually each process has a page table, and each page in the process has an entry corresponding to it in the page table, that is to say, the page table is based on The virtual addresses are sorted, so that the OS can easily use the page number to obtain the physical block number where the page is stored. The inverted page table sets a page table entry for each physical block, and sorts these page table entries according to the size of the physical block number. In this way, the entire system has only one page table, and each physical block of memory corresponds to an entry in the page table. The process of using the inverted page table for address translation is: use the process identification number and the page number to retrieve the inverted page table, then the serial number of the table entry is the physical block number of the page in the memory, and the physical block number is spliced ​​with the in-page address. form the physical address. If the entire page table is searched and no matching page table entry is found, an illegal address access has occurred, indicating that the page is currently not in memory. For the storage management system with the function of paging on demand, an interrupt on paging on demand should be generated; if If the system does not support this function, it means that the address is wrong. Disadvantage: Increased retrieval time. In systems that support demand paging, since some pages of the process are not stored in memory, a traditional page table must be created for the process and stored in external memory. This page table will be used when accessing memory.

segmented storage management

For the various storage management technologies introduced earlier, the user logical address space is a linear address space. In general, a job consists of multiple program segments and data segments, which requires compiling and linking programs to arrange them linearly in one dimension, which brings difficulties to the sharing and protection of programs and data. Segmented storage management is also called segmented storage management.

In the segmented storage management system, the address space of a job consists of several logical segments. Each segment is a set of information with relatively complete logical meaning. Each segment has its own name. Addressing starts from 0 and uses a continuous address space. Segmented storage management allocates memory in units of segments, and each segment allocates a contiguous memory area, but the segments are not required to be contiguous. Memory allocation and reclamation are similar to dynamic partition allocation. The logical address of the segmented storage management system consists of the segment number S and the displacement W (intra-segment address) within the segment.

Break table and address conversion

Similar to paging storage management, in order to realize the conversion of logical addresses to physical addresses, the system establishes a segment table for each process, in which each table entry describes the information of a segment, and the table entry includes the segment number, segment length and the segment. memory start address. The segment table is generally placed in memory.

To facilitate address conversion, the system sets up a segment table register to store the segment table start address and segment table length. When performing address conversion, the system compares the segment number in the logical address with the length of the segment table. If the boundary is exceeded, an out-of-bounds interrupt signal will be generated. Read the starting address of the segment in the memory, and then check whether the displacement in the segment exceeds the segment length of the segment. If it exceeds the segment, an out-of-bounds interrupt signal is issued. If the segment is not out of bounds, the segment's starting address and the segment address are added to get The physical address to access. In order to improve the access speed can also use the fast table.

Difference Between Paging and Segmentation

Both use the discrete allocation method, and both implement address conversion through an address conversion mechanism. the difference:

(1) A page is the physical unit of information, and paging is to achieve discrete allocation to reduce memory fragmentation and improve memory utilization. A segment is a logical unit of information that contains a set of relatively complete information.

(2) The size of the page is fixed by the system, and the logical address is divided into the page number and the displacement within the page, which is realized by hardware. The length of the segment is not fixed, and is divided by the compilation system according to the nature of the information when compiling the source program.

(3) The address space of the job in the paging system is one-dimensional, while the address space of the job in the segmentation system is two-dimensional. When the programmer identifies an address, he must give both the segment name and the displacement within the segment. .

In the segment page management system, the address space of a job is first divided into several logical segments, each segment has its own segment number, and then each segment is divided into several pages of fixed size. The management of the memory space is still the same as the paging management. It is divided into several physical blocks with the same size as the page, and the memory is allocated in units of physical blocks. In the segment-page management system, the logical address of the job includes: the segment number S, the page number P in the segment, and the displacement D in the page. In order to realize address conversion, a segment table and a page table need to be set up in the segment page management system at the same time. The system creates a segment table for each process, and each segment has a page table. The segment table entry includes at least the segment number, the start address of the page table, and the length of the page table. The start address of the page table indicates the start address of the page table of the segment in memory. The page table entry should at least include the page number and block. No. In addition, in order to facilitate the realization of address conversion, the system also needs to configure a segment table register, which stores the start address of the segment table and the length of the segment table. When performing address conversion, first use the segment number S to compare the length of the segment table in the segment table register. If it is less than the length of the segment table, it means that there is no out of bounds. Therefore, use the start address and segment number in the segment to find the corresponding segment of the segment. The position of the table entry, from which the page table start address of the segment is obtained, and then the segment number P in the logical address is used to obtain the position of the corresponding page table entry, from which the physical block number of the page is read, and then spliced ​​with the displacement in the page form a physical address.

finally:

There are three storage allocation methods: direct allocation, static allocation and dynamic allocation. Direct allocation means that programmers use physical memory addresses when writing programs or compiling programs to compile source programs; static allocation means determining their locations in memory when jobs are loaded into memory. It cannot be moved in memory, nor can it apply for memory space; dynamic allocation refers to determining the location of a job in memory when it is loaded, but can apply for additional memory space as needed during its running process.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324837358&siteId=291194637