当前位置:网站首页>17. Memory partition and paging
17. Memory partition and paging
2022-07-04 12:35:00 【PacosonSWJTU】
【README】
1. The content of this paper is summarized from B standing 《 operating system - Li Zhijun, teacher of Harbin Institute of technology 》, The content is great , Wall crack recommendation ;
2. The program uses memory 3 A step :
- step 1: Divide the program into segments , Including code snippets , Data segment ; This is what compilation should do ;
- step 2: Find a free memory in memory ( Or free partition );
- step 3: Once the free partition is found , Just read the segment contents of the program on the disk into the free partition ; In the process of reading , Need to put LDT Initialize well ,LDT The mapping relationship between segment sequence number and segment base address is stored ;
【1】 Memory partition
1) Memory partition : How to divide memory , In order to load each segment of the program into the corresponding memory partition ;
simply , Memory partition refers to a segment divided to store program segments ( Or code snippets cs, Or data segment ds) Of memory space ;
【1.1】 Fixed partition and variable partition
1) Fixed partition
Equal division , During the initialization of the operating system, the memory is divided into k Zones ;
But the length of the segment varies , The fixed partition cannot meet the variable segment length Business scenario of ;

【1.2】 Variable partition management
1) Core data structure
- Free partition table : Store free memory partition data ( Or the memory space list after segment release );
- Partition table assigned : Record information of used memory partition ;
- Be careful : Zone information Just record Partition base address and length These two kinds of information ;

2) Request to allocate memory

【 The illustration 】
- Currently, the free memory address space is 250K~500K;
- And paragraph 3 Memory request allocation 100K, Then put 250K~350K Assign to this paragraph 3(seg3); And add a new allocated partition table record ;
3) Free memory
【 example 】 paragraph 2 No longer need , Release segment 2 Of memory
At the same time, update the released memory to the space partition table , Remove segments from the allocated partition table 2 The record of ;

4) Apply again
At this time, the free partition table is as follows :
Memory partition number | Base address | length |
1 | 350K | 150K |
2 | 200K | 50K |
【1.3】 Memory partition allocation algorithm
Memory request allocation 40K, And partition 1 Yes 150K, Partition 2 Yes 50K, That should allocate partitions 1 Or partition 2 Give this request ?
- First, adapt :(350, 150), Select the first partition in the free partition table with enough partition space to allocate ; Free partition table query is fast enough ;
- Best fit :(200, 50), Less memory waste , But the free partition size will be smaller and smaller , There are many memory fragments after segmentation ;
- Worst fit :(350, 150), Choose the largest partition to allocate , A lot of memory space is wasted , But the free partition size is relatively uniform , Less memory fragmentation ;

【 example 】 Which memory partition allocation algorithm is the best

【2】 paging
【2.1】 Memory fragmentation problem
0) problem : Caused by memory partition Memory fragmentation problem
- resolvent : Introduce paging Solve the problem of memory fragmentation caused by memory partitioning ;
- The actual physical memory allocation is Use paging instead of partitioning ; ( Add : Virtual memory is partitioned or allocated )

【 The illustration 】 Memory fragmentation problem :
- As shown in the figure above , The application size is 160K Of memory space , In the free partition table 2 All partitions are smaller than 160K And they are not contiguous memory address spaces , Therefore, it is impossible to allocate directly 160K Of memory space ( You need to merge free partitions first , Gather the pieces together ), Even if the total memory size is 200K( Greater than 160K);
1) Memory fragments :
- The total available memory size is larger than the requested memory size , But memory partitions are not continuous , And each partition is smaller than the application size , The memory request failed ;
2) resolvent :
- Memory crunch ; In short , Merge the white partitions , Color partitions are merged at once , This requires copying the content ;
3) Problems caused by memory compression ( Memory compression takes time and effort ):
- Need to modify the segment base address LDT, Very trouble ;
- In the process of memory compression , The upper user application cannot execute , Because multiple segments of the program are copying content to the new memory space , The segment base address needs cascade modification ;
- Memory compression takes a long time , It is easy to cause the machine to fake death ;
Summary : Memory compression is not feasible to solve memory fragmentation ; This leads to memory paging to solve the problem of memory fragmentation ;
【2.2】 paging
1) Definition : When the operating system is initialized , The physical memory space is divided into multiple pages , Each page takes 4K Memory size ;
When processing segment memory requests , Allocate memory to segments on a page by page basis , The following table :
surface 2 Memory paging table
Physical memory page number ( Page frame number ) | paragraph | page |
7 | paragraph 0 | page 3 |
6 | paragraph 0 | page 0 |
5( Free page ) | ||
4( Free page ) | ||
3 | paragraph 0 | page 2 |
2( Free page ) | ||
1 | paragraph 0 | page 1 |
0( Free page ) |

