Memory Management: Operating Systems Lecture Notes
Memory Management: Operating Systems Lecture Notes
Memory Management
Matthew Dailey
Some material Silberschatz, Galvin, and Gagne, 2002
Todays Lecture
Why memory management? We want to give the user the illusion of a large, contiguous address space But we also want to maximize the degree of multiprogramming and utilization of physical memory
Topics: Memory address binding Logical vs. physical address spaces Allocation Memory paging Segmentation
Physical addresses
But physical memory is shared among many processes
Relocation Allocation Paging Segmentation
C source
test1.c, test2.c gcc c test1.c; gcc c test2.c mylib, test1.o, test2.o gcc o test test.o test2.o lmylib -lm test
compiler
static library
compiler
object code
object code
linker
dynamic library executable
loader
memory image
./test
test, math library, standard C libraries
Reduces memory usage by sharing between processes Requires OS support for protection
OS
Process 1
The memory controller sees a stream of physical addresses. The memory management unit (MMU) does the conversion.
Process 2 Process 1 Process 2 Process 3
Logical View
Process 3
Physical View
Relocation
CPU generates logical addresses in the range 0 to max.
The MMU adds a base register (value=r) to the logical addresses to get physical addresses.
Physical address space is r+0 to r+max. e.g. with r=14000, a logical address of 346 gets mapped to the physical address 14346.
Allocation
When processes are created or swapped in, we must allocate space for them. The relocation register scheme only supports contiguous allocation:
OS stays resident in low memory To protect other processes, use a limit register with the relocation register
Contiguous allocation
Basic idea: keep list of available memory holes. When a new process arrives, find a hole for it.
OS process 5 OS process 5 OS process 5 process 9 process 8 OS process 5 process 9 process 10
process 2
process 2
process 2
process 2
Best fit:
Worst fit: allocate from largest hole possible Simulations show first fit and best fit are usually better than worst fit, but no clear winner.
External fragmentation:
Loading and releasing memory blocks can leave small holes spread over physical memory. It is possible to have enough memory to satisfy a request even though it is broken up into too-small pieces. This wasted space is called external fragmentation.
Physical memory broken into fixed-size frames. Logical memory broken into same-size pages. Logical addresses now consist of page number and offset. Page number p indexes a page table. Page table entry p contains the address f of page p in physical memory. Requires hardware support.
Can still have fragmentation?: internal yes, external no.
Example
4-bit addresses 2 bits for page number 2 bits for offset 4-byte pages
Process loading
Split process logical address space into pages. Find enough free frames for the process pages. If necessary, swap out an old process. Find a sufficient number of free frames. Copy process pages into their designated frames. Fill in the page table.
Process loading
If very small, page table can be stored in fast registers Usually, the page table is too big:
32 bit addresses, 4 KB pages: more than a million entries Needs to be stored in main memory Each memory access now requires two accesses. 1MB page table for a 1MB process is wasteful
page number p p
page offset
d 2 Page number field isi split into a page number and an offset within that page. 10 10 12 Now one memory access requires 3 accesses!
Tag
Value
Page #
Frame #
Tag
Value
Page #
Frame #
TLB Usage
The TLB stores a few page number / frame number pairs When CPU generates a logical address: TLB is searched in parallel for the page number tag If present, TLB hit
Frame number is used immediately to access memory.
Effective access time (EAT) = 0.8 x ( 20 ns + 100 ns ) + 0.2 x ( 20 ns + 100 ns + 100 ns ) = 140 ns Paging overhead is 40% (100 ns vs. 140 ns)
Segmentation
An alternative way of breaking up the logical address space User programs are broken into logical segments
Main program, function1, global heap, etc.
Each process has several variable-sized memory segments Hardware provides base and limit registers for each segment Packing problem simpler (smaller chunks) but external fragmentation still occurs
Solution: combine paging and segmentation
1 1 4
3 4
2 3
user space
Segmentation example