Internal = deposit = management = management

Early system

Multiple processes are all loaded into memory and memory bursts. And multiple programs are in user mode, interfering with each other, accidentally accessing others' memory

Solve the problem of memory explosion

【Screenshot】

Pagination

First, we divide the program into blocks. The size of the block is fixed. This block is called a memory page (The most basic standard memory page of the program, the size is 4KAlthough the current memory page size is 16K, 64K, etc., the most basic size is 4K. Some systems do not support large paging, but 4K paging is supported).

At the same time, in my memory, I also divided the memory space into frames, called page frames. Which page to use and which to install.

That is to say, the management of memory with regard to the process now actually unloads the process logically . So our usual program, I want to load it into memory to run, has it really been loaded into memory? You said QQ.exe, I want to put it in memory to run it, will it be installed in memory? No. In fact, I just paging QQ.exe into a page table, and told the process scheduler that there is a process that needs to come in. Its executable entry is on the third page (assuming there are 6 pages). Then the process scheduler said OK, I just have a space here, pay attention to only one! Could you please bring in the third page, then the third block can be executed. During the execution, the third block needs the fourth block of data, there is a space next to it, and the fourth block of content is loaded.

Swap partition

The problem is, if my memory is full, the fourth block will use the fifth block, what should I do? Swap the least frequently used block swap and put it in the swap partition . Linux has a special partition called a swap partition . What it does is to throw the least commonly used blocks in memory into it. Of course, this is achieved by using a hard disk (swap partition), which is relatively inefficient. But in such a way, it can support more and more talkative process management. This is the origin of the swap partition.

When a process has just been loaded into memory and has not yet begun to execute, the operating system has allocated resources to it, not even memory, but just allocated a table to it, saying I know that you have divided several pages, each page What have you done. The current operating system does just that, which page is used to load which page. When the memory is full, the newly loaded blocks will swap the least frequently used blocks to swap swap memory. It is the same if the data on the swap memory is to be used again, and then swap out the oldest one.This algorithm is the famous LRU algorithm

Headline interview third round hand tearing LRU algorithm

leetcode 146 questions Video 161 section 1 hour 05 minutes
LRU (Least Recently Used) The least commonly used algorithm recently. Find the least commonly used piece and replace it. That's what it means.
Problem-solving ideas:

  1. Hash table (guaranteed search operation O (1)), linked list (guaranteed that the operation of changing a node in the linked list to the tail is O (1))
  2. Doubly linked list (operation to ensure that a node in the linked list is moved to the tail and deleted from the original list O (1))

Solve the problem of mutual interference: virtual address space

Virtual Memory

【Screenshot】

  1. In order not to affect each other, let the process work in the virtual space. A virtual space is not a real space. The addresses you read and write in are all virtual, not real physical addresses.
  2. The address space used in the program is no longer a direct physical address, but a virtual address, so that the A process can never access the B process space.
  3. How big is the virtual address? It is the size of your addressing space. If it is a 64-bit system, the size is 2 ^ 64 bit, which is much larger than the physical space.
  4. From a virtual perspective, the process is exclusive to the entire system (2 ^ 64 bit) + CPU
  5. Linear addresses are mapped to real physical addresses through OS + MMU (Hardware Memory Management Unit).

Why use virtual memory

【Screenshot】

Memory map

Again, from a process perspective, it occupies a huge, unique space that is not shared with others. But this space is virtual. How to map the virtual memory space to the real physical space one by one is called memory mapping.

How to do memory mapping?

Here are a few concepts:

  • Logical address: equivalent to offset
  • Linear address: equivalent to base address + offset
  • Physical address: the address on the real hardware

Linear addresses are mapped to physical addresses, relying on the MMU. In other words, as a process, you can never know which real physical address is, only the operating system knows it. This becomes very safe.

【Screenshot】

The above picture, P1, P2, P3, P4, 4 processes, from their perspective, they all monopolize the entire kernel, in fact it is not the same thing, the kernel has evolved a lot of phantoms, let the big guy think Everyone enjoys her alone, this is a watery woman, coping with many people at the same time. Then she cooperated with another buddy, this buddy is MMU. The MMU allocates memory space for each process. If the memory space is not enough, the LRU algorithm is used to throw unnecessary memory pages to the swap partition on the hard disk .

When a process needs to find an address, it needs to go through the operating system and the MMU to get the real address. This will make the operating system very, very safe.

to sum up

[The bottom layer of the operating system that Java programmers need to master. Md] The content above ZGC.

Published 40 original articles · Likes0 · Visits 381

Guess you like

Origin blog.csdn.net/weixin_43780400/article/details/105685177