Operating System Principles for Undergraduate Students Learning Record
Learning Record Family Bucket
Chapter 8 Memory Management
8.1 Memory management background
8.1.1 Basic hardware
Programs must be loaded into memory to be executed
The only memory that the CPU can directly access is the main memory cache and registers
Registers can usually be accessed in 1 (or less) CPU clock cycle, and main memory accesses may take multiple CPU clock cycles to complete
CPU pause (Stall): When reading memory data, the CPU is idle
Cache is located between main memory and CPU registers to coordinate speed differences
Memory protection is required to ensure correct operation
Memory management unit MMU:
-
Hardware that maps virtual addresses to physical addresses
-
It is the control line used by the CPU to manage memory
-
In the MMU strategy, the value in the base register is added to every address generated by a user process when it is brought into memory
-
The user program corresponds to the logical address, and the physical address is never visible to it
Purpose of memory management
- Improve memory utilization
- Improve instruction execution speed
- Ensure safe operation of instructions
Functions of memory management:
- memory allocation
- memory recovery
- address translation
- storage protection
- memory expansion
- memory sharing
8.1.2 Logical and physical addresses
Logical address Logical address
-
Generated by the CPU
-
relative address within the process
-
Also known as: virtual address, program address
Physical addressPhysical address
-
memory address
-
All memory is uniformly addressed
-
Also known as: absolute address, real address
8.1.3 Dynamic loading
The process is not loaded before it is called, it is loaded when it is used
Does not require special support from the operating system, and can be realized through programming
Dynamic Link Libraries for Windows
8.1.4 Dynamic linking
Dynamic linking : those object files that make up the program are not linked until the program is about to run
-
Linking with various libraries is deferred until execution time
-
Need dynamic loading technical support
-
OS support required
8.1.5 Address positioning
Address binding (relocation): When the program is loaded into memory, the corresponding address in the program
The process of converting an address to an absolute address in memory
Instructions and data are bound to memory addresses in three distinct stages:
Compile time ( Compile time )
-
Can generate absolute code if memory location is known
-
If the start position changes, the code needs to be recompiled
Load time ( Load time )
- If the storage location is not known at compile time, relocatable ( relocatable ) code must be generated
Execution time ( Execution time )
-
Address binding can be deferred until runtime if memory can be moved while the process is executing
-
Requires hardware support for address mapping (e.g. base address and length-limited registers)
8.2 Memory allocation and recovery
8.2.1 Contiguous memory allocation
Allocate a contiguous memory space for a user program,
Classification:
- single contiguous allocation
- fixed partition allocation
- variable partition allocation
single contiguous allocation
Allocation method: In a single program environment, only one user program is installed, that is, the user space of the entire memory is exclusively occupied by this program
-
Memory allocation management is very simple, low memory utilization
-
For single-user, single-tasking OS
-
CP/M、MS-DOS、RT11
No memory protection measures taken
-
save hardware
-
The solution is feasible
fixed partition allocation
The earliest and simplest storage management method that can run multiple programs.
Divide the allocatable main memory space into several contiguous areas in advance, called a partition.
Each partition can be the same or different size. But the partition size is fixed, each partition can install one and only one program
Memory allocation: if there is a free partition, it is allocated to the process
The method of partitioning:
-
The same partition size: lack of flexibility: the program is too large, it cannot be installed; the program is too small, a waste of memory
There are also merits, which can be used in some specific occasions: such as a computer controlling multiple objects of the same size
-
The size of the partitions is different: multiple small partitions, a moderate amount of medium partitions, and a small number of large partitions
variable partition allocation
Partitions (holes, holes) - available memory blocks, partitions of different sizes are distributed throughout the memory
When a process comes in, it allocates memory from a partition large enough to hold it.
The operating system contains the following information:
-
Allocated Partition - Allocated Partition Table
-
empty partition - free partition table
Allocation Algorithm:
- First-fit Algorithm First-fit: Allocation first finds a suitable free partition
- Best-fit algorithm Best-fit: Search the entire list and find the smallest partition suitable for the condition for allocation
- Worst-fit algorithm Worst-fit: Search the entire list to find the largest partition for allocation
memory recovery
Four possible situations:
- Before and after the reclaimed partition are free areas, merge these three partitions into one large free area
- The back and forth of the reclaimed partition are used areas, at this time only the current partition can be reclaimed
- The previous partition of the reclaimed partition is free, and the latter partition is the used area, then merge the current partition and the previous partition into a large free area
- The latter partition of the reclaimed partition is free, and the former partition is the used area, then merge the current partition and the latter partition into a large free area
fragments
Outer fragmentation – the entire available memory space can be used to satisfy a request, but it is not contiguous
Internal Fragmentation – The allocated memory may be a bit larger than the requested memory, the difference between the two is inside the partition, but it is not used
Can be compacted to reduce outer debris
-
Combine small chunks of free memory into one big chunk.
-
Compaction is only possible when relocation is dynamic, and compaction occurs at execution time
8.2.2 Discrete memory allocation
page management
The process physical address space may not be contiguous
- If physical memory is available, it will be allocated to the process
Divide physical memory into fixed-size blocks called frames
-
power of 2
-
Early: 512 bytes to 8192 bytes
-
Now: 4K-64K
The logical memory is also divided into blocks of the same size, called pages (Page)
The system keeps a record of all idle frames
To run a program with N pages size, you need to find N empty frames to load the program
Create a page table to convert logical addresses to physical addresses
Fragmentation within existence
address translation mechanism
Paging addresses are divided into:
- Page number: It contains the base address of each page in the physical address, which is used as the index number of the page table
- Page offset: Combined with the base address, it is used to determine the physical memory address sent to the memory device
Hardware support for paging:
memory protection
Simple method:
compare the page number with the page table length limit register (PRLR)
The protection of the memory is realized by the protection bit connected with each frame
Valid-invalid bits are appended to each entry in the page table:
-
"Valid" means that the associated page is in the logical address space of the process and is a valid page
-
"invalid" means the page is not in the logical address space of the process
Valid and invalid bits in the page table
8.3 Page Table Structure Management
8.3.1 Two-Level Page Tables
A logical address is divided into (on a 32-bit machine with a 4K page size)
-
a 20-digit page number
-
A 12-bit page offset
Since the page where the page table is located is also paged, the page number is further divided into:
-
a 10-digit page number
-
A 10-bit page offset
The logical address is thus represented as follows:
address translation mechanism
8.3.2 Hash page table
Typically address space > 32 bits
Virtual page numbers are hashed into a page table. Each entry in such a page table consists of a linked list element that hashes to the same location.
The virtual page number is compared with each element in the linked list to find a match. If there is a match, the corresponding physical frame is fetched.
8.3.3 Reverse page table
There is one entry for each real memory page or frame.
Each entry holds the virtual address of the page at the real memory location, along with information including the process that owns the page.
Inverted page table mechanism:
8.4 Segmented memory management
A memory management mechanism that supports user views.
A program is a collection of segments, a segment is a logical unit,
Segmentation Mechanism (1)
A logical address is a collection of two vectors:
<segment-number, offset>
Segment table - maps two-dimensional user addresses, each table entry includes:
base address - contains the starting address of the segment physical address in memory
limit length - specifies the segment Length
The Segment Table Base Register (STBR) points to the address of the segment table in memory The
Segment Table Limit Register (STLR) indicates the number of segments used by a program Segment
number s is valid if s < STLR
Segment hardware:
Segmentation Mechanism (2)
Since segments vary in length, memory allocation is a dynamic storage-allocation problem
memory allocation
- first/best fit
- external debris problem
reset
- dynamic
- Executed by segment table
shared
- shared segment
- same segment number
Segmentation Mechanism (3)
protection, the entries of each segment table are:
-
valid bit = 0 => illegal segment
-
read/write/execute rights
Protection bits are associated with segments, code sharing at the segment level
8.5 Segment page management
Combination of Segmentation and Paging Principles
Divide the user program into several segments first, then divide each segment into several pages, and assign a segment number to each segment
Logical address: <segment number, page number, page offset>
Such a combination method brings: there is inner fragmentation, but there is no outer fragmentation
Example of Intel 386 using segment paging:
8.6 Memory Expansion
What to do if the memory space is not enough:
- compaction (dynamic partitioning)
- overlay technology
- switching technology
- Virtual memory technology (detailed introduction in Chapter 9)
8.6.1 Overlay technology
Solve the problem: the program size exceeds the total physical memory
program execution
-
Keep only those instructions and data in memory that are needed at any one time
-
Different parts of the program replace each other in memory
The overlay structure is declared by the programmer without special support from the operating system
Programming overlay structures is complex
Applied to earlier operating systems
8.6.2 Switching technology
In a multi-programming environment, on the one hand, some processes in the memory are blocked because an event has not occurred, but it takes up a large amount of memory space, and sometimes even all processes in the memory are blocked. Situations that force the CPU to stall and wait
On the other hand, there are many jobs waiting on the external memory, and cannot enter the memory to run due to lack of memory.
Waste resources and reduce system throughput.
A process can be temporarily swapped (swap) to a backup area outside of memory, and then can be swapped back into memory to continue execution
Spare area—is a fixed, fast disk large enough to hold copies of all user memory images; direct access to these memory images must be provided
The idea of swapping technology: When the memory space is tight, some processes are swapped out to the external memory, and some processes in the external memory that are ready to run are swapped into the memory to run.