ch7 Virtual Memory
ch7 Virtual Memory
ch7 Virtual Memory
Fall 2023
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Lecture 7: Virtual Memory
! Background
! Demand Paging
! Copy-on-Write
! Page Replacement
! Allocation of Frames
! Thrashing
! Memory-Mapped Files
! Allocating Kernel Memory
! Other Considerations
! Operating-System Examples
Operating System Concepts – 9th Edition 9.2 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Objectives
! To Describe Benefits of a Virtual Memory
System
! To Explain Concepts of Demand Paging,
Page-Replacement Algorithms, and
Allocation of Page Frames
! To Discuss Principle of Working-Set Model
! To Examine Relationship between Shared
Memory and Memory-Mapped Files
! To Explore how Kernel Memory is Managed
Operating System Concepts – 9th Edition 9.3 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Background
! Code needs to be in Memory to Execute, but Entire
Program Rarely Used
! Error code, unusual routines, large data structures
! Entire Program Code not Needed at Same Time
! Consider ability to Execute partially-loaded program
! Program no longer constrained by limits of physical
memory
! Each program takes less memory while running è more
programs run at the same time
4 Increased CPU utilization and throughput with no increase in
response time or turnaround time
! Less I/O needed to load or swap programs into memory
è each user program runs
th
Operating System Concepts – 9 Edition 9.4
faster Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Silberschatz,
Background (cont.)
! Virtual Memory – separation of user logical
memory from physical memory
! Only part of program needs to be in memory for
execution
! Logical address space can therefore be much
larger than physical address space
! Allows address spaces to be shared by several
processes
! Allows for more efficient process creation
! More programs running concurrently
! Less I/O needed to load or swap processes
Operating System Concepts – 9th Edition 9.5 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Background (cont.)
! Virtual Address Space – logical view of how
process is stored in memory
! Usually start at address 0, contiguous addresses
until end of space
! Meanwhile, physical memory organized in page
frames
! MMU must map logical to physical
! Virtual Memory can be Implemented via:
! Demand paging
! Demand segmentation
Operating System Concepts – 9th Edition 9.6 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Virtual Memory Larger than
Physical Memory
Operating System Concepts – 9th Edition 9.7 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Virtual-Address Space
! Usually design Logical Address
Space for Stack to start at Max
Logical Address and grow “down”
while heap grows “up”
! Maximizes address space use
! Unused address space between
two is hole
4 No physical memory needed until
heap or stack grows to a given new
page
Operating System Concepts – 9th Edition 9.8 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Virtual-Address Space (cont.)
! Enables sparse address spaces
with holes left for growth,
dynamically linked libraries, etc
! System libraries shared via
mapping into virtual address space
! Shared memory by mapping pages
read-write into virtual address
space
! Pages can be shared during
fork(), speeding process creation
Operating System Concepts – 9th Edition 9.9 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Shared Library Using Virtual Memory
Operating System Concepts – 9th Edition 9.10 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Demand Paging
! Could Bring Entire Process
into Memory at Load Time
! Or bring a Page into Memory
only when it is Needed
! Less I/O needed, no
unnecessary I/O
! Less memory needed
! Faster response
! More users
! Similar to paging system
with swapping
Operating System Concepts – 9th Edition 9.11 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Demand Paging (cont.)
! Page is needed Þ
reference to it
! Invalid reference Þ abort
! not-in-memory Þ Bring
to memory
! Lazy Swapper – never
swaps a page into memory
unless page will be needed
! Swapper that deals with
pages is a pager
Operating System Concepts – 9th Edition 9.12 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Basic Concepts
! With swapping, Pager Guesses which pages
will be used before swapping out again
! Instead, pager brings in only those pages into
memory
! How to determine that set of pages?
! Need new MMU functionality to implement
demand paging
! If pages needed are already memory
resident
! No difference from non demand-paging
Operating System Concepts – 9th Edition 9.13 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Basic Concepts (cont.)
! If page needed and not memory resident
! Need to detect and load page into memory from
storage
4Without changing program behavior
4Without programmer needing to change code
Operating System Concepts – 9th Edition 9.14 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Valid-Invalid Bit
! With each page table entry a valid–invalid bit
is associated
!v Þ in-memory – memory resident
! i Þ not-in-memory
Operating System Concepts – 9th Edition 9.16 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Page Fault
1. If there is a reference to a page, 1st reference
to that page will trap to OS: Page Fault
2. OS looks at table to decide:
! Invalid reference Þ abort
! Just not in memory Þ proceed with step 3
3. Find Free Frame
4. Swap page into frame via scheduled disk
operation
5. Reset tables to indicate page now in memory
(Set validation bit = v)
6. Restart instruction that caused page fault
Operating System Concepts – 9th Edition 9.17 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Steps in Handling a Page Fault
Operating System Concepts – 9th Edition 9.18 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Aspects of Demand Paging
! Extreme case – start process with no pages
in memory
! OS sets instruction pointer to first instruction of
process, non-memory-resident è page fault
! For every other process pages on first access
! Pure demand paging
Operating System Concepts – 9th Edition 9.19 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Aspects of Demand Paging (cont.)
! A given instruction could even access
multiple pages è multiple page faults
! Consider fetch and decode of instruction which
adds 2 numbers from memory and stores result
back to memory è significant performance drop
! Not a real case due to locality of reference
Operating System Concepts – 9th Edition 9.21 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Performance of Demand Paging
! Stages in Demand Paging (worse case)
1. Trap to OS
2. Save user registers and process state
3. Determine that the interrupt was a page fault
4. Check that page reference was legal and
determine location of page on disk
5. Issue a read from disk to a free frame:
1. Wait in a queue for this device until read request
is serviced
2. Wait for device seek and/or latency time
3. Begin transfer of the page to a free frame
Operating System Concepts – 9th Edition 9.22 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Performance of Demand Paging (cont.)
! Stages in Demand Paging (worse case)
6. While waiting, allocate CPU to some other user
7. Receive an interrupt from disk I/O subsystem (I/O
completed)
8. Save registers and process state for other user
9. Determine that interrupt was from disk
10.Correct page table and other tables to show page
is now in memory
11.Wait for CPU to be allocated to this process again
12.Restore user registers, process state, and new
page table, then resume interrupted instruction
Operating System Concepts – 9th Edition 9.23 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Performance of Demand Paging (cont.)
! Three Major Activities
! Service interrupt – careful coding means just several
hundred instructions needed
! Read page – lots of time
! Restart process – again just a small amount of time
Operating System Concepts – 9th Edition 9.26 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Copy-on-Write (cont.)
! In general, free pages are allocated from a
pool of zero-fill-on-demand pages
! Pool should always have free frames for
fast demand page execution
4Don’t want to have to free a frame as
well as other processing on page fault
! vfork()
! Virtualmemory fork() system call
! Has parent suspend and child using copy-on-
write address space of parent
Operating System Concepts – 9th Edition 9.27 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Before Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.28 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
After Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.29 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
What Happens if There is no Free Frame?
! Used up by Process Pages
! How much to allocate to each?
! Page Replacement
! Find some page in memory, but not really in
use, page it out
! Algorithm – terminate? swap out? replace page?
! Performance – want an algorithm which will
result in minimum number of page faults
! Same page may be brought into memory
several times
Operating System Concepts – 9th Edition 9.30 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Page Replacement
! Prevent Over-allocation of memory
! By controlling degree of multi-programming
! By modifying page-fault service routine to
include page replacement
! Use Modify (Dirty) bit to reduce overhead of
page transfers
! Only modified pages are written to disk
! Page Replacement completes Separation
between Logical memory and Physical memory
! Large virtual memory can be provided on a
smaller physical memory
Operating System Concepts – 9th Edition 9.31 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Need For Page Replacement
Operating System Concepts – 9th Edition 9.32 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Basic Page Replacement
1. Find Location of Desired Page on Disk
2. Find a Free Frame:
- If there is a free frame, use it
- If there is no free frame, use a page replacement
algorithm to select a victim frame
- Write victim frame to disk if dirty
3. Bring Desired Page into (newly) free frame;
update page and frame tables
4. Continue Process by Restarting Instruction
that caused trap
Note now potentially 2 page transfers for page
fault – increasing EAT
Operating System Concepts – 9th Edition 9.33 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Page Replacement
Operating System Concepts – 9th Edition 9.34 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Page and Frame Replacement Alg.
" Frame-Allocation Algorithm determines
! How many frames to give each process?
! Which frames to replace?
" Page-Replacement Algorithm
! Want lowest page-fault rate on both first access and re-access
" Evaluate algorithm by running it on a particular string of
memory references (reference string) and computing
number of page faults on that string
! String is just page numbers, not full addresses
! Repeated access to same page does not cause a page fault
! Results depend on number of frames available
" In all our examples, reference string of referenced page
numbers is: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
Operating System Concepts – 9th Edition 9.35 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Graph of Page Faults Versus No of Frames
Operating System Concepts – 9th Edition 9.36 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
First-In-First-Out (FIFO) Algorithm
! Reference string:
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
! 3 frames (3 pages can be in memory at a time per process)
! Can vary by reference string: consider
1,2,3,4,1,2,5,1,2,3,4,5
! Adding more frames can cause more page faults!
4 Belady’s Anomaly
! How to track ages of pages?
! Just use a FIFO queue
12
10
8
6
4
2
1 2 3 4 5 6 7
number of frames
Operating System Concepts – 9th Edition 9.38 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Optimal Algorithm
! Replace Page that will not be used for Longest
Period of Time (in future)
! 9 is optimal for this example
! How do you know this?
! Can’t read the future
! Used just as a Reference Model
! For measuring how well your algorithm performs
Operating System Concepts – 9th Edition 9.39 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Least Recently Used (LRU) Algorithm
! Use Past Knowledge rather than Future
! Replace Page that has not been Used in Most
Amount of Time
! Associate Time of Last Use with each page
! 12 faults – better than FIFO but worse than OPT
! Generally good algorithm and frequently used
! But How to Implement?
Operating System Concepts – 9th Edition 9.40 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
LRU Algorithm (cont.)
! 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
4Search through table needed
! Cons
4Writeto memory on every memory access L
4Needs to look into all counters for smallest value L
4Counter overflow is an issue L
Operating System Concepts – 9th Edition 9.41 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
LRU Algorithm (cont.)
! Stack Implementation
! Keep stack of page numbers in a double link-list
! Page referenced:
4Move it to top
4Requires 6 pointers to be changed
Operating System Concepts – 9th Edition 9.42 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Use Of a Stack to Record Most Recent Page
References
reference string
4 7 0 7 1 0 1 2 1 2 7 1 2
2 7
a b
1 2
0 1
7 0
4 4
stack stack
before after
a b
Operating System Concepts – 9th Edition 9.43 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
LRU Algorithm (cont.)
! Stack Algorithms
! An algorithm for which it can be shown that set of
pages in memory for n frames is always a
subset of set of pages that would be in memory
with n+1 frames
! Informally: Don’t have Belady’s anomaly
! Example of Stack Algorithms
! LRU
! OPT
Operating System Concepts – 9th Edition 9.44 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
LRU Approximation Algorithms
! LRU needs Special HW and still slow
! Reference Bit
! With each page associate a bit, initially = 0
! When page is referenced, bit set to 1
! Replace any with reference bit = 0 (if one exists)
4 We do not know the order, however
! Additional-Reference-Bits Algorithm
! Use multiple history bits
! OS shifts reference bits at intervals
! Example: consider 8 reference bits
4 00000000 è page has not been referenced in non of intervals
4 11111111 è page has been referenced in eight time intervals
4 11000000 è ?
Operating System Concepts – 9th Edition 9.45 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
LRU Approximation Algorithms
! Second-Chance Algorithm
! Generally FIFO, plus HW-provided reference bit
! Clock replacement
! If page to be replaced has
4Reference bit = 0 è replace it
4Reference bit = 1 then:
– Set reference bit 0, leave page in memory
– Replace next page, subject to same rules
Operating System Concepts – 9th Edition 9.46 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Second-Chance (CLOCK) Page-
Replacement Algorithm
reference pages reference pages
bits bits
0 0
0 0
next 1 0
victim
1 0
0 0
…
…
1 1
1 1
Operating System Concepts – 9th Edition 9.56 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Global vs. Local Allocation
! Global Replacement – process selects a
replacement frame from set of all frames;
one process can take a frame from another
! But then process execution time can vary greatly
! But greater throughput so more common
! Local Replacement – each process selects
from only its own set of allocated frames
! More consistent per-process performance
! But possibly underutilized memory
Operating System Concepts – 9th Edition 9.57 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Non-Uniform Memory Access
! So far all memory accessed equally
! Many systems are NUMA – speed of access to
memory varies
! Consider system boards containing CPUs and memory,
interconnected over a system bus
! Optimal performance comes from allocating
memory “close to” CPU on which thread scheduled
! And modifying scheduler to schedule thread on same
system board when possible
! Solved by Solaris by creating lgroups
4 Structure to track CPU / Memory low latency groups
4 Used my schedule and pager
4 When possible schedule all threads of a process and allocate all
memory for that process within
th
Operating System Concepts – 9 Edition 9.58
the lgroup
Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Thrashing
! If a Process does not have “Enough” Pages,
Page-Fault Rate is Very High
! Page fault to get page
! Replace existing frame
! But quickly need replaced frame back
! This leads to:
4Low CPU utilization
4OS thinking that it needs to increase degree of
multiprogramming
4Another process added to system
Operating System Concepts – 9th Edition 9.60 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Demand Paging and Thrashing
! Why does Demand Paging work?
! Locality Model
! Process migrates from one locality to another
! Localities may overlap
! Why does Thrashing Occur?
! S Size of Locality > Total Memory Size
! Limit effects by using local or priority page
replacement
Operating System Concepts – 9th Edition 9.61 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Locality in a Memory-Reference Pattern
Operating System Concepts – 9th Edition 9.62 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Working-Set Model
! D º working-set window º a fixed number of
page references
! WSSi (working set of Process Pi) =
total number of pages referenced in most
recent D (varies in time)
! If D too small è will not encompass entire
locality
! If D too large è will encompass several
localities
! If D = ¥ è will encompass entire program
Operating System Concepts – 9th Edition 9.63 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Working-Set Model (cont.)
! D = S WSSi º Total Demand Frames
! Approximation of locality
! if D > m Þ Thrashing
! Policy: if D > m, then suspend or swap out
one of processes
Operating System Concepts – 9th Edition 9.64 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Keeping Track of Working Set
! Approximate with Interval Timer + a Reference Bit
! Example: D = 10,000
! Timer interrupts after every 5000 time units
! Keep in memory 2 bits for each page
! Whenever a timer interrupts copy and sets
values of all reference bits to 0
! If one of bits in memory = 1 Þ page in working
set
! Why is this not completely accurate?
! Improvement=10 bits & interrupt every 1000 time units
Operating System Concepts – 9th Edition 9.65 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Page-Fault Frequency
! More Direct Approach than WSS
! Establish “acceptable” Page-Fault Frequency
(PFF) Rate and Use Local Replacement Policy
! If actual rate too low è process loses frame
! If actual rate too high è process gains frame
page-fault rate
increase number
of frames
upper bound
lower bound
decrease number
of frames
number of frames
Operating System Concepts – 9th Edition 9.66 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Working Sets and Page Fault Rates
! Direct Relationship between Working Set of
a Process and its Page-Fault Rate
! Working Set Changes over Time
! Peaks and Valleys over Time
Operating System Concepts – 9th Edition 9.67 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Memory-Mapped Files
! Memory-Mapped File I/O allows File I/O to be
treated as routine memory access by
mapping a disk block to a page in memory
! A File is initially Read using Demand Paging
!A page-sized portion of file is read from file
system into a physical page
! Subsequent reads/writes to/from file are treated
as ordinary memory accesses
! Simplifies and Speeds File Access by Driving
File I/O through memory rather than read()
and write() system calls
Operating System Concepts – 9th Edition 9.68 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Memory-Mapped Files (cont.)
! Also Allows Several Processes to Map same
File allowing Pages in Memory to be Shared
! But when does Written Data Make it to Disk?
! Periodically and/or at file close() time
! For example, when pager scans for dirty pages
Operating System Concepts – 9th Edition 9.69 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Memory-Mapped File Technique for all I/O
! Some OSes uses memory mapped files for
standard I/O
! Process can explicitly request memory
mapping a file via mmap() system call
! Now file mapped into process address space
Operating System Concepts – 9th Edition 9.70 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Memory-Mapped File Technique for all I/O
! Solaris: For standard I/O (open(),read(),
write(),close()), mmap anyway
! Butmap file into kernel address space
! Process still does read() and write()
4Copies data to and from kernel space and user space
! Uses efficient memory management subsystem
4Avoids needing separate subsystem
! COW can be used for read/write non-shared
pages
! Memory mapped files can be used for shared
memory (although again via separate system calls)
Operating System Concepts – 9th Edition 9.71 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Memory Mapped Files
1
2
3
1 4
2 3 5
3 6
4
5 6
6
1
process A 5 process B
virtual memory virtual memory
4
2
physical memory
1 2 3 4 5 6
disk file
Operating System Concepts – 9th Edition 9.72 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Shared Memory via Memory-Mapped I/O
process1 process2
shared memory-mapped
memory file
shared
memory
shared
memory
Operating System Concepts – 9th Edition 9.73 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Allocating Kernel Memory
! Kernel Memory Treated Differently from User
Memory
! Often Allocated from a Free-Memory Pool
! Kernel requests memory for structures of varying
sizes
! Some kernel memory needs to be contiguous
4Structures for device I/O
4Page tables
Operating System Concepts – 9th Edition 9.74 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Buddy System
! Allocates Memory from Fixed-Size Segment
consisting of Physically-Contiguous Pages
! Memory allocated using power-of-2 allocator
! Satisfies requests in units sized as power of 2
! Request rounded up to next highest power of 2
! When smaller allocation needed than is
available, current chunk split into two buddies of
next-lower power of 2
4Continue until appropriate sized chunk available
Operating System Concepts – 9th Edition 9.75 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Buddy System (cont.)
! For example, assume 256KB chunk
available, kernel requests 21KB
! Split into AL and AR of 128KB each
4One further divided into BL and BR of 64KB
– One further into CL and CR of 32KB each – one used
to satisfy request
Operating System Concepts – 9th Edition 9.76 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Buddy System Allocator
physically contiguous pages
256 KB
128 KB 128 KB
A A
L R
64 KB 64 KB
B B
L R
32 KB 32 KB
C C
L R
Operating System Concepts – 9th Edition 9.77 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Slab Allocator
! Alternate Strategy
! Slab is one or more physically contiguous pages
! Cache consists of one or more slabs
! Single cache for each unique kernel data
structure
!A cache for file objects, a cache for semaphores
! Each cache filled with objects – instantiations of
data structure
! When cache created, filled with objects
marked as free
Operating System Concepts – 9th Edition 9.78 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Slab Allocator (cont.)
! When structures stored, objects marked as
used
! If slab is full of used objects, next object
allocated from empty slab
! If no empty slabs, new slab allocated
! Benefits include no fragmentation, fast
memory request satisfaction
Operating System Concepts – 9th Edition 9.79 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Slab Allocation
kernel objects caches slabs
3-KB
objects
physically
contiguous
pages
7-KB
objects
Operating System Concepts – 9th Edition 9.80 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Further Readings
! Demand Paging Optimizations
! Slab Allocation in Linux
! Operating System Examples
! Windows
! Solaris
! Prepaging
! TLB Reach
Operating System Concepts – 9th Edition 9.81 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Slab Allocator in Linux
! For example process descriptor is of type struct
task_struct
! Approx 1.7KB of memory
! New task -> allocate new struct from cache
! Will use existing free struct task_struct
! Slab can be in three possible states
1. Full – all used
2. Empty – all free
3. Partial – mix of free and used
! Upon request, slab allocator
1. Uses free struct in partial slab
2. If none, takes one from empty slab
3. If no empty slab, create new empty
Operating System Concepts – 9th Edition 9.82 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Slab Allocator in Linux (cont.)
! Slab started in Solaris, now wide-spread for
both kernel mode and user memory in
various OSes
! Linux 2.2 had SLAB, now has both SLOB
and SLUB allocators
! SLOB for systems with limited memory
4Simple List of Blocks – maintains 3 list objects for
small, medium, large objects
! SLUB is performance-optimized SLAB removes
per-CPU queues, metadata stored in page
structure
Operating System Concepts – 9th Edition 9.83 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Shared Memory in Windows API
! First create a file mapping for file to be mapped
! Then establish a view of the mapped file in process’s
virtual address space
! Consider producer / consumer
! Producer create shared-memory object using memory
mapping features
! Open file via CreateFile(), returning a HANDLE
! Create mapping via CreateFileMapping() creating a
named shared-memory object
! Create view via MapViewOfFile()
! Sample code in Textbook
Operating System Concepts – 9th Edition 9.84 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Other Considerations -- Prepaging
! Prepaging
! To reduce the large number of page faults that
occurs at process startup
! Prepage all or some of the pages a process will
need, before they are referenced
! But if prepaged pages are unused, I/O and
memory was wasted
! Assume s pages are prepaged and α of the
pages is used
4Is cost of s * α save pages faults > or < than the cost
of prepaging
s * (1- α) unnecessary pages?
Operating System α near
4Concepts
th zero Þ prepaging
– 9 Edition 9.85 losesGalvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Silberschatz,
Other Issues – Page Size
! Sometimes OS designers have a choice
! Especially if running on custom-built CPU
! Page size selection must take into consideration
! Fragmentation
! Page table size
! Resolution
! I/O overhead
! Number of page faults
! Locality
! TLB size and effectiveness
! Always power of 2, usually in range 212 to 222 bytes
! On average, growing over time
Operating System Concepts – 9th Edition 9.86 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Other Issues – TLB Reach
! TLB Reach - amount of memory accessible from
TLB
! TLB Reach = (TLB Size) X (Page Size)
! Ideally, working set of each process stored in TLB
! Otherwise there is a high degree of page faults
! Increase Page Size
! This may lead to an increase in fragmentation as not all
applications require a large page size
! Provide Multiple Page Sizes
! This allows applications that require larger page sizes
opportunity to use them without an increase in
fragmentation
th
Operating System Concepts – 9 Edition 9.87 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Other Issues – Program Structure
! Program structure
! int[128,128] data;
! Each row is stored in one page
! Program 1
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i,j] = 0;
! Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
Operating System Concepts – 9th Edition 9.90 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
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
4Pages not associated with a file (like stack
and heap) – anonymous memory
4Pages 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) Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Operating System Concepts – 9th Edition 9.91
Windows
" Uses demand paging with clustering. Clustering brings in
pages surrounding the faulting page
" Processes are assigned working set minimum and
working set maximum
" Working set minimum is the minimum number of pages the
process is guaranteed to have in memory
" A process may be assigned as many pages up to its
working set maximum
" When the amount of free memory in the system falls below
a threshold, automatic working set trimming is
performed to restore the amount of free memory
" Working set trimming removes pages from processes that
have pages in excess of their working set minimum
Operating System Concepts – 9th Edition 9.92 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Solaris
" Maintains a list of free pages to assign faulting processes
" Lotsfree – threshold parameter (amount of free memory)
to begin paging
" Desfree – threshold parameter to increasing paging
" Minfree – threshold parameter to being swapping
" Paging is performed by pageout process
" Pageout scans pages using modified clock algorithm
" Scanrate is the rate at which pages are scanned. This
ranges from slowscan to fastscan
" Pageout is called more frequently depending upon the
amount of free memory available
" Priority paging gives priority to process code pages
Operating System Concepts – 9th Edition 9.93 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
Solaris 2 Page Scanner
8192
fastscan
scan rate
100
slowscan
Operating System Concepts – 9th Edition 9.94 Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023
End of Lecture 7
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013, Edited by H. Asadi, Fall 2023