Upload
trananh
View
218
Download
1
Embed Size (px)
Citation preview
Operating Systems
IV. Memory Management
Ludovic [email protected], office 470
http://soc.eurecom.fr/OS/
@OS Eurecom
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Outline
Basics of Memory ManagementHardware ArchitectureMonoprogrammingMultiprogramming
Main Mechanisms
What about Windows and Linux?
2/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Memory Hierarchy
3/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Memory Access
CPU Memory
Address bus
Data bus
Write
Read
4/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Memory Protection
Goal
Prevent a process to access unauthorized memory
I Memory used by other processesI Memory used by Operating System
I Interrupt vector, interrupt service routines, kernel, services, etc.
Mechanisms
Dual Mode and MMU - Memory Management Unit
5/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Monoprogramming
6/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Multiprogramming: Issues
Process AdmittanceI OS estimates the required memory and allocates it
Dynamic Allocation
I A process may request additional memory space
I A process may release part of its memory space
Process terminationI OS must release all the allocated memory
7/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Memory Allocation
Main issuesI Keep track of memory allocations
I Return requested memory chunks as fast as possible
I Avoid fragmentation
Allocation unitI Smallest amount of continuous memory managed by the OS
I From a few bytes to several KBytesI Impact of this size?
Keeping track of allocation units
I Bitmaps
I Linked Lists
8/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Bitmaps vs. Linked Lists
9/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Allocation Algorithms
First Fit
The system scans the list along until a large enough continuousallocation is found
Next FitI Scanning begins at the last position where a free block has
been found
I Performs slightly worse than First Fit
Best FitI Scans all the list and takes the smallest free block that is
adequateI Performs worse than First Fit!
I Can you guess why?
10/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Hardware ArchitectureMonoprogrammingMultiprogramming
Allocation Algorithms (Cont.)
Worse FitI Searches for the largest free block
I Not very good either!
Quick FitI Separate lists for most usual sizeI Additional complexity of memory management
I Merging is expensive
I But very quick search!
11/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Outline
Basics of Memory Management
Main MechanismsSwappingVirtual MemorySegmentation and Paging
What about Windows and Linux?
12/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Swapping: Principle
13/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
When to Swap?Swapping is expensive!
I How much time at least is necessary to swap in a 1 GBprocess (e.g., Firefox) with a transfer rate of 500 MB /second (Typical transfer rate for a (very good) ssd)
I May have to perform a swap out before!
Swap out
I When?I Memory occupied over thresholdI A memory allocation request fails
I Which process to swap out?I Recently executed processes (RR
scheduling)I Processes with lower priorities
(Priority-based scheduling)
Swap in
I When a process isready to execute
I I/O completed
I When a largeamount of memoryfreed
14/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Logical vs. Physical Address Space
At compilation time, the exact memory location of a program maynot be known → Virtual Memory
⇒ Two address spaces
I Virtual address space vs. physical address space
I Logical / virtual address: address used at CPU level (i.e.,addresses generated at compilation time)
I Physical address: physical address of the RAM
Address binding (virtual → physical) done at execution time:Memory Management Unit
15/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Memory Management Unit
CPU Memory
Logical addresses
Data bus
Write
Read
MMUPhysical addresses
16/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Segmentation of Memory
I Segment = logical memory unit of variable lengthI Virtual segments mapped to physical memory segmentsI Memory address = segment number + an address within the
segment (= offset)
17/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Segmentation of Processes
A process is a collection of different types of data
I Code, stack, heap, etc.
→ Use of several segments per Process
18/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Data Sharing with Segmentation
19/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Limitations of Segmentation
Fragmentation
Solution: Using algorithms to select segments (e.g., best-fit,first-fit, etc.)
Cost of segment expansion!
If a process allocates more space in a segment and this segmentcannot be expanded, and there is free memory available elsewhere→ memory segment must be moved
1. Process is blocked
2. OS makes a memory copy → segment is moved to anotherlocation
3. Process is unblocked
⇒ Paging!20/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Basics of Paging
Paging allows the logical address space of a process to benon contiguous in physical memory
Physical memory
I All physical memory is cut into fixed-size blocks
I Physical memory includes swap partitions
I Logical memory: page
I Physical memory: frame
Virtual memory
I Address divided into two partsI Page number (p)I Page offset (d)
21/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
MMU with Paging
22/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Combining Segmentation and Paging
Intel 80386 Address Translation
23/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Segmentation and Page Faults
Memory protection
I Process switching: the OS updates the address table
I MMU detects addresses having no correspondence → trap
Reasons for segment / page faults
I The address is invalid i.e., outside of the process addressspace
I Process is stopped (segmentation fault)
I Segment / page has been swapped outI The OS must make a swap in operation
I A segment / page must first be swapped out if memory is full→ Page Replacement Algorithm
24/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Page Fault Replacement Algorithms
Issue: the page-fault rate should be as low as possible
I FIFO Page Replacement
I Optimal Page Replacement
I LRU Page Replacement
I LRU Approximation Page Replacement
I Counting-Based Page Replacement
I . . . : ongoing research work on this issue
25/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Hardware vs. OS Support
Might be system-dependent!
Mechanism Hardware or OS?
Address translation Hardware
Segment / page allocation OS
MMU configuration (choice ofactive tables, etc.)
OS
Segment / page fault Hardware detection, OSmanagement
Finally:Can you tell what are the main interests of MMUs for OS?
26/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
SwappingVirtual MemorySegmentation and Paging
Use of Memory in Programs
…Char * name;…name = (char ) ( malloc ( 20 * sizeof(char) ) );…name[0] = 'h';name[1] = 'i';name[2] = '!';name[3] = '\0'
Process 1
malloc()
page1 page3
Process 1
LibC
OS
Free mem
page2 page4
page1page2page3page4
RAM
brk()
MMU
27/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Outline
Basics of Memory Management
Main Mechanisms
What about Windows and Linux?
28/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Windows and Linux
Segments / Pages
I Linux and Windows: only pages for user processes
I Many Unix use both techniques
Copy and Write (fork())
I Frame is first shared
I If a write operation is performed, frame is duplicated
Background daemon
Invoked periodically: Page flushing, freeing unused memory
Memory mapped Files
A file can be mapped onto memory
29/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Windows and Linux (Cont.)
Data structures to describe process space
Windows LinuxTree structure, each node iscalled a Virtual Address De-scriptor
Linked lists (just like mostUNIX) of vm area structs
OS vs. users process virtual address spaces (x86, 32-bit mode)
I Higher part: kernel code, and lower part: user code
I Linux: 3GB for the process, 1GB for the kernel
I Windows: 2GB for both
I When switching processes, upper part remains the same
30/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Windows and Linux (Cont.)
OS vs. users process virtual address spaces (x86, 64-bit mode)
Windows Linux
I Support since March 2005(Windows XPProfessional x64 Edition).
I User processes/OS: 128TB of virtual addressspace (Since Windows8.1)
I Since kernel 2.4 (2001)
I User processes: 128 TBof virtual address space
31/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management
Basics of Memory ManagementMain Mechanisms
What about Windows and Linux?
Windows and Linux (Cont.)
Windows: Page replacement
Clock algorithm: Circular list of pages in memory, with the”hand” (iterator) pointing to the oldest page in the list
Linux: Page replacement
I Linux 2.2: NRU (Not Recently Used)I OS Scans through memory and evicts every page that wasn’t
accessed since the last scan
I Since kernel 2.4: LRU (Improved in 2.6: ”CLOCK-PRO”)I Counter is increased when the page is referencedI Counter is divided by 2 when it was not referenced
I kswapdI Awakes periodically (e.g., every 1 sec.)I Frees memory if enough is not available
32/32 Fall 2017 Institut Mines-Telecom Operating Systems - Memory Management