2) Advantages of memory paging :
- advantage 1: There is no memory fragmentation , Because the free pages in the middle can be allocated to other processes ;
- advantage 2: Less memory waste : One segment is wasted at most 1 page , That is, one segment is wasted at most 4K( A segment consists of multiple pages );
【 Summary 】
- Physical memory is divided and allocated by paging , Reduced memory waste , Avoid memory fragmentation ;
- Users want to divide the program into multiple segments ;
- Sum up , The operating system needs to support both segmentation and paging to partition and allocate memory ;
【 example 】 Memory allocation in pages

【 problem 】jmp 40 To which page ?
- Allocation of memory pages , Record through page table , And each process has a page table ; Page table register CR3; The page table base address is stored in the process PCB;
【 example 】mov [0x2240], %eax
- step 1:0x2240 Divide 4K, That is, move right 12 position , Get page number as 0x02, offset 0x240;
- step 2: By page number 2 Find the leaf frame number 3;
- step 3: Get the physical address 0x3240 ;(3 multiply 4K,3 Move left 12 Bit gets the base address 0x3000);
- step 4: Page box 3 The base address of is 0x3000, offset 0x240, So the physical address is 0x3000 + 0x240 obtain 0x3240 ;
- Add : Put the logical address 0x2240 according to CR3 Stored page table base address , It can be translated into physical address 0x3240, This memory address translation process is performed by MMU Accomplished ;
【 Summary 】
- A program consists of multiple segments ;
- The contents of each segment are scattered and stored in multiple pages in memory ; Each segment is not stored directly in memory ( That will cause memory fragmentation and waste );
- In order to realize relocation during program execution ( Find the segment base address plus the offset address to get the physical memory address ), You need to establish a page table to store the mapping relationship between page number and page frame number , And finding the page frame number can calculate the memory page base address ;
- Because the size of each page 4K, Multiply the page frame number by 4K Get the page base address ;
边栏推荐
- Classification and application of AI chips
- MySQL advanced (Advanced) SQL statement
- The most robust financial products in 2022
- Netgear switch basic configuration command set
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- Leetcode: 408 sliding window median
- Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- Entity framework calls Max on null on records - Entity Framework calling Max on null on records
- Method of setting default items in C # ComboBox control code
- Reptile learning 4 winter vacation series (3)
猜你喜欢

Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution

2021-08-09

Btrace tells you how to debug online without restarting the JVM
![Cadence physical library lef file syntax learning [continuous update]](/img/d5/0671935b074e538a2147dbe51a5a70.jpg)
Cadence physical library lef file syntax learning [continuous update]
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7](/img/44/1861f9016e959ed7c568721dd892db.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12](/img/b1/926d9b3d7ce9c5104f3e81974eef07.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12

DDS-YYDS

Realize cross tenant Vnet connection through azure virtual Wan

Decrypt the advantages of low code and unlock efficient application development

(2021-08-20) web crawler learning 2
随机推荐
How do std:: function and function pointer assign values to each other
Review of week 278 of leetcode II
Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
mm_ Cognition of struct structure
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Snowflake won the 2021 annual database
01. Basics - MySQL overview
Global and Chinese market of piston rod 2022-2028: Research Report on technology, participants, trends, market size and share
03_ Armv8 instruction set introduction load and store instructions
8.8.1-PointersOnC-20220214
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
IPv6 experiment
Pat 1059 prime factors (25 points) prime table
昨天的事情想说一下
Reptile learning 3 (winter vacation learning)
[the way of programmer training] - 2 Perfect number calculation
CSDN documentation specification