Operating System-Memory Management

The following mainly describes the process to memory mapping

1. The development history of memory management

1.1 Single-process DOS era

DOS era-only one process can be running at the same time, single process
windows9x start, multiple processes can be loaded into the memory
.
Causes the problem: the memory is bursting and
disturbing each other
Insert picture description here

1.2 Memory management

In order to solve the problems mentioned above, the current memory management system is introduced: using virtual addresses, paging loading, and addressing with a combination of software and hardware.

1.2.1 Solve the memory explosion

Paging the memory (the memory is not enough), divide the memory into fixed-size page frames (4K), divide the program (on the hard disk) into 4K blocks, which block is used, which block is loaded, and during the loading process, if the memory is already When it is full, the least frequently used block will be placed in the swap partition, and the latest block will be loaded (LRU algorithm)

For example, when QQ.exe is executed, its page table is recorded, and when it is executed, which page in the page table is used, the page is loaded into the memory.
During the loading process, if the memory is full, the least frequently used block will be placed in the swap partition (Linux swap partition), and the latest block will be loaded (LRU algorithm).
Insert picture description here
Almost all those involved in caching use LRU (Least Recently Used) or LFU algorithm.

How to design the LFU cache mechanism to support obtaining data and writing data O(1) complexity:
if it is an array, each element saves the timestamp, if you check it again, the complexity is O(n)
if you use a singly linked list, the most recently used Added to the tail, the head is definitely the least frequently used. When one of them is used again, the position needs to be moved. These are pointer operations O(1), but searching for a certain element is still O(n).
Improved: use hashmap hash table (guarantee search operation O(1))+ Doubly linked list (linked list guarantees the sorting operation and new operation O(1), doubly linked list guarantees that the block pointed to by the left pointer of the found element block can point to the right block), the LinkedHashMap of java implements caching in this way
Insert picture description here

1.2.2 Solve the problem of mutual disturbance

Virtual memory
In order to ensure that they do not affect each other-let processes work in virtual space, the space address used in the program is no longer a direct physical address, but a virtual address. In this way, process A can never access the space of process B.
Virtual space size: look at the addressing space-64-bit system 2^64, 32-bit system 2^32 (expressed 2^32 different memory addresses), and each address can store 8bit data, that is, the unit is byte
station From the perspective of virtual space, the process is exclusive to the entire system + CPU

Virtual space segmentation, segment paging, which page needs to be loaded into the page frame?
Insert picture description here
The virtual address used by the program, how does it map to the physical address:
offset (20 below) + base address of the segment (1000 below) = linear address (virtual Space) After
obtaining the linear address, use OS + MMU (cpu internal hardware Memory Management Unit)
Insert picture description here
to further understand
P1, P2, P3, and P4 through the following figure. The four processes think that they are monopolizing the entire kernel, which is actually a shared operating system. Kernel.
MMU allocates their memory resources to each process.
If the memory is full, use the LRU algorithm to put the least frequently used pages into the swap space of the hard disk.
Insert picture description here
When the page fault interrupt
is executing an instruction, if it is found that the page that needs to be used is not in the memory, then the execution of the instruction is stopped, and a page fault exception (interrupt) is generated, which is processed and loaded by the kernel. After that, the exception caused by the original The instruction can continue to execute without generating an exception.

Guess you like

Origin blog.csdn.net/qq_33873431/article/details/112003010