[Review notes] Operating system memory management

1. Three ways to load the program into memory

1. Absolute loading

Insert picture description here

2. Repositionable loading

Insert picture description here

3. Dynamic Relocation

Insert picture description here

The process from writing to running

Insert picture description here

Two, three ways of program linking

Insert picture description here

Insert picture description here

3. Covering technology

Insert picture description here
Insert picture description here

Four, exchange technology

Insert picture description here
Insert picture description here

5. Continuous distribution management method

1. Single continuous distribution (continuous)

Insert picture description here

2. Fixed partition allocation (continuous)

Insert picture description here
Partition description table

Insert picture description here

3. Dynamic partition allocation (continuous)

Insert picture description here

Data structure for storage

Insert picture description here
Partition allocation algorithm

  • First fit algorithm: free partition according to address increasing order link, each from a low start address lookup , find the first free partition size to meet
  • Best fit algorithm: free partition in accordance with the capacity increasing order links, order to find spare partition chain every time memory allocation
  • The worst adaptation algorithm: free partition in accordance with the descending order of capacity link, find the spare partition chain sequence each time memory allocation
  • Nearby adaptation algorithm: free partition according to address increasing order link, each time from the beginning of the end to find the last time to find the location , find the first free partition size to meet. You can skip as first fit algorithm small free partitions much lower address section. In the end, the remaining free partitions are relatively uniform .

Insert picture description here

Distribution and recycling of partitions : As long as the recycled partitions are adjacent to other free partitions, they need to be merged.

Insert picture description here

Six, basic paging storage management

Page, page: the part after the process is divided.
Page frame, page frame: the part after the memory space is divided.

Page table

Insert picture description here
Insert picture description here
Address translation

Insert picture description here
Example address conversion

Insert picture description here
Note: If the size of each page is 2 k B 2^k B2k B, use a binary number to represent the logical address, then the endkkThe k bit is the offset within the page, and the rest is the page number. If the page size happens to be2 22 integer powers, the page table need only recorda physical block numberandpage offsetsplicing to givethe physical address

Address conversion mechanism
Insert picture description here
Insert picture description here
base address conversion table means having a fast (TLB) of

Fast table: It is a high-speed cache with much faster access speed than memory (TLB is cache, not memory). It is used to store a copy of recently accessed page table entries , which can speed up address conversion. Corresponding to this, the page table in memory is called a slow table.
Insert picture description here

Insert picture description here
Note: Fast table can reduce the number of memory accesses, and access to fast table is faster than access to memory at the same time

After the introduction of fast tables, address conversion speeds up
Insert picture description here

Principle of locality

Insert picture description here

Seven, two-level page table mechanism

Problems with single-level page tables:

  1. The page table must be stored continuously, so when the page table is large, it needs to occupy a lot of continuous page frames
  2. There is no need to make the entire page table resident in memory, because the process may only need to access a few specific pages for a period of time

Insert picture description here
Insert picture description here
Insert picture description here

8. Basic segmented storage management

Insert picture description here
Segmented storage management address conversion mechanism

Insert picture description here

Comparison of paging and segmentation

Insert picture description here

Insert picture description here

Nine, segment page storage management

Insert picture description here
Insert picture description here

Paragraph address conversion mechanism
Insert picture description here

10. Virtual storage technology

Characteristics and disadvantages of traditional storage management methods

Insert picture description here

Definition and characteristics of virtual storage

Insert picture description here

Implementation of Virtual Storage Technology

Insert picture description here

Basic page table and request page table

Insert picture description here

Operation when a page fault is interrupted

Insert picture description here

The difference between request paging and basic paging

Insert picture description here

Points to note when requesting pagination

11. Page replacement algorithm

1. Optimal permutation algorithm (OPT)

Insert picture description here

2. First-in first-out replacement algorithm

Insert picture description here
Note: Actually difficult to achieve

3. Least Recently Used (Least Recently Used)

Insert picture description here

4. Clock replacement algorithm (CLOCK)

Insert picture description here

5. Improved clock replacement algorithm

Insert picture description here

Comparison of replacement algorithms

Insert picture description here

12. Page allocation strategy

Insert picture description here
Jitter phenomenon: The page that has just been swapped into the memory will be swapped out immediately. Frequent page swapping in and out causes the process to fail to work properly. The main reason: the number of pages frequently accessed by the process is higher than the number of available physical blocks.

Insert picture description here

Thirteen, stack allocation

The memory occupied by a C/C++ compiled program is divided into the following parts:

  1. Stack area (stack) : automatically allocated and released by the compiler, storing function parameter values, local variable values, etc.
  2. Heap : It is allocated and released by the programmer. If the programmer does not release it, the OS may reclaim it when the program ends.
  3. Global area (static area) (static) : The storage of global variables and static variables are placed together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. An area.
  4. Text constant area: The constant string is placed here.
  5. Program code area: store the binary code of the function body.

The stack is a contiguous block in memory. A register called the stack pointer (SP) points to the top of the stack. At the bottom of the stack is a fixed address. One characteristic of the stack is that it is last in, first out. In other words, the data that is put in later is taken out first.

Heap is a data structure that extends to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses.

  • In high-level languages, the program function calls and variables defined in the functions all use the stack.
  • The variable space allocated by malloc, calloc, realloc and other functions is on the heap.
  • What is defined outside of all functions is a global quantity.
  • After adding the static modifier, no matter where it is placed, it belongs to a static variable and is stored in the global area (static area)
  • The static variables defined outside the body of all functions are valid in this file and cannot be externed to other files.
  • The static defined in the function body means that it is only valid in the function body.
  • Strings such as "armfly" in the function are stored in the constant area.
int a = 0; //全局初始化区, 可以被其他c文件 extern 引用
static int ss = 0; //静态变量,只允许在本文件使用
char *p1; //全局未初始化区
void main(void){
    
    
	int b; //栈
	char s[] = "abc"; //栈
	char *p2; //栈
	char *p3 = "123456"; //123456\0在常量区, p3在栈上。
	static int c =0; //全局(静态)初始化区
	p1 = (char *)malloc(10); //在堆区申请了10个字节空间
	p2 = (char *)malloc(20); //在堆区申请了20个字节空间
	strcpy(p1, "123456"); /* 123456字符串(结束符号是0,总长度7)放在常量区,编译器可能会
	将它与p3所指向的"123456"优化成一个地方 */
}

Reference: local variables, global variables, heap, stack, static and global

Guess you like

Origin blog.csdn.net/qq_42500831/article/details/107975194