You are on page 1of 9

Operating Systems Operating Systems

Chapter 4c Learning Objectives


Virtual Memory •  The difference between a First-In First-Out
(FIFO), Least-Recently-Used (LRU), and
Optimal Page Replacement (OPT) policies

•  The concept of the working set and how it is


Virtual Memory used in memory allocation schemes
Demand Paging
Chapter 4

Chapter 4
•  The impact that virtual memory had on
multiprogramming

1 2

Operating Systems Operating Systems

Concept
Introduction
•  Evolution of virtual memory •  Virtual memory – separation of user logical
memory from physical memory.
–  Paged, segmented, segmented / demand paging
–  Foundation for current virtual memory methods
–  Only part of the program needs to be in memory
•  Improvement areas for execution
–  Need for placement of entire program in memory –  Logical address space can therefore be much
during execution larger than physical address space
–  Fragmentation –  Allows address spaces to be shared by several
processes
Chapter 4

Chapter 4

–  Allows for more efficient process creation

3 4

Operating Systems Operating Systems

Virtual Memory That is Larger than Virtual Memory


Physical Memory
•  Allows program execution even if not stored
Backing Store
entirely in memory

•  Advantages:
–  Job size not restricted to size of main memory
–  Memory used more efficiently
–  Allows an unlimited amount of multiprogramming
–  Eliminates external fragmentation and minimizes
Chapter 4

Chapter 4

internal fragmentation
–  Allows the sharing of code and data
5 6

1
Operating Systems Operating Systems

