Operating system memory overload (swap and virtual memory)

If the physical memory of the computer is large enough, all processes can be saved. But in fact, the total amount of RAM required by all processes usually far exceeds the range supported by the memory. Faced with the problem of memory overload, the operating system has two general methods . One is to swap , transfer a process completely into the memory, let the process run for a period of time, and then save it back to the disk. Idle processes are mainly stored on the disk. It will not take up memory when it is not operating. Another strategy is virtual memory , which enables the program to run when only part of it is transferred to the memory.

exchange

The operation of the swap system is shown in the figure below. The
Insert picture description here
shaded part is the unused memory. When the program is running, A is first transferred to the disk, and finally transferred to the memory. Swapping will generate many space areas in the memory. By moving all processes down, small free areas can be merged into large blocks. This technique is called memory compaction . Its essence is also a process of copying, mass data in the data memory. Move and copy accordingly will consume a lot of CPU time.

Swap has a problem to consider when allocating memory. Memory should be allocated for the process when it is created or transferred. If the allocation principle for a process with a constant size is simple, the operating system allocates it according to its required size. But now many programming languages ​​allow users to dynamically allocate memory from the heap, so when the process space is trying to grow, how to allocate memory? A feasible method is to allocate some additional memory when swapping in or moving the process, and divide the allocated memory into two parts: the size required by the process and the space reserved for growth. It is worth noting that when the process is called out of this memory, what needs to be exchanged is the actual size of the process, otherwise the extra memory exchange will be wasted.
For the design of this type of structure, you can refer to the following figure. The
Insert picture description here
heap stores the variables dynamically applied by the programmer, and the stack stores ordinary local variables and return addresses. The program stack segment grows downwards at the top of the memory occupied by the process, immediately after the program segment. The data segment grows upward. The memory between the two is used by the two segments. When this space is used up, the process must move to a sufficiently large free area or be swapped out of memory to wait for enough memory area to be placed in or terminate the process.

For dynamically allocated memory, the operating system needs to manage it. The main methods are the bitmap method and the free linked list method. In linux, some specific memory allocators, such as buddy allocators and slab allocators, are used.

Virtual Memory

The basic idea of ​​virtual memory: each program has its own address space, this space will be divided into multiple blocks, each block is called a page or page. Each page has a contiguous address space. These pages are mapped to physical memory, but not all pages must be in memory to run programs. When the program references a part of the address space in the physical memory, the hardware immediately executes the necessary mapping. When the program references a part of the address space that is not in the physical memory, the operating system is responsible for loading the missing part into the physical memory and re-executing the failed instruction.

On any computer, the program references a set of memory addresses. Address can be generated by index, base address register, segment register and other methods. These addresses are called virtual addresses, and they form a virtual address space. On a computer without a virtual address, the system directly transports the virtual address to the memory bus, and read and write operations use physical memory words with the same address; if virtual memory is used, the virtual address is not sent to the memory bus. Instead, it is sent to the memory management unit MMU , and the virtual address is mapped to the physical address through the MMU.

Insert picture description here
Suppose there is a computer with a 16-bit address, the address range is from 0-64K-1, these addresses are virtual addresses. However, this computer has only 32KB of physical memory, so the 64KB program cannot be loaded into the memory. The virtual address space is divided into pages according to a fixed size , and the corresponding unit in the physical memory becomes a page frame . The size of the page and page frame in this question is 4KB.

If the program tries to access address 0 at this time, execute the following instruction
MOV REG 20500.
The virtual address 20500 is sent to the MMU. The MMU finds that the virtual address 20500 is on page 5. According to the mapping result, this page corresponds to page frame 3, and the virtual address 20 is 20 bytes away from the start address of virtual page 5, that is, the offset is 20. So finally the MMU converts the virtual address into a physical address of 12288+20=12308. By properly setting the MMU, 16 virtual pages can be mapped to 8 page frames, but there are 8 pages that are not mapped. In the actual hardware, there is a flag to record whether the page is in the memory.

