0% found this document useful (0 votes)
22 views37 pages

(OS'25) Lecture 5

Chapter 8 of 'Operating Systems: Internals and Design Principles' discusses virtual memory, focusing on the roles of the operating system in managing memory through fetch, placement, and replacement policies. It covers various algorithms for page replacement, including optimal, least recently used (LRU), FIFO, and clock algorithms, highlighting their advantages and disadvantages. The chapter also includes learning outcomes and references for further study on virtual memory principles and mechanisms.

Uploaded by

ziadsameh071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views37 pages

(OS'25) Lecture 5

Chapter 8 of 'Operating Systems: Internals and Design Principles' discusses virtual memory, focusing on the roles of the operating system in managing memory through fetch, placement, and replacement policies. It covers various algorithms for page replacement, including optimal, least recently used (LRU), FIFO, and clock algorithms, highlighting their advantages and disadvantages. The chapter also includes learning outcomes and references for further study on virtual memory principles and mechanisms.

Uploaded by

ziadsameh071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Operating Systems:

Internals and Design Principles, 6/E


William Stallings

Chapter 8
Virtual Memory – Part II
[OS Role]

Dave Bremer
Otago Polytechnic, N.Z.
©2008, Prentice Hall

*Lecture slides are modified by Dr. Ahmed Salah *


GENERAL NOTE
Hidden Slides are Extra Knowledge
Not Included in Exams
Roadmap
KNOW HOW the Operating System do its Main Jobs?

Solve competition on…

Shared
Memory CPU Resourc
e

Physical Virtual Single Critical


Multi CPU Deadlocks
Mem Mem CPU Sections
(CH.10) (CH.6)
(CH.7) (CH.8) (CH.9) (CH.5)
Agenda
• OS Roles
– Main Goal
– Fetch Policy
– Placement Policy
– Replacement Policy
• Intel MMU
• Questions (Credits to Dr. Ahmed Salah)
Learning Outcomes

• Summarize the principles of virtual memory as applied to caching


and paging.
• Describe the various OS mechanisms used to implement virtual
memory.
References
• Chapter 8
– Section 8.1 (H/W Roles)
– Section 8.2 (OS Roles)

• Stanford CS140
– Lec.5 Virtual Memory (H/W)

• Berkeley CS162
– Lec.13 Address Translation + Caching
Agenda
• OS Roles
– Main Goal
– Fetch Policy
– Placement Policy
– Replacement Policy
• Intel MMU
• Questions (Credits to Dr. Ahmed Salah)
OS Main Goal
• Minimize page faults

• No definitive best policy


Fetch Policy
• Determines when a page should be brought into memory
• Two main types:
1. Demand Paging
2. Prepaging
Fetch Policy
• Demand paging
– Only brings pages into main memory when a reference is made
to a location on the page
– Many page faults when process first started
• Prepaging
– Brings in more pages than needed, either at:
1. Process Start
2. Page Fault
– More efficient to bring in pages that reside contiguously on the disk
Agenda
• OS Roles
– Main Goal
– Fetch Policy
– Placement Policy
– Replacement Policy
• Intel MMU
• Questions (Credits to Dr. Ahmed Salah)
Replacement Policy
• Determines which page currently in memory is to be replaced
• Answer 3 questions:
1. When to replace?
1. RAM is Full
2. Process reaches its Max
2. Which page to be replaced?
• Page least likely to be referenced
• Predict future from the past (Based on: Principle of Locality)
• 5 different algorithms
3. From whom?
1. Local from process
2. Global from any other process
Replacement Policy
• Optimal Algorithm:
– Selects page for which time to the next reference is the longest
– Adv: BEST method (benchmark for others)
– DisAdv: Not feasible – need perfect knowledge of future
Ex. Trace [OPTIMAL]
P#: 2 3 2 1 5 2 4 5 3 2 5 2
Replacement Policy
Example:

• Optimal: 3 page faults after the frame


allocation has been filled.
Replacement Policy
• Least Recently Used (LRU):
– Replaces the page that has not been referenced for longest time
– By the principle of locality, should be least likely to be referenced
in the near future
– Adv: BEST feasible
– DisAdv: Difficult to implement
• Tag each page with the time of last reference.
• requires a great deal of overhead.
Ex. Trace [LRU]
P#: 2 3 2 1 5 2 4 5 3 2 5 2
Replacement Policy
Example:

• LRU: 4 page faults


• It does nearly as well as optimal.
Replacement Policy
• FIFO:
– Treats allocated frames as a circular buffer
– Pages are removed in round-robin style
– Adv: Simplest to implement
– DisAdv: first-loaded pages may be needed again very soon
Ex. Trace [FIFO]
P#: 2 3 2 1 5 2 4 5 3 2 5 2
Replacement Policy
Example:

• FIFO: 6 page faults.


• LRU recognizes that pages 2, 5 are
referenced more frequently than others,
• whereas FIFO does not.
Replacement Policy
• Clock:
– Compromise between
• Simplicity of FIFO
• Not used pages from LRU
– Choose an old page rather that the oldest page
– Uses an additional bit called a “use bit”
• Set to 1 when the page is used (accessed). by whom?
– OS scans the set flipping all 1’s to 0
– The first with use bit = 0 is replaced.
Ex. Trace [CLOCK]
P#: 2 3 2 1 5 2 4 5 3 2 5 2
Replacement Policy
Example:

• Clock: 5 page faults


• It protect frames 2 and 5 from replacement.
Comparison

Solution
Opt1: prefer clean over modified (modified clock)

Opt2: keep replaced page in RAM as long as possible


(page buffer)
• In prev. algorithms: DON’T take care of modified
• Replacing modified page  2 I/O (write, read)
• Replacing clean page  1 I/O (read)
Question
• In a system using a virtual paging, the page size is 1 KB, the program virtual
memory size is 1GB and the physical memory size is 16MB.
• Assume that the fetch policy is pre-paging and a fixed size resident set with
local replacement is used.
• The resident set size is 4 pages per program.
• The pre-paged pages are 1, 2 and 3.
• Consider a program of size 7 KB has the following sequence of virtual
addresses:
7916 (r), 3285 (r), 3736 (r), 1986 (w), 3945 (w), 6923 (r), 4315 (w), 5615 (r),
3388 (w)
Question (Cont.)
Answer the following questions:
• Using a LRU replacement, what’s the content of the
resident set at each of the following:
• after the 5th reference? [as page numbers]
• after the last reference? [as page numbers]
• What’s the total number of page faults?
Solution
Question

Final 2012: Q.3b


Solution
• Page size = 100 B, program size = 460 B  # pages of this program
= 5 pages, where:
• Page 0 takes from address 0 to address 99
• Page 1 takes from address 100 to address 199
• Page 2 takes from address 200 to address 299
• Page 3 takes from address 300 to address 399
• Page 4 takes from address 400 to address 460
Solution
Virtual address Page number Offset
10 0 10
11 0 11
104 1 4
170 1 70
73 0 73
309 3 9
185 1 85
245 2 45
246 2 46
434 4 34
358 3 58
54 0 54
Solution
Since physical memory size = 200 B, frame size = page size = 100 B
So, number of frames in physical memory = 200 / 100 = 2 frames
Sequence of page references (from table) = 0, 0, 1, 1, 0, 3, 1, 2, 2, 4, 3, 0
• LRU 0 0 1 1 0 3 1 2 2 4 3 0
0 0 0 0 0 0 1 1 1 4 4 0
1 1 1 3 3 2 2 2 3 3
F F F F F F

• Optimal
0 0 1 1 0 3 1 2 2 4 3 0
0 0 0 0 0 3 3 3 3 3 3 0
1 1 1 1 1 2 2 4 4 4
F F F F
Solution

• Clock

0 0 1 1 0 3 1 2 2 4 3 0
0* 0* 0* 0* 0* 3* 3* 3 3 4* 4 0*
  1* 1* 1* 1 1* 2* 2* 2* 3* 3*
F F F F F
Question
Midterm’19 Q4
Midterm’19 Q4

You might also like