operating systems cmpsci 377 lecture 11: memory management
DESCRIPTION
Operating Systems CMPSCI 377 Lecture 11: Memory Management. Emery Berger University of Massachusetts, Amherst. Course Outline. Processes & Threads CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems. Today: Memory Management. Terminology - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/1.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science
Emery BergerUniversity of Massachusetts, Amherst
Operating SystemsCMPSCI 377
Lecture 11: Memory Management
![Page 2: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/2.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 2
Course Outline
Processes & Threads CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Distributed Systems
![Page 3: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/3.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 3
Today: Memory Management
Terminology Addresses Uniprogramming Multiprogramming Relocation Allocation
Policies Fragmentation Compaction
Swapping
![Page 4: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/4.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 4
Memory Management
Where in memory is executing process?
How do we allow multiple processes to share main memory?
What’s an address and how is one interpreted?
![Page 5: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/5.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 5
Background:Computer Architecture
Program executable on disk OS loads program into memory CPU fetches instructions & data
from memory while executing program
![Page 6: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/6.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 6
Memory Management:Terminology
Segment: chunk of memory assigned to process
Physical address: real address in memory
Virtual address: address relative to start of process’s address space
![Page 7: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/7.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 7
Where Do Addresses Come From?
Instruction & data addresses Compile-time:
Exact physical location in memory starting from fixed position k
Load-time: OS determines process’s starting
position, fixes up addresses Execution time:
OS can place address anywhere in physical memory
![Page 8: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/8.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 8
Uniprogramming
Only one program at a time) simplifies memory management
OS gets fixed region of memory (e.g., highest)
One process at a time Load at address 0 Executes in contiguous memory
Compiler generates physical addresses Max address = memory size – OS size OS protected from process by checking
addresses
![Page 9: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/9.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 9
Example: Uniprogramming
Simple – but no overlap of I/O, computation
![Page 10: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/10.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 10
Multiprogramming Requirements
Transparency No process aware memory is shared Process has no constraints on physical
memory Safety
Processes cannot corrupt each other Or the OS
Efficiency Performance not degraded due to
sharing
![Page 11: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/11.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 11
Relocation
Put OS in high memory
Process starts at 0 Max addr = memory
size – OS size Load process by
allocating contiguous segment for process
Smallest addr = base, largest = limit
![Page 12: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/12.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 12
Static Relocation
At load time, OS adjusts addresses in process to reflect position in memory
OS cannot move process after relocation
![Page 13: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/13.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 13
Dynamic Relocation
Hardware adds relocation register (base) to virtual address to get physical address
Hardware compares address with limit register (addr must be less than base) Test fails ) trap
![Page 14: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/14.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 14
Relocation, Pros & Cons Advantages
OS can easily move process during execution OS can allow process to grow over time Simple, fast hardware
Two special registers, add, & compare Disadvantages
Slows everything (add on every reference) Can’t share memory between processes
“Text” section Process limited to physical memory size Degree of multiprogramming limited
All memory of active processes must fit in memory
Complicates memory management
![Page 15: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/15.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 15
Relocation, Properties
Transparency Processes largely unaware of sharing
Safety Each memory reference checked
Efficiency Memory checks fast if done in
hardware But: if process grows, may have to be
moved (SLOW)
![Page 16: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/16.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 16
Memory Allocation
As processes enter system, grow & terminate, OS must track available and in-use memory
Can leave holes OS must decide where to put new processes
![Page 17: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/17.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 17
Memory Allocation Policies
First-fit: Use first hole in which process fits
Best-fit: Use smallest hole that’s large enough
Worst-fit: Use largest hole
What’s best?
![Page 18: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/18.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 18
Fragmentation
Fragmentation = % memory unavailable for allocation, but not in use
External fragmentation: Caused by repeated unloading &
loading Many small holes
No contiguous chunk large enough Internal fragmentation:
Space inside process allocations Unavailable to other processes
![Page 19: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/19.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 19
Compaction
Can make space available by compacting process space Eliminate holes
![Page 20: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/20.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 20
Compaction Example
Issues Amount of
memory moved
Size of created block
Other choices?
![Page 21: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/21.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 21
Alternative: Swapping
Swapping = copy process to disk, release all memory When process active, must reload Static relocation: same position(!) Dynamic relocation: ok
Compaction simplified if swapping supported But not supported in modern OS’s
Interaction with scheduling?
![Page 22: Operating Systems CMPSCI 377 Lecture 11: Memory Management](https://reader036.vdocuments.us/reader036/viewer/2022062407/56812d34550346895d92326d/html5/thumbnails/22.jpg)
UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 22
Summary
Processes must reside in memory to execute Generally use virtual addresses
Translated to physical addresses before accessing memory
Segmentation: Allows processes to share memory Expensive to grow over time
Swapping: Total being used by all can exceed main memory Increases context switch time