Operating Systems: Memory Management
Operating Systems: Memory Management
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
What is a memory?
Memory consists of a large array of bytes, each with its own
address.
Execution of an instruction.
Fetch an Instruction from memory, Decode the instruction,
operands are fetched(from memory or registers)
After the instruction is executed, results are stored back.
The memory unit(MU) sees the stream of addresses.
Memory unit does not know how these addresses are
generated.
We will learn how the addresses are generated by the running
program.
OPERATING SYSTEMS
Basic Hardware
Program must be brought (from disk) into memory and
placed within a process for it to be run
Main memory and registers are only storage CPU can
access directly
The data required for CPU must be made available in the
registers.
Register access in one CPU clock (or less)
Main memory can take many cycles, causing a stall
Cache sits between main memory and CPU registers
Speeds up memory access without any OS control
Protection of memory required to ensure correct
operation
OPERATING SYSTEMS
Basic Hardware
Protection of OS from its access by user processes
On multiuser systems user processes must be protected
from each other.
This protection must be provided by the hardware
because the operating system doesn’t usually intervene
between the CPU and its memory accesses.
Several hardware protection methods will be discussed.
Protection by using two registers, usually a base and a
limit.
OPERATING SYSTEMS
Basic Hardware
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to
be sure it is between base and limit for that user
OPERATING SYSTEMS
Hardware Address Protection
The base and limit registers can be loaded only by the operating
system.
Using special privileged instruction.
privileged instructions can be executed only in kernel mode.
OS runs in the kernel mode. Only OS can change the register
values.
This prevents other user programs to modify the register
values.
OPERATING SYSTEMS
Address Binding
Programs on disk as binary executable and are brought to main memory
for execution
Processes may be moved between the disk and memory during execution.
What are the steps involved in the program execution?
Most systems allow a user process to reside in any part of the physical
memory.
The address space of the computer may start at 00000 but the first
address of the user process need not be 00000
Addresses in the source program are generally symbolic (Ex. variable
count).
OPERATING SYSTEMS
Address Binding
A compiler typically binds these symbolic addresses to relocatable
addresses
“14 bytes from the beginning of this module”
The linkage editor or loader in turn binds the relocatable addresses to
absolute addresses
Ex. 74014
Each binding is a mapping from one address space to another.
OPERATING SYSTEMS
Memory-Management Unit (Cont.)
Static linking – system libraries and program code combined by the loader into
the binary program image
Dynamic linking –linking postponed until execution time
Small piece of code, stub, used to locate the appropriate memory-resident
library routine
Stub replaces itself with the address of the routine, and executes the routine
Operating system checks if routine is in processes’ memory address
If not in address space, add to address space
Dynamic linking is particularly useful for libraries
System also known as shared libraries
Consider applicability to patching system libraries
Versioning may be needed
OPERATING SYSTEMS
Static and Dynamic Linking
A program whose necessary library functions are embedded directly in
the program’s executable binary file is statically linked to its libraries
The main disadvantage of static linkage is that every program generated
must contain copies of exactly the same common system library functions
Dynamic linking is more efficient in terms of both physical memory and
disk-space usage because it loads the system libraries into memory only
once
Demo
$cc fork.c
size a.out
$cc -static fork.c
size a.out
THANK YOU
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Swapping
Lower address
higher address
OPERATING SYSTEMS
Contiguous Allocation (Cont.)
Multiple-partition allocation
Divide memory into several fixed-size partitions.
Each partition may contain exactly one process
Degree of multiprogramming is bounded by number of partitions.
when a partition is free, a process is selected from the input queue
and is loaded into the free partition
used by the IBM OS/360 operating system,but is no longer in use
Variable-partition sizes for efficiency (sized to a given process’ needs)
This scheme keeps a table indicating which parts of memory are
available/occupied
Hole – block of available memory; holes of various size are scattered
throughout memory
OPERATING SYSTEMS
Multiple-partition allocation (Cont.)
Advantages
1. No Internal Fragmentation
2. No restriction on Degree of Multiprogramming
3. No Limitation on the size of the process
Disadvantages
Causes External Fragmentation
Difficult to implement
OPERATING SYSTEMS
Dynamic Storage-Allocation Problem and Strategies
Example: Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and 125 KB
(in order), how would the first-fit, best-fit, and worst-fit algorithms place processes of size
115 KB, 500 KB, 358 KB, 200 KB, and 375 KB (in order)? Rank the algorithms in terms of how
efficiently they use memory.
Soln: Partitions First-Fit Partitions Worst-Fit
Partitions Best Fit
M1=300 KB P1
M1=300 KB
M1=300 KB Processes and their sizes
185KB M2=600 KB P3
P1=115 KB P2=500 KB
M2=600 KB P2 P3=358 KB P4=200 KB
M2=600 KB P2 242KB P5=375 KB
100KB
100KB M3=350 KB P4
M3=350 KB
M3=350 KB P4 150KB
150KB
M4=200 KB P4
M4=200 KB
M4=200 KB M5=750 KB P3
M5=750 KB P1
M5=750 KB P3 392KB P5 635KB P2
392KB P5 17KB 135KB
17KB M6=125 KB P1 M6=125 KB
M6=125 KB 10KB P5 must wait
P5 must wait
OPERATING SYSTEMS
Fragmentation
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Segmentation
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Segmentation
➢Segmentation is a memory-
management scheme that
supports this programmer view of
memory
➢Each segment has a name and a
length.
➢Addresses specify segment
name + offset within the segment
OPERATING SYSTEMS
Logical View of Segmentation
1 4
3
4 2
user space
physical memory space
OPERATING SYSTEMS
Segmentation Basics
Protection
With each entry in segment table associate:
validation bit = 0 illegal segment
read/write/execute privileges
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Paging
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
▪ Hit ratio – percentage of times that a page number is found in the TLB
▪ An 80% hit ratio means that we find the desired page number in the TLB 80% of
the time.
▪ Suppose that 10 nanoseconds to access memory.
• If we find the desired page in TLB then a mapped-memory access take 10 ns
• Otherwise we need two memory access so it is 20 ns i.e. if we fail to find the
page number in the TLB then we must first access memory for the page table and
frame number (10 ns) and then access the desired byte in memory (10 nanoseconds),
for a total of 20 ns (assuming that a page table lookup takes only one memory access).
▪ Effective Access Time (EAT)
EAT = 0.80 x 10 + 0.20 x 20 = 12 nanoseconds
implying 20% slowdown in access time
▪ Consider a more realistic hit ratio of 99%,
EAT = 0.99 x 10 + 0.01 x 20 = 10.1ns
implying only 1% slowdown in access time.
OPERATING SYSTEMS
Memory Protection
Shared code
One copy of read-only (reentrant) code shared among
processes (i.e., text editors, compilers, window systems)
Similar to multiple threads sharing the same process space
Also useful for interprocess communication if sharing of
read-write pages is allowed
Private code and data
Each process keeps a separate copy of the code and data
The pages for the private code and data can appear
anywhere in the logical address space
OPERATING SYSTEMS
Shared Pages Example
THANK YOU
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Memory structures for paging can get huge using straight-forward methods
Consider a 32-bit logical address space as on modern computers
Page size of 4 KB (212)
Page table would have 1 million entries (232 / 212)
If each entry is 4 bytes -> 4 MB of physical address space / memory for page
table alone
That amount of memory used to cost a lot
Don’t want to allocate that contiguously in main memory
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
OPERATING SYSTEMS
Hierarchical Page Tables
A logical address (on 32-bit machine with 1K page size) is divided into:
a page number consisting of 22 bits
a page offset consisting of 10 bits
Since the page table is paged, the page number is further divided into:
a 12-bit page number
a 10-bit page offset
Thus, a logical address is as follows:
where p1 is an index into the outer page table, and p2 is the displacement within
the page of the inner page table
OPERATING SYSTEMS
Address-Translation Scheme
The outer page table is still 234 bytes (16 GB) in size. And possibly 4 memory access to
get to one physical memory location
❑ The next step would be a four-level paging scheme, where the second-level outer
page table itself is also paged, and so forth.
❑ The 64-bit UltraSPARC would require seven levels of paging—a prohibitive number of
memory accesses— to translate each logical address. So, for 64-bit architectures,
hierarchical page tables are generally considered inappropriate.
OPERATING SYSTEMS
Hashed Page Tables
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Rather than each process having a page table and keeping track of all
possible logical pages, track all physical pages
One entry for each real page of memory
Entry consists of the virtual address of the page stored in that real
memory location, with information about the process that owns that
page
Decreases memory needed to store each page table, but increases time
needed to search the table when a page reference occurs
Use hash table to limit the search to one — or at most a few — page-
table entries
TLB can accelerate access
But how to implement shared memory?
One mapping of a virtual address to the shared physical address
OPERATING SYSTEMS
Inverted Page Table
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Virtual Memory
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Swap space I/O faster than file system I/O even if on the
same device
Swap space is allocated in larger blocks, less
management needed than file system
Copy entire process image to swap space at process load
time
Then page in and out of swap space
Used in older BSD Unix
OPERATING SYSTEMS
Demand Paging Optimizations (Cont.)
Demand page in from program binary on disk, but discard rather than
paging out when freeing frame
Used in Solaris and current BSD
Still need to write to swap space
Pages not associated with a file (like stack and heap) – anonymous memory
Pages modified in memory but not yet written back to the file system
Mobile systems
Typically don’t support swapping
Instead, demand page from file system and reclaim read-only pages (such as
code) from applications
if memory becomes constrained and demand page such data from file
system later if needed
THANK YOU
Chandravva Hebi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebi
Department of Computer Science
OPERATING SYSTEMS
Virtual Memory
Chandravva Hebi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Replace page that will not be used for longest period of time
9 is optimal for the example
How do you know this?
Can’t read the future
Used for measuring how well your algorithm performs
OPERATING SYSTEMS
Least Recently Used (LRU) Algorithm
Counter implementation
Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter
When a page needs to be changed, look at the counters to find smallest value
Search through table needed
Stack implementation
Keep a stack of page numbers in a double link form:
Page referenced:
move it to the top
requires 6 pointers to be changed
But each update more expensive
No search for replacement
OPERATING SYSTEMS
Use Of A Stack to Record Most Recent Page References
▪ LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
▪ Use of a Stack to Record Most Recent Page References
THANK YOU
Chandravva Hebi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebi
Department of Computer Science
OPERATING SYSTEMS
Virtual Memory
Chandravva Hebi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Counter implementation
Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter
When a page needs to be changed, look at the counters to find smallest value
Search through table needed
Stack implementation
Keep a stack of page numbers in a double link form:
Page referenced:
move it to the top
requires 6 pointers to be changed
But each update more expensive
No search for replacement
LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly
OPERATING SYSTEMS
Use Of A Stack to Record Most Recent Page References
▪ LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly
▪ Use Of A Stack to Record Most Recent Page References
OPERATING SYSTEMS
Allocation of Frames
Equal allocation – For example, if there are 100 frames (after allocating
frames for the OS) and 5 processes, give each process 20 frames
Keep some as free frame buffer pool
Proportional allocation – Allocate according to the size of process
Dynamic as degree of multiprogramming, process sizes change
si = size of process pi
S = si
m = total number of frames
si
ai = allocation for pi = m
S
OPERATING SYSTEMS
Priority Allocation
Local replacement – each process selects from only its own set of
allocated frames
More consistent per-process performance
But possibly underutilized memory
OPERATING SYSTEMS
Non-Uniform Memory Access
Chandravva Hebi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Virtual Memory
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]
OPERATING SYSTEMS
Memory Management
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Chandravva Hebbi
Department of Computer Science
OPERATING SYSTEMS
Slides Credits for all PPTs of this course
5 bits for page protection, 20 bits for page frame address, 4 bits
to select a paging file, and 3 bits that describe the page state.
V=0, T=0, P=0
OPERATING SYSTEMS
Linux Memory Management
The page allocator allocates and frees all physical pages; it can
allocate ranges of physically-contiguous pages on request
The allocator uses a buddy-heap algorithm to keep track of
available physical pages
Each allocatable memory region is paired with an adjacent
partner
Whenever two allocated partner regions are both freed up
they are combined to form a larger region
If a small memory request cannot be satisfied by allocating
an existing small free region, then a larger free region will be
subdivided into two partners to satisfy the request
OPERATING SYSTEMS
Splitting of Memory in a Buddy Heap
OPERATING SYSTEMS
Managing Physical Memory (Cont.)
Chandravva Hebbi
Department of Computer Science Engineering
[email protected]