CH 9
CH 9
Operating System Concepts – 10th Edition, Silberschatz, Galvin and Gagne ©2018
Observations
n In many cases, not all of the logical address space of
a process is accessed during the life of a process:
l 80-20 rule: only, say, 20% is used code while
the rest is error handling etc, which is rarely
used.
l Arrays, lists, tables - many times are allocated
to be able to contain the maximal need, which
is rarely needed.
n And, of course, not all of the logical memory space is
needed at the same time ( loops, recursive functions,
etc.
2
Operating System Concepts – 10th Edition 8.2 Silberschatz, Galvin and Gagne ©2018
Virtual Memory
n Virtual memory is a scheme that allows a process to
execute while it is only partially in memory:
l A process is no longer constrained by the
amount of physical memory available.
Process size can exceed the physical
memory size
l More programs could be run at the same time,
increasing the utilization and throughput of the
machine.
l Less I/O is required to load each program and
start running it, so each program would start
to run faster.
3
Operating System Concepts – 10th Edition 8.3 Silberschatz, Galvin and Gagne ©2018
Fetch Policy
n Determines when a page should be brought into main
memory. Two common policies:
l Demand paging only brings pages into main memory
when a reference is made to a location on the page
(paging on demand only).
many page faults when a process is started but should
decrease as more pages are brought in.
It uses lazy swapper (never swap pages into memory
unless they are needed)
l Prepaging brings in more pages than needed.
It is more efficient to bring in pages that reside
contiguously on the disk.
efficiency not definitely established: the extra pages
brought in are “often” not referenced.
l Pager is used instead of8.4swapper 4
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Demand Paging
n Initially, valid bit is set to 0 for all page table entries.
n During address translation, if valid bit in page table entry is 0,
a page fault trap occurs.
n Some commands makes multiple reference to memory
l Example: Add A, B ( Fetch Instruction ADD, Fetch A,
Fetch B, Add A,B, Store Sum into C)
frame valid
0 A 0
0 3 v
1 B 1
1 i
2 C 2
2 5 v
3 D 3 A
3 i
4 E 4
4 7 v A B
5 F 5 C
5 i C D E
6 G 6
6 i
7 E
logical memory
page table 5
Operating System Concepts – 10th Edition 8.5 physical memory Silberschatz, Galvin and Gagne ©2018
Page Fault
n Page fault can occur at any memory reference.
n Trap to the operating system (save registers and state).
n Operating system looks at the process PCB to decide whether:
l Not in logical address space abort the process.
l Page is not in memory continue:
n Allocate a free frame in memory.
n Schedule a disk read of the page to the free frame. This process now in
waiting state. Say it is P0
n Allocate the CPU to some other process. This process now runs. Say P1
n Interrupt from the disk (I/O completed).
n Save registers and state for currently running process (P1).
n Update original process (P0) page table (the desired page is in memory).
n Move original process (P0) to ready queue - will wait for CPU allocation.
n Restore registers and state then restart the trapped instruction of P0.
6
Operating System Concepts – 10th Edition 8.6 Silberschatz, Galvin and Gagne ©2018
Page Fault-From BOOK
Operating System Concepts – 10th Edition 8.7 Silberschatz, Galvin and Gagne ©2018
Page Fault (Cont.)
Operating System Concepts – 10th Edition 8.8 Silberschatz, Galvin and Gagne ©2018
Page Fault (Cont.)
Operating System Concepts – 10th Edition 8.9 Silberschatz, Galvin and Gagne ©2018
Performance of Demand Paging
n Page fault rate 0 p 1
l if p = 0, no page faults
l if p = 1, every reference results in a page fault.
n Effective Access Time:.
EAT =
(1 - p) effective_memory_access +
p ( page fault overhead + [swap out] + swap in + restart
overhead )
12
Operating System Concepts – 10th Edition 8.12 Silberschatz, Galvin and Gagne ©2018
Performance Example (cont).
So: What is a good page fault rate?
Effective Access Time = 107* (1-p) +( 100,000+
10,000,000+0.5*10,000,000+20,000+205)*p
EAT = 107 *(1-p) + 15,120,205*p.
Suppose p is 1% EAT = 151,309
Talking about a slowdown of 1414!!!
13
Operating System Concepts – 10th Edition 8.13 Silberschatz, Galvin and Gagne ©2018
When Will Paging Work?
OS allocates memory frames to programs on demand (page
fault)
If no frame is available, then OS needs to evict another page to
free a frame
Which page should be evicted?
• A page “cache” miss is similar to a TLB miss or a memory
cache miss
• However, a miss may require accessing the disk
• So miss handling can be very expensive
• Disk access times are at least 1000x memory access times
Paging can only work if it occurs rarely
Paging schemes depend on locality of reference
• Spatial locality
• Programs tend to use a small fraction of their memory, or
• Memory accesses are near memory accessed recently
• Temporal locality
• Programs use same memory over short periods of time, or
• Memory accessed recently will be accessed again
Programs normally have both kinds of locality
• Hence, overall cost of paging is not very high bcz of locality
Operating System Concepts – 10th Edition 8.14 Silberschatz, Galvin and Gagne ©2018
Page Replacement Algorithms
It is important to reduce page misses
Several Algorithms
• The Optimal Page Replacement Algorithm
• First In First Out (FIFO)
• Not Recently Used (NRU)
• Second Chance / Clock
• Least Recently Used (LRU)
• Working Set/WSClock
• Page Fault Frequency
Operating System Concepts – 10th Edition 8.15 Silberschatz, Galvin and Gagne ©2018
Page Replacement
n What happens if there is no free frame to allocate? We take a frame
(victim) that is presently being used by another process & allocate!
n Here comes page replacement - find a page in memory that is “not
that used” and swap it out. A free frame was just created.
l Note that the same page may be brought into memory several times
over the life of a process.
l Use a modified bit (dirty bit) to reduce overhead of page transfers -
only modified pages need to be written to disk, unmodified pages can
be discarded.
n Page replacement completes the separation between logical
memory and physical memory. Large virtual memory can be
provided on a smaller physical memory.
1. swap out
2. Modify to invalid i
v f victim
4. Modify to f, valid f
3. swap in
16
Operating System Concepts – 10th Edition 8.16 Silberschatz, Galvin and Gagne ©2018
Page Replacement Algorithms
n Goal
l Lowest page fault rate.
n Evaluation
l Usually by running it on reference stings and
counting the number of page faults.
n Adding more memory
l Should hopefully lead to lower page fault rate.
Number of page faults
Number of frames
17
Operating System Concepts – 10th Edition 8.17 Silberschatz, Galvin and Gagne ©2018
First In First Out (FIFO)
n The victim is the oldest page in memory.
n Assume 4 frames and the following reference string:
1 2 3 2 4 3 5 1 5 2 4 5
18
Operating System Concepts – 10th Edition 8.18 Silberschatz, Galvin and Gagne ©2018
First In First Out (FIFO)
n Replace the oldest page in memory.
n Assume 4 frames and the following reference string:
1 2 3 2 4 3 5 1 5 2 4 5
1 1 1 1 1 1 5 5 5 5 5 5
2 2 2 2 2 2 1 1 1 1 1
3 3 3 3 3 3 3 2 2 2
4 4 4 4 4 4 4 4
f f f f f f f
•Easy algorithm to understand and implement:
•Put with each page the time it was brought into a physical frame
•Select as a victim page to be replaced, the oldest page
•Implement with a FIFO queue of references to pages
Will adding memory always improve things?
19
Operating System Concepts – 10th Edition 8.19 Silberschatz, Galvin and Gagne ©2018
First In First Out (FIFO)
n FIFO suffers from the following anomaly which is
called Belady’s anomaly.
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 5 5 5 5 4 4
2 2 2 2 2 2 1 1 1 1 5
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3
f f f f f f f f f f
20
Operating System Concepts – 10th Edition 8.20 Silberschatz, Galvin and Gagne ©2018
First In First Out (FIFO)
n FIFO suffers from the following anomaly which is
called Belady’s anomaly.
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 5 5 5 5 4 4
2 2 2 2 2 2 1 1 1 1 5
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3
f f f f f f f f f f
1 1 1 4 4 4 5 5 5 5 5 5
2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
f f f f f f f f f
21
Operating System Concepts – 10th Edition 8.21 Silberschatz, Galvin and Gagne ©2018
FIFO Example (3 frames) Study it yourself
* * * * * * * * * * * * * * *
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
So, 15 page faults for this reference string, FIFO, & 3 frames
Operating System Concepts – 10th Edition 8.22 Silberschatz, Galvin and Gagne ©2018
FIFO Implementation
Keep list of all pages in memory
Problem
• The oldest page may be needed again soon
• Some page may be important throughout
execution
• When it gets old, replacing it causes page fault
• Suffers from Belady’s Anomaly
•Faults might increase when algorithm is given
more memory!
Operating System Concepts – 10th Edition 8.23 Silberschatz, Galvin and Gagne ©2018
Belady’s Anomaly
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 5 5 5 5 5 5
f f f f f f
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7
0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 5 5 5 5 4 3
4 4 4 4 4 4 3 3 4
f f f f f f f f
LRU or OPT does not suffer from Belady’s anomaly:
set of pages in memory for n frames is always a subset of
the set of pages that would be in memory with n+1 frames.
32
Operating System Concepts – 10th Edition 8.32 Silberschatz, Galvin and Gagne ©2018
LRU Example (3 frames)
Study it yourself
History of page references
* * * * * * * * * * * *
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
So, 12 page faults for this reference string, LRU, & 3 frames
Operating System Concepts – 10th Edition 8.33 Silberschatz, Galvin and Gagne ©2018
Frame Allocation
n Minimum number of frames in memory per process
l Architecture dependent: All of the pages needed for
any possible instruction need to be in memory for this
instruction to be executed.
l Example: the IBM 370 move block operation will need
up to 6 pages in memory at the same time in order to
execute (two for the instruction, two for the source
and two for the destination).
n Equal allocation
l Each process gets the same number of frames. Divide
the frames equally between the processes.
n Proportional allocation
l Allocate frames according to size of process.
n Priority allocation
42
l Higher priority processes
Operating System Concepts – 10th Edition
8.42 get more frames.
Silberschatz, Galvin and Gagne ©2018
Global versus Local Frame Allocation
n With multiple processes competing for frames, Page replacement
algorithms can be classified into 2 categories:
l Global replacement
Allows a process to select a victim frame from the set of all
frames->a process can allocate other processes frames (means
that those can page fault).
Page fault rate does not depend only of the process behavior
but depends on other processes behavior.
l Local replacement
Number of frames allocated for each process can not be
changed
A victim frame is selected from the set of frames allocated to
this process.
The set of pages in memory for a process is affected by the
behavior of only that process not others
n Global replacement is more efficient overall and therefore is commonly
used. It increases throughput and also easier to implement.
n Local replacement prevents external influences on the page fault rate of
this process as long as the same number of frames is allocated. 43
Operating System Concepts – 10th Edition 8.43 Silberschatz, Galvin and Gagne ©2018
Thrashing
n Why does paging work in practice ?
l Because of locality of reference in practical programs.
n If a process does not have “enough” pages to capture this locality of
reference (varies from program to program and over time), the
resulting page fault rate will be very high.
l Careful selection of data and programming structures can
increase locality and lower page fault rate.
l Example:
Long ArrayOfObject[10000 ,10000];
for i = 1 to 10000
for j = 1 to 10000
A[i,j] = i*j;
l Do all programs use locality? Sure not
n Low CPU Utilization- ready state may be near empty but waiting for
page device is near full (waiting for frames to be free! Global
allocation frame scheme is used
The operating system will think that it needs to increase the
degree of multiprogramming (bring back swapped processes
into memory)
l This will make the problem even worse because of the increase
45
of page fault rate!!!
Operating System Concepts – 10 Edition
th 8.45 Silberschatz, Galvin and Gagne ©2018
Thrashing (cont.)
n Thrashing is caused by under allocation of the minimum number of
pages required by a process, forcing it to continuously page fault.
n Operating system can detect thrashing by evaluating the level of
CPU utilization as compared to the level of multiprogramming.
n It can be eliminated by reducing the level of multiprogramming.
n Thrashing - Operating System is busy swapping pages in and out:
l Not enough memory to capture locality for all processes:
size of locality for in-memory processes > total memory size.
l Processes don’t do much progress->CPU utilization is decreased.
Results in severe performance problems
CPU Utilization
A process is
thrashing if it
spends more
time paging (i.e.,
doing page
faults) than
executing
Degree of
Multiprogramming
46
Operating System Concepts – 10th Edition 8.46 Silberschatz, Galvin and Gagne ©2018
Thrashing (cont.)
n Thrashing - Operating System is busy swapping pages in and
out:
l Not enough memory to capture locality for all processes:
size of locality for in-memory processes > total
memory size.
n Solutions: BUY more Memory? Run more programs?
Suspend processes? Are we here or there?
CPU Utilization
Degree of
Multiprogramming
47
Operating System Concepts – 10th Edition 8.47 Silberschatz, Galvin and Gagne ©2018
Page Fault Frequency Scheme
n PFF = page faults / instructions executed
n if PFF rises above threshold, process needs more memory
l not enough memory on the system? Swap out.
n if PFF sinks below threshold, memory can be taken away
So with limited memory at most 128 page faults for the array
may have as many as 128 x 128 page data of the program (one per row)
faults = 16, 384 faults
49
Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.49
Page Size Considerations
n How to select a page size?
n Tradeoffs:
l Size of the page table - to keep it relatively small, we
need bigger pages).
l Memory utilization - better with smaller pages because
of less internal fragmentation.
l I/O time - latency / throughput characteristics call for
bigger pages.
l Capturing locality - better with smaller pages because of
better resolution.
n Historical trend is for bigger page sizes.
l Memory is cheap so we care less about utilization.
l Disk access advances in slower advance rate compared
with memory/CPU so I/O factor is most important. 50
Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 8.50
Q&A
n Consider a demand-paging system with the following time-measured
utilizations:
l CPU utilization 20%
l Paging disk 97.7%
l Other I/O devices 5%
n Which (if any) of the following will (probably) improve CPU utilization?
Explain your answer.
l a. Install a faster CPU.
l b. Install a bigger paging disk.
l c. Increase the degree of multiprogramming.
l d. Decrease the degree of multiprogramming.
l e. Install more main memory.
l f. Install a faster hard disk or multiple controllers with multiple
l hard disks.
l g. Add prepaging to the page fetch algorithms.
l h. Increase the page size. 51
Operating System Concepts – 10th Edition 8.51 Silberschatz, Galvin and Gagne ©2018
Q&A
n Answer: The system obviously is spending most of its time paging,
indicating over-allocation of memory.
l If the level of multiprogramming (degree of multi-programming) is
reduced resident processes would page fault less frequently and
the CPU utilization would improve.
l Another way to improve performance would be to get more
physical memory or a faster paging disk (back store swap device).
Pure Paging
Has advantages, but requires entire process to be in
main memory
Virtual memory
Adds capability of enabling process to run without
all pages resident in main memory
Some benefits
Programs can be larger than physical memory
Programmer views computer as having large
memory
More programs can be partially resident, potentially
increasing CPU utilization
Drawbacks
Complexity of implementation (O/S)
Performance penalties (e.g., page faults)
Operating System Concepts – 10th Edition 8.54 Silberschatz, Galvin and Gagne ©2018
Exercise
n Problem 10.11, page 367, solve for four frames only.
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6
Operating System Concepts – 10th Edition 8.55 Silberschatz, Galvin and Gagne ©2018