Virtual Memory (cont'd.)


•  Virtual memory can be implemented via TWO
•  Disadvantages: approaches:
–  Increased processor hardware costs
–  Increased overhead for handling paging
interrupts Virtual Memory
–  Increased software complexity to prevent
thrashing (rapid exchanging data on disk)
Demand Demand
Paging Segmentation
Chapter 4

Chapter 4
(Not cover in this course)

7 8

Operating Systems Operating Systems

Demand Paging
•  Allowed for wide availability of virtual memory
concept
–  Provides appearance of almost infinite or nonfinite
Virtual Memory: physical memory
Demand Paging –  Jobs run with less main memory than required in
paged memory allocation scheme
•  Disadvantage(s):
–  Increased overhead Solution: Swapping
caused by tables and page •  how and when pages
passed in memory
Chapter 4

Chapter 4

interrupts •  Depends on
–  Requires high-speed direct predefined policies
access storage device
9 10

Operating Systems Operating Systems

Transfer of a Paged Memory to


•  Bring a page into
memory only when it is Lazy swapper Contiguous Disk Space
needed •  never swaps a page
–  Less I/O needed into memory unless
page will be needed
–  Less memory needed
•  Swapper that deals
–  Faster response
with pages is a
–  More users pager

•  Page is needed ⇒ reference to it


Chapter 4

Chapter 4

–  invalid reference ⇒ abort


Backing Store
–  not-in-memory ⇒ bring to memory

11 12

2
Operating Systems Operating Systems

Page Table When Some Pages Are Not


Valid-Invalid Bit
in Main Memory
•  With each page table entry a valid–invalid bit •  Invalid à the page
is associated either is not valid
(not in the logical
(v ⇒ in-memory, i ⇒ not-in-memory) address space of
the process) or is B

D E
valid but is currently
•  Initially valid–invalid bit is set to i on all entries on the disk.
G H

•  Access to a page
Chapter 4

Chapter 4
marked invalid Backing Store

causes a page fault.


13 14

Operating Systems Operating Systems

Steps in Handling a Page fault Page Replacement Steps


Backing Store
•  Trap à result •  Is executed when one of the following events
•  During address translation, if valid–invalid bit
of the OS’s occur:
in page table entry
failure to –  Page fault occurs
bring theis I ⇒ page fault
–  No free frames in physical memory
desired page
into memory.
•  To reduce the overhead of swapping time
–  use modify bit (or dirty bit)
•  0 – page has not been modified
Chapter 4

Chapter 4

•  1 – page has been modified


–  If the page has not been modified since it was read
into memory, then don’t write the page to the disk.
15 16

Operating Systems Operating Systems

Page Replacement Steps Page Replacement Steps

1.  Find the location of the 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;

3. Bring the desired page into the (newly) free frame;


update the page and frame tables
Chapter 4

Chapter 4

Backing Store
4. Restart the process;
17 18

3
Operating Systems Operating Systems

Page Replacement Algorithm If the number of frames increases, then the


number of page faults decreases.
•  Page replacement algorithm should be
chosen based on performance – need an 16

Number of page fault


algorithm which will result in minimum number 14
12
of page faults;
10
8
•  Algorithm can be tested by using: 6

– Reference string 4
2
– Number of frames
Chapter 4

Chapter 4
1 2 3 4 5 6
Number of Frames
19 20

Operating Systems Operating Systems

Reference String Example 1a


•  If CPU accesses the following addresses: •  Reference String
1 4 1 6 1 6 1 6 1 6 1
0100 0432 0101 0612 0102 0103 0104
0101 0611 0102 0103 0104 0101 0610
•  Number of frames = 3
0102 0103 0104 0101 0609 0102 0105
1 4 1 6 1 6 1 6 1 6 1
Frame #0 1 1 1
•  If page size = 100 byte, reference string will be, Frame #1 4 4
Chapter 4

Chapter 4

1 4 1 6 1 6 1 6 1 6 1 Frame #2 6

–  Number of page fault = 3


21 22

Operating Systems Operating Systems


Ex
tr
a

Example 1b Self-Test 4c.1:

Given a reference string: 1 4 1 6 1 6 1 6 1 6 1


•  Reference String
If the number of frame is 2, define how many number of page faults
1 4 1 6 1 6 1 6 1 6 1 will be generated. Show your works.

Solution 1: 4 page faults


•  Number of frames = 1 1 4 1 6 1 6 1 6Simple
1 conclusion?
6 1
Frame #0 1 1 6 6
Frame #1 4 4 1 Number of page faults
1 4 1 6 1 6 1 6 1 6 1 depend on:
Solution 2: 3 page faults •  Number of frames
Chapter 4

Chapter 4

Frame #0 1 4 1 6 1 6 1 6 1 6 1 1 4 1 6 1 6 1 • 6 Which


1 frame
6 1to be
Frame #0 1 1 1 replaced (policies)
–  Number of page fault = 11 Frame #1 4 6
23 24

4
Operating Systems Operating Systems

Page Replacement Policies /


Algorithms •  Another Page Replacement Policies:

•  Policy to select page removal –  Random page replacement


–  Crucial to system efficiency •  Replace page at random

•  Page replacement polices –  Second Chance


–  First-In First-Out (FIFO) policy
•  Best page to remove is one in memory longest –  Counting Algorithm
–  Least Recently Used (LRU) policy •  Least Frequently Used (LFU)
Chapter 4

Chapter 4
•  Best page to remove is least recently accessed •  Most Frequently Used (MFU)
–  Optimal Page Replacement (OPT) policy

25 26

Operating Systems Operating Systems

First-In First-Out (FIFO) FIFO Page Replacement

•  Removes the page that in memory with the


longest of time;

•  Efficiency
–  Ratio of page interrupts to page requests
•  Not so good
Chapter 4

Chapter 4

•  FIFO anomaly
–  More memory does not lead to better performance

27 28

Operating Systems Operating Systems

Self-Test 4c.2: Self-Test 4c.3:

Reference String: 1 2 3 4 1 2 5 1 2 3 4 Reference String: 1 2 3 4 1 2 5 1 2 3 4


•  3 frames (3 pages can be in memory at a time per process) •  4 frames (4 pages can be in memory at a time per process)
•  How many page faults? •  How many page numbers?
Chapter 4

Chapter 4

29 30

5
Operating Systems Operating Systems

FIFO Illustrating Belady’s Anomaly Least Recently Used (LRU)


•  Belady’s Anomaly: Unexpected result for page •  Removes page least recently accessed
faults.
•  Or replace the page that has not been used for the
longest period of time.

•  Efficiency
–  Causes either decrease in or same number of
interrupts
Chapter 4

Chapter 4
–  Slightly better (compared to FIFO)

31 32

Operating Systems Operating Systems

LRU Page Replacement LRU Implementation


•  Problem: To determine an order for the frames
defined by the time of last use.
•  How to implement LRU algorithm?

•  Two implementations:

–  Counter
•  Every page entry has a counter;
•  Every time page is referenced through this entry,
Chapter 4

Chapter 4

copy the clock into the counter


•  When a page needs to be changed, look at the
counters to determine which are to change
33 34

Operating Systems Operating Systems

Use Of A Stack to Record The Most Recent


Page References
–  Stack
•  keep a stack of page numbers in a double link
form: (Header pointer & Tail pointer)
•  When a page is referenced:
–  move page to the top
–  Therefore, most recently used page is on top
while least recently used is always at the bottom.
•  No search for replacement
Chapter 4

Chapter 4

35 36

6
Operating Systems Operating Systems

Optimal Page Replacement (OPT) Optimal Page Replacement (OPT)

•  Replace the page that will not be used for the


longest period of time.

•  The lowest page faults rate of all algorithms.


Chapter 4

Chapter 4
37 38

Operating Systems Operating Systems

Page Replacement
•  Global Replacement:
–  Choose frames from all sets of frames in the system;
–  Process cannot control page-fault rate by itself;
Virtual Memory:
•  Local Replacement:
Demand Paging
–  Choose frames already allocated to the process;
–  Effect – a process may keep pages that are seldom
used while other process that need more frames has
a high page fault rate;
(Some Issues)
Chapter 4

Chapter 4

•  Generally, global replacement results in greater


system throughput and is therefore the more
39
common method. 40

Operating Systems Operating Systems

Thrashing …Thrashing
•  Why does this happen?
–  An excessive amount of page swapping or –  If CPU utilization is low, OS increase the degree of
paging activity (in & out) between main memory multiprogramming;
and secondary storage; –  The number of programs increases while the number
–  A process is thrashing if it spends more time on of frames remains the same;
paging than executing; –  Therefore, page fault will occur more frequently;
CPU Utilization

Thrashing

•  Consequences:
Chapter 4

Chapter 4

–  that leads to low CPU utilization;


–  inefficient operation of system; Degree of Multiprogramming
Main memory: DRAM Secondary storage: Hard disk
41 42

7
Operating Systems Operating Systems

Example :
How to prevent thrashing?
Page fault due to program structure
•  Program structure
Ø  Int[128,128] data;
Ø  Each row is stored in one page
Ø  Program 1
Two Techniques
for (j = 0; j <128; j++)
for (i = 0; i < 128; i++)
data[i,j] = 0;

128 x 128 = 16,384 page faults


Page-fault frequency Working-Set Model
Ø  Program 2
for (i = 0; i < 128; i++)
Chapter 4

Chapter 4
for (j = 0; j < 128; j++)
data[i,j] = 0;

128 page faults


43 44

Operating Systems Operating Systems

Page-Fault Frequency Page-Fault Frequency

•  Provide upper and lower bound of page-fault


Page Fault Rate

Upper bound
Increase number of frames
•  If page-fault rate > upper bound, allocate more
frames
•  If page-fault rate < lower bound, reduce frame Lower bound
allocation Decrease number of frames
Chapter 4

Chapter 4

Number of Frames

45 46

Operating Systems Operating Systems

Working-Set Model Working-Set Model

•  System decides:
–  Number of pages comprising working set;
–  Maximum number of pages allowed for a working
set;

•  The accuracy of a working-set depends on the


•  Based on the assumption of locality; selection of ∆ (its size)
•  Uses a parameter, ∆ , to define the working-set
window; –  If ∆ too small à not encompass the entire locality;
Chapter 4

Chapter 4

•  Working-Set à the set of pages in the most –  If ∆ too large à may overlap page several localities;
recent ∆ page references;
47 48

8
Operating Systems Operating Systems

Tutorial Summary
Consider the following page reference string:
•  Segmented / demand paged memory
1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6 –  Problems solved
•  Compaction, external fragmentation

How many page faults would occur for the


•  Virtual memory
following replacement algorithms, assuming 1,
–  Programs execute if not stored entirely in memory
3 and 5 frames? Remember that all frames are
–  Job’s size no longer restricted to main memory size
initially empty.
Chapter 4

Chapter 4
•  FIFO replacement.
•  LRU replacement.
•  OPT replacement.
49 50

Operating Systems Operating Systems

Virtual Memory (cont'd.)

(Page Map Table) (Segment Map Table)


Chapter 4

Chapter 4

51 52

You might also like