What happens if a page that is not in memory is accessed at this time? The MMU notices that the page is not mapped, so the CPU falls into the operating system. This trap is called a page fault interrupt . At this time, the operating system will use the page replacement algorithm, select a rarely used page frame, and write its content to the disk (if it is not on the disk). Then read the page that needs to be accessed into the page frame just recycled, modify the mapping relationship, and then restart the instruction that caused the trap.

For the mapping relationship between virtual memory and physical memory, we can use page tables to describe. The virtual address can be assigned a virtual page number and an offset within the page. For example, for a 16-bit address and a 4KB page size, the upper four bits are the virtual page number, and the lower 12 bits are the offset within the page. The virtual page number can be used as an index of the page table to find the page table entry corresponding to the virtual page. The page frame number in the corresponding memory can be found by the page table entry. Then the page frame number + page offset to get the final physical address.

Page table entry structure

Insert picture description here

The following two issues need to be considered in any paging system:
1. The mapping of virtual addresses to physical addresses must be very fast.
2. If the virtual address is large, the page table will also be large.
For problem 1, we found that most programs always access a small number of pages multiple times, so only a few page table entries will be read repeatedly, so we set up a small hardware device to directly map the virtual address to the physical address. Without accessing the page table, this device is called the conversion detection buffer or fast table .
For the second problem, to deal with the huge virtual address space, two methods are adopted. One is to use a multi-level page table; the other is to use an inverted page table.

When a page fault occurs, the operating system must select a page in the memory to be called out to make enough space for the page that is called in. If the page in the affected area has been modified while the memory is resident, it must be written back to the disk. If it has not been modified, it does not need to be written back. Simply use the transferred page to cover the eliminated page.

Page fault interrupt processing needs to go through steps such as protecting the interrupt site, analyzing the cause of the interrupt, transferring to the interrupt processing program, and restoring the interrupt site. The specific process is shown in the figure.
Insert picture description here

In the page replacement selection, the page replacement algorithm is particularly important.

  1. FIFO first-in first-out page replacement algorithm
  2. LRU least recently used page replacement algorithm: When a page interruption occurs, the page that has not been used for the longest time is replaced. It is difficult to achieve, because a linked list is required in memory to maintain, the most recently used pages are placed at the head of the table, and the least used pages are placed at the end of the table. The entire linked list must be updated every time the memory is accessed.
  3. Best permutation algorithm
  4. Simple clock algorithm: Set an access bit for each page, and then all pages form a circular queue through the link pointer. When a page is accessed, its access bit is set to 1. The replacement algorithm only selects the page to be eliminated. Need to check the access position of each page. If it is one, the page access position will be changed to zero and not swapped out. If it is 0, the page is swapped out as an obsolete page.
  5. Improved clock algorithm

Finally, summarize the advantages and disadvantages of virtual memory

advantage

  1. Expand the memory address space.
  2. Memory protection, each process runs in its own virtual memory space without interfering with each other. Virtual memory also provides write protection to specific memory addresses to prevent code or data tampering.
  3. Fairly allocate memory, each process is equivalent to the same size of virtual memory space.
  4. Virtual sharing can be used when processes communicate.
  5. When different processes need to use the same code, only one copy of such code can be stored in the physical memory, and different processes only need to map their own virtual memory into it.
  6. When the program needs to allocate contiguous memory space, it only needs to allocate contiguous space in virtual memory space without the contiguous space of actual physical memory, and memory space fragments can be used.

Disadvantage

  1. Virtual memory requires a lot of data structures to be established, which takes up additional memory.
  2. The mapping of virtual addresses to physical addresses increases instruction execution time.
  3. The replacement of pages requires disk I/O, which is time consuming.
  4. If there is only part of the data in a page, memory will be wasted.

Guess you like

Origin blog.csdn.net/GGGGG1233/article/details/114828127