Osc Unit 5
Osc Unit 5
Operating System Concepts – 9th Edition 8.2 Silberschatz, Galvin and Gagne
Logical vs. Physical Address Space
The concept of a logical address space that is bound to a
separate physical address space is central to proper
memory management
Logical address – generated by the CPU; also referred to
as virtual address
Physical address – address seen by the memory unit
Logical address space is the set of all logical addresses
generated by a program
Physical address space is the set of all physical addresses
generated by a program
Operating System Concepts – 9th Edition 8.3 Silberschatz, Galvin and Gagne
Memory-Management Unit (MMU)
Hardware device that at run time maps virtual to
physical address
To start, consider simple scheme where the value in
the relocation register is added to every address
generated by a user process at the time it is sent to
memory
Base register now called relocation register
The user program deals with logical addresses; it
never sees the real physical addresses
Operating System Concepts – 9th Edition 8.4 Silberschatz, Galvin and Gagne
Dynamic relocation using a relocation register
Operating System Concepts – 9th Edition 8.7 Silberschatz, Galvin and Gagne
Schematic View of Swapping
Operating System Concepts – 9th Edition 8.9 Silberschatz, Galvin and Gagne
4.Limited resource, must allocate efficiently
Contiguous Allocation
Main memory must support both OS and user
processes
Limited resource, must allocate efficiently
Contiguous allocation is one early method
Main memory usually divide into two partitions:
Resident operating system, usually held in low
memory with interrupt vector
User processes usually , held in high memory
Each process contained in single contiguous
section of memory
Operating System Concepts – 9th Edition 8.12 Silberschatz, Galvin and Gagne
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’
needs)
Hole – block of available memory; holes of various size are
scattered throughout memory
When a process arrives, it is allocated memory from a hole large
enough to accommodate it
Process exiting frees its partition, adjacent free partitions
combined
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Operating System Concepts – 9th Edition 8.14 Silberschatz, Galvin and Gagne
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?
Operating System Concepts – 9th Edition 8.16 Silberschatz, Galvin and Gagne
Fragmentation
External Fragmentation – total memory space
exists to satisfy a request, but it is not
contiguous
Internal Fragmentation – allocated memory may
be slightly larger than requested memory; this
size difference is memory internal to a partition,
but not being used
Operating System Concepts – 9th Edition 8.18 Silberschatz, Galvin and Gagne
Fragmentation (Cont.)
Reduce external fragmentation by compaction
Shuffle memory contents to place all free
memory together in one large block
Compaction is possible only if relocation is
dynamic, and is done at execution time
Operating System Concepts – 9th Edition 8.19 Silberschatz, Galvin and Gagne
Paging :
Paging
Physical address space of a process can be noncontiguous;
process is allocated physical memory whenever the latter
is available
Avoids external fragmentation
Avoids problem of varying sized memory chunks
Divide physical memory into fixed-sized blocks called
frames
Size is power of 2, between 512 bytes and 16 Mbytes
Divide logical memory into blocks of same size called pages
Keep track of all free frames
To run a program of size N pages, need to find N free frames
and load program
Set up a page table to translate logical to physical
addresses
Backing store likewise split into pages
Still have Internal fragmentation
Operating System Concepts – 9th Edition 8.22 Silberschatz, Galvin and Gagne
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page
table which contains base address of each page in
physical memory
Page offset (d) – combined with base address to
define the physical memory address that is sent to
the memory page
unitnumber page offset
p d
m -n n
Operating System Concepts – 9th Edition 8.23 Silberschatz, Galvin and Gagne
Paging Hardware
Operating System Concepts – 9th Edition 8.24 Silberschatz, Galvin and Gagne
Paging Model of Logical and Physical Memory
Operating System Concepts – 9th Edition 8.25 Silberschatz, Galvin and Gagne
Free Frames
Operating System Concepts – 9th Edition 8.26 Silberschatz, Galvin and Gagne
Implementation of Page Table
Page table is kept in main memory
Page-table base register (PTBR) points to the
page table
Page-table length register (PTLR) indicates size
of the page table
In this scheme every data/instruction access
requires two memory accesses
One for the page table and one for the data /
instruction
The two memory access problem can be solved
by the use of a special fast-lookup hardware
cache called associative memory or translation
look-aside buffers (TLBs)
Operating System Concepts – 9th Edition 8.27 Silberschatz, Galvin and Gagne
Paging Hardware With TLB
Operating System Concepts – 9th Edition 8.28 Silberschatz, Galvin and Gagne
Segmentation
Memory-management scheme that supports user view of
memory
A program is a collection of segments
A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays
Operating System Concepts – 9th Edition 8.32 Silberschatz, Galvin and Gagne
User’s View of a Program
Operating System Concepts – 9th Edition 8.33 Silberschatz, Galvin and Gagne
Logical View of Segmentation
4
1
3 2
4
Operating System Concepts – 9th Edition 8.34 Silberschatz, Galvin and Gagne
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,
Operating System Concepts – 9th Edition 8.35 Silberschatz, Galvin and Gagne
Segmentation Hardware
Operating System Concepts – 9th Edition 8.36 Silberschatz, Galvin and Gagne
Chapter 9: Virtual Memory
Operating System Concepts – 9th Edition 9.40 Silberschatz, Galvin and Gagne
Background
Virtual memory – separation of user logical memory
from physical memory
Only part of the 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.41 Silberschatz, Galvin and Gagne
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.42 Silberschatz, Galvin and Gagne
Virtual Memory That is Larger Than Physical Memory
Operating System Concepts – 9th Edition 9.43 Silberschatz, Galvin and Gagne
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 (diagram on right)
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.44 Silberschatz, Galvin and Gagne
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
If page needed and not memory resident
Need to detect and load the page into memory from
storage
Without changing program behavior
Without programmer needing to change code
Operating System Concepts – 9th Edition 9.45 Silberschatz, Galvin and Gagne
Valid-Invalid Bit
With each page table entry a valid–invalid bit is
associated
(v in-memory – memory resident, i not-in-memory)
Initially valid–invalid bit is set to i on all entries
Example of a page table snapshot:
Operating System Concepts – 9th Edition 9.47 Silberschatz, Galvin and Gagne
Page Fault
Operating System Concepts – 9th Edition 9.48 Silberschatz, Galvin and Gagne
Thrashing
If a process does not have “enough” pages, the page-
fault rate is very high
Page fault to get page
Replace existing frame
But quickly need replaced frame back
This leads to:
Low CPU utilization
Operating system thinking that it needs to
increase the degree of multiprogramming
Another process added to the system
Operating System Concepts – 9th Edition 9.49 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition 9.50 Silberschatz, Galvin and Gagne
Working-Set Model
working-set window a fixed number of page references
Example: 10,000 instructions
WSSi (working set of Process Pi) =
total number of pages referenced in the most recent (varies
in time)
if too small will not encompass entire locality
if too large will encompass several localities
if = will encompass entire program
D = WSSi total demand frames
Approximation of locality
if D > m Thrashing
Policy if D > m, then suspend or swap out one of the
processes
Operating System Concepts – 9th Edition 9.51 Silberschatz, Galvin and Gagne
Keeping Track of the Working Set
Approximate with interval timer + a reference bit
Example: = 10,000
Timer interrupts after every 5000 time units
Keep in memory 2 bits for each page
Whenever a timer interrupts copy and sets the values
of all reference bits to 0
If one of the bits in memory = 1 page in working set
Why is this not completely accurate?
Improvement = 10 bits and interrupt every 1000 time
units
Operating System Concepts – 9th Edition 9.52 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 9th Edition 9.53 Silberschatz, Galvin and Gagne
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.54 Silberschatz, Galvin and Gagne
Page Replacement
Operating System Concepts – 9th Edition 9.55 Silberschatz, Galvin and Gagne
Basic Page Replacement
1. Find the location of the desired page on disk
3. Bring the desired page into the (newly) free frame; update
the page and frame tables
Operating System Concepts – 9th Edition 9.56 Silberschatz, Galvin and Gagne
Page Replacement
Operating System Concepts – 9th Edition 9.57 Silberschatz, Galvin and Gagne
Operating System Concepts – 9th Edition 9.58 Silberschatz, Galvin and Gagne
•The oldest page in memory is the one replaced.
Belady's Anomaly is a real-world phenomenon that can occur in certain page replacement
algorithms, leading to unexpected behavior where increasing the number of page frames can
result in more page faults. It has implications for system performance and resource
allocation, and while it cannot be completely eliminated, it can be minimized by using
certain algorithms and careful experimentation.
Operating System Concepts – 9th Edition 9.59 Silberschatz, Galvin and Gagne
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)
15 page faults
Operating System Concepts – 9th Edition 9.61 Silberschatz, Galvin and Gagne
Principle : Look for Right most element & use FIFO at the end if no element exist
•Replaces the page that will not be used for the longest duration in the future.
•Provides the best performance, but it is not practical as it requires future
knowledge of page references
Optimal Algorithm
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 System Concepts – 9th Edition 9.63 Silberschatz, Galvin and Gagne
Principle : Look for left most element
•Replaces the page that has not been used for the longest period of time.
Provides good performance, but it requires a significant overhead to implement and is not
always practical
Least Recently Used (LRU) Algorithm
Use past knowledge rather than future
Replace page that has not been used in the most
amount of time
Associate time of last use with each page
Operating System Concepts – 9th Edition 9.65 Silberschatz, Galvin and Gagne
Use Of A Stack to Record Most Recent Page References
Operating System Concepts – 9th Edition 9.66 Silberschatz, Galvin and Gagne
•Second Chance Algorithm: Based on the FIFO algorithm, but it gives a second chance
to a page before replacing it.
•It is more efficient than FIFO as it considers the recent usage of pages
•Not Recently Used (NRU): NRU is a page replacement algorithm used
in operating systems to determine which pages to replace.
•It divides pages into four categories based on their usage history and
selects a page for replacement from the category with the lowest priority.
NRU replaces a page from the lowest category, giving priority to pages that
have not been recently used.
Class 3 is given highest priority followed by class 2 , class 1 and last class 0