Memory Management
Operating Systems Course Fall 2017 Erfan Sharafzadeh Iran University of Science and Technology The Abstraction: Address Spaces
Interlude: Memory API
Agenda Mechanism: Address Translation
Segmentation
Ope n S s e s: Th e E y Pi (Rem . Ar a -Dus u d An e C. Ar a -Dus u)
2 The Abstraction: Address Spaces
Early Systems Now
3 Virtualizing the Memory: Goals
Transparency Efficiency
The OS should implement virtual 1. Time memory in a way that is invisible to 2. Space the running program. OS will need hardware support! Protection
Deliver the property of isolation among processes.
4 EVERY ADDRESS YOU SEE IS VIRTUAL
location of code : 0x1095afe50 location of heap : 0x1096008c0
location of stack : 0x7fff691aea64 5 Memory API
Stack H
➔ To what extent are they controlled by the OS or language runtime? ➔ What is their scope? ➔ What determines the size of each of them? ➔ What makes one faster?
Take a look: https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
6 Linux Memory Management
● The malloc() Call ● The free() Call ● Some Errors: ○ Segmentation fault! => YOU DID SOMETHING WRONG WITH MEMORY YOU FOOLISH PROGRAMMER AND I AM ANGRY. ○ Forgetting To Free Memory ○ Freeing Memory Before You Are Done With It => Dangling Pointer! ● Underlying OS Support ○ brk() ○ mmap()
7 Mechanism: Address Translation
● manage memory: keeping track of which locations are free and which are in use, and judiciously intervening to maintain control over how memory is used. ● Once again the goal of all of this work is to create a beautiful illusion: that the program has its own private memory, where its own code and data reside. Behind that virtual reality lies the ugly physical truth: that many programs are actually sharing memory at the same time, as the CPU (or CPUs) switches between running one program and the next.
8 Three Assumptions
● User’s address space must be placed contiguously in physical memory ● The size of the address space is not too big; specifically, that it is less than the size of physical memory. ● Each address space is exactly the same size.
9 An Example void func() { int x = 3000; // thanks, Perry. x = x + 3; // this is the line of code we are interested in }
128: movl 0x0(%ebx), %eax ;load 0+ebx into eax 132: addl $0x03, %eax ;add 3 to eax register 135: movl %eax, 0x0(%ebx) ;store eax back to mem
10 An Example: Process Address space
• Fetch instruction at address 128 • Execute this instruction (load from address 15 KB) • Fetch instruction at address 132 • Execute this instruction (no memory reference) • Fetch the instruction at address 135 • Execute this instruction (store to address 15 KB)
11 An Example: Physical Memory dynamic relocation: using base and bounds registers: physical address = virtual address + base
Bounds register help with protection
All of these need hardware support
Exception handling in OS
12 An Example: Hardware Support
13 Put it together: 1
14 Put it together: 2
15 Put it together: 3
16 Segmentation
What segmentation allows the OS to do is to place each one of (CODE, STACK & HEAP) segments in different parts of physical memory, and thus avoid filling physical memory with unused virtual address space.
17 Segmentation
ASIDE: THE SEGMENTATION FAULT! ● arises from a memory access on a segmented machine to an illegal address. ● Humorously, the term persists, even on machines with no support for segmentation at all. ● Or not so humorously, if you can’t figure why your code keeps faulting.
18 Segmentation
● A more complete hardware support:
● External fragmentation ○ Fitting algorithms: best-fit, worst-fit, first-fit, buddy
19 What’s left...
● Free space management
● Paging
● TLB
20