0% found this document useful (0 votes)
56 views106 pages

Chapter 9-Virtual Memory - Updated

Uploaded by

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

Chapter 9-Virtual Memory - Updated

Uploaded by

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

Chapter 9: Virtual Memory

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Chapter 9: 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 – 8th Edition 9.2 Silberschatz, Galvin and Gagne ©2009
Objectives
 To describe the benefits of a virtual memory system

 To explain the concepts of demand paging, page-replacement


algorithms, and allocation of page frames

 To discuss the principle of the working-set model

Operating System Concepts – 8th Edition 9.3 Silberschatz, Galvin and Gagne ©2009
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

 Virtual memory can be implemented via:


 Demand paging
 Demand segmentation

Operating System Concepts – 8th Edition 9.4 Silberschatz, Galvin and Gagne ©2009
Virtual Memory That is
Larger Than Physical Memory

Operating System Concepts – 8th Edition 9.5 Silberschatz, Galvin and Gagne ©2009
Demand Paging
 Bring a page into memory only when it is needed
 Less I/O needed
 Less memory needed
 Faster response
 Higher Degree of Multi-programming

 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 – 8th Edition 9.6 Silberschatz, Galvin and Gagne ©2009
Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(v  in-memory, i  not-in-memory)
 Initially valid–invalid bit is set to i on all entries
 Example of a page table snapshot:

Frame # valid-invalid bit


v
v
v
v
i
….

i
i
page table

 During address translation, if valid–invalid bit in page table entry


is i  page fault
Operating System Concepts – 8th Edition 9.7 Silberschatz, Galvin and Gagne ©2009
Page Table When Some Pages
Are Not in Main Memory

Operating System Concepts – 8th Edition 9.8 Silberschatz, Galvin and Gagne ©2009
Page Fault

 If there is a reference to a page, first reference to that page will trap to


operating system:
page fault
1. Operating system looks at another table to decide:
 Invalid reference  abort
 Just not in memory
2. Get empty frame
3. Swap page into frame
4. Reset tables
5. Set validation bit = v
6. Restart the instruction that caused the page fault

Operating System Concepts – 8th Edition 9.9 Silberschatz, Galvin and Gagne ©2009
Steps in Handling a Page Fault

Operating System Concepts – 8th Edition 9.10 Silberschatz, Galvin and Gagne ©2009
Performance of Demand Paging
 Page Fault Rate 0  p  1.0
 if p = 0 no page faults
 if p = 1, every reference is a fault

 Effective Access Time (EAT)


EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in
+ restart overhead
)

Operating System Concepts – 8th Edition 9.11 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
 Memory access time = 100 nanosec
 Page fault service time = 25 millisec
 Teffective = (1 - p) x 100 + p (25 milli)
= (1 - p) x 100 + p (25000000)
= 100 + 24999900 x p
 If one access out of 1000 causes a page
fault, effective access time is 25
microseconds, a slowdown by a factor of
250.

Operating System Concepts – 8th Edition 9.12 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
 If we want less than 10 percentage
degradation in effective memory access
time then we have the following inequality
110 > 100 + 25000000 x p
10 > 25000000 x p
p < 0.0000004
 This means we can allow only one page
fault every 2,500,000.

Operating System Concepts – 8th Edition 9.13 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
 Memory access time = 200 nanoseconds

 Average page-fault service time = 8 milliseconds

 EAT = (1 – p) x 200 + p (8 milliseconds)


= (1 – p) x 200 + p x 8,000,000
= 200 + p x 7,999,800

 If one access out of 1,000 causes a page fault, then


EAT = 8.2 microseconds.
This is a slowdown by a factor of 40!!

Operating System Concepts – 8th Edition 9.14 Silberschatz, Galvin and Gagne ©2009
What happens if there is no free frame?

 Page replacement – find some page in memory, but not really in use,
swap it out
 algorithm
 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 – 8th Edition 9.15 Silberschatz, Galvin and Gagne ©2009
Page Replacement
 Prevent over-allocation of memory 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 – 8th Edition 9.16 Silberschatz, Galvin and Gagne ©2009
Basic Page Replacement

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

4. Restart the process

Operating System Concepts – 8th Edition 9.17 Silberschatz, Galvin and Gagne ©2009
Page Replacement

Operating System Concepts – 8th Edition 9.18 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms

 Want lowest page-fault rate

 Evaluate algorithm by running it on a particular string of memory


references (reference string) and computing the number of page faults
on that string

 In all our examples, the reference string is

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

Operating System Concepts – 8th Edition 9.19 Silberschatz, Galvin and Gagne ©2009
Graph of Page Faults Versus
The Number of Frames

Operating System Concepts – 8th Edition 9.20 Silberschatz, Galvin and Gagne ©2009
First-In-First-Out (FIFO) Algorithm
 Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
 3 frames (3 pages can be in memory at a time per process)

1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
 4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2

4 4 3

 Belady’s Anomaly: more frames  more page faults

Operating System Concepts – 8th Edition 9.21 Silberschatz, Galvin and Gagne ©2009
FIFO Page Replacement

Operating System Concepts – 8th Edition 9.22 Silberschatz, Galvin and Gagne ©2009
FIFO Illustrating Belady’s Anomaly

Operating System Concepts – 8th Edition 9.23 Silberschatz, Galvin and Gagne ©2009
Optimal Algorithm
 Replace page that will not be used for longest period of time
 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

1 4
2 6 page faults
3

4 5
 How do you know this?
 Used for measuring how well your algorithm performs

Operating System Concepts – 8th Edition 9.24 Silberschatz, Galvin and Gagne ©2009
Optimal Page Replacement

Operating System Concepts – 8th Edition 9.25 Silberschatz, Galvin and Gagne ©2009
Least Recently Used (LRU) Algorithm
 Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3

 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
determine which are to change

Operating System Concepts – 8th Edition 9.26 Silberschatz, Galvin and Gagne ©2009
LRU Page Replacement

Operating System Concepts – 8th Edition 9.27 Silberschatz, Galvin and Gagne ©2009
Use Of A Stack to Record The
Most Recent Page References

Operating System Concepts – 8th Edition 9.28 Silberschatz, Galvin and Gagne ©2009
Counting Algorithms
 Keep a counter of the number of references that have been made to
each page

 LFU Algorithm: replaces page with smallest count

 MFU Algorithm: based on the argument that the page with the
smallest count was probably just brought in and has yet to be used

Operating System Concepts – 8th Edition 9.29 Silberschatz, Galvin and Gagne ©2009
Allocation of Frames

 Each process needs minimum number of pages

 Three major allocation schemes


 fixed allocation
 Proposional allocation
 priority allocation

Operating System Concepts – 8th Edition 9.30 Silberschatz, Galvin and Gagne ©2009
Fixed Allocation
 Equal allocation – For example, if there are 100 frames and 5
processes, give each process 20 frames.

 Proportional allocation – Allocate according to the size of process


si size of process pi
S   si
m total number of frames
s
ai allocation for pi  i m
S
m 64
s1 10
s2 127
10
a1  64 5
137
127
a2  64 59
137

Operating System Concepts – 8th Edition 9.31 Silberschatz, Galvin and Gagne ©2009
Priority Allocation
 Use a proportional allocation scheme using priorities rather than size

 If process Pi generates a page fault,


 select for replacement one of its frames
 select for replacement a frame from a process with lower priority
number

Operating System Concepts – 8th Edition 9.32 Silberschatz, Galvin and Gagne ©2009
Global vs. Local Allocation

 Global replacement – process selects a replacement frame from the


set of all frames; one process can take a frame from another

 Local replacement – each process selects from only its own set of
allocated frames

Operating System Concepts – 8th Edition 9.33 Silberschatz, Galvin and Gagne ©2009
Thrashing

 If a process does not have “enough” pages, the page-fault rate is very
high. This leads to:
 low CPU utilization
 operating system thinks that it needs to increase the degree of
multiprogramming
 another process added to the system

 Thrashing  a process is busy swapping pages in and out

Operating System Concepts – 8th Edition 9.34 Silberschatz, Galvin and Gagne ©2009
Thrashing (Cont.)

Operating System Concepts – 8th Edition 9.35 Silberschatz, Galvin and Gagne ©2009
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?


 size of locality > total memory size

Operating System Concepts – 8th Edition 9.36 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms

 These policies are concerned with


deciding which page or segment to
displace/remove to make room for an
incoming page (or segment) when the
main memory is already full.
 Following are these strategies:

Operating System Concepts – 8th Edition 9.37 Silberschatz, Galvin and Gagne ©2009
1. The Principle of Optimality
 In this strategy, that page is replaced that
will not be used again for the furthest time
into the future.
 Although it is possible to demonstrate the
optimality of this strategy, it cannot be fully
applied since the future cannot be
predicted.

Operating System Concepts – 8th Edition 9.38 Silberschatz, Galvin and Gagne ©2009
2. Random Page Replacement

 It’s a simple technique in which all


pages in the main storage have an
equal probability of being selected for
replacement.
 This strategy could select any page for
replacement, including the next page to
be referenced (which is, of course the
worst page to replace).
 Since it’s a hit-or-miss approach, this
scheme is rarely used.
Operating System Concepts – 8th Edition 9.39 Silberschatz, Galvin and Gagne ©2009
3. FIFO (First-In-First-Out) Page Replacement

 Each page is time-stamped as it enters (arrives) the


main memory.
 The page which has stayed longest in the main
memory is chosen to be replaced (since it had arrived
first).
 The general technique of this strategy is that, since
this page has had its chance and it’s time to give
another page a chance.
 Negative: Under this scheme a heavily used page is
likely to be replaced, because it has been in the
memory for a long time (remember the time-stamp!).
And it’s the same page that has been in constant use
and is likely to be used again in the future.

Operating System Concepts – 8th Edition 9.40 Silberschatz, Galvin and Gagne ©2009
 Implementation: This strategy can also
be implemented with a simple FIFO
queue.
 As each page arrives, it is placed at the
tail of the queue and pages are replaced
from the head of the queue.
 -ve: The overhead of keeping the
time-stamp.
 Belady’s Anomaly (Reading Homework
Assignment).
Operating System Concepts – 8th Edition 9.41 Silberschatz, Galvin and Gagne ©2009
Example

 A process makes references to 4 pages:


A, B, E, and R
 Reference stream: BEERBAREBEAR
 Physical memory size: 3 pages

Operating System Concepts – 8th Edition 9.42 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B
2
3

Operating System Concepts – 8th Edition 9.43 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B
2 E
3

Operating System Concepts – 8th Edition 9.44 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B
2 E *
3

Operating System Concepts – 8th Edition 9.45 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B
2 E *
3 R

Operating System Concepts – 8th Edition 9.46 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R

Operating System Concepts – 8th Edition 9.47 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R

Operating System Concepts – 8th Edition 9.48 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E *
3 R

Operating System Concepts – 8th Edition 9.49 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E *
3 R *

Operating System Concepts – 8th Edition 9.50 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E * *
3 R *

Operating System Concepts – 8th Edition 9.51 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E * *
3 R *

Operating System Concepts – 8th Edition 9.52 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R *

Operating System Concepts – 8th Edition 9.53 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R *

Operating System Concepts – 8th Edition 9.54 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R * E

Operating System Concepts – 8th Edition 9.55 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A *
2 E * * B
3 R * E

Operating System Concepts – 8th Edition 9.56 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A *
2 E * * B
3 R * E

Operating System Concepts – 8th Edition 9.57 Silberschatz, Galvin and Gagne ©2009
FIFO

Memory page B E E R B A R E B E A R
1 B * A * R
2 E * * B
3 R * E

Operating System Concepts – 8th Edition 9.58 Silberschatz, Galvin and Gagne ©2009
FIFO

 7 page faults

Memory page B E E R B A R E B E A R
1 B * A * R
2 E * * B
3 R * E

Operating System Concepts – 8th Edition 9.59 Silberschatz, Galvin and Gagne ©2009
4. LRU (Least-Recently-
Used)

 This strategy selects that page for replacement


that has not been used for the longest time.
 The principle here is that “the recent past is a
good indicator of the near future”.
 LRU requires that each page be time-stamped
whenever it is referenced.
 -ve: This strategy requires substantial
overhead, and thus although it’s appealing, it
is not often implemented in current systems.
However, lower overhead strategies that
approximate LRU are used.
Operating System Concepts – 8th Edition 9.60 Silberschatz, Galvin and Gagne ©2009
 Implementation: This strategy can be
implemented with a list structure containing one
entry for each occupied page frame.
 Each time a page frame is referenced, the entry
for that page is placed at the head of the list.
 Older entries migrate toward the tail of the list.
 When a page must be replaced to make room for
an incoming page, the entry at the tail of the list is
selected, the corresponding page is freed, and the
incoming page is placed at the head of the list
because that page is now the one that has been
most recently used.
Operating System Concepts – 8th Edition 9.61 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B
2 E *
3 R

Operating System Concepts – 8th Edition 9.62 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R

Operating System Concepts – 8th Edition 9.63 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R

Operating System Concepts – 8th Edition 9.64 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R

Operating System Concepts – 8th Edition 9.65 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R *

Operating System Concepts – 8th Edition 9.66 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R *

Operating System Concepts – 8th Edition 9.67 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E
2 E * A
3 R *

Operating System Concepts – 8th Edition 9.68 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E
2 E * A
3 R *

Operating System Concepts – 8th Edition 9.69 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E
2 E * A B
3 R *

Operating System Concepts – 8th Edition 9.70 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R *

Operating System Concepts – 8th Edition 9.71 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R *

Operating System Concepts – 8th Edition 9.72 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R * A

Operating System Concepts – 8th Edition 9.73 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R * A

Operating System Concepts – 8th Edition 9.74 Silberschatz, Galvin and Gagne ©2009
LRU

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B R
3 R * A

Operating System Concepts – 8th Edition 9.75 Silberschatz, Galvin and Gagne ©2009
LRU

 8 page faults

Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B R
3 R * A

Operating System Concepts – 8th Edition 9.76 Silberschatz, Galvin and Gagne ©2009
5. LFU (Least-Frequently-
Used)

 The thing to be considered here is “how intensive


the use of each page has been”.
 The page to replace is that page that is least
frequently used or least intensively referenced.
 -ve: The wrong page could quite easily be
selected for replacement, e.g., the least
frequently used page could be the page brought
into main memory most recently. This page has
been used once whereas all other pages in the
main storage could have been used more than
once. So, this technique replaces this page when
in fact it would be highly likely to be used
immediately.
Operating System Concepts – 8th Edition 9.77 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B
2
3

Operating System Concepts – 8th Edition 9.78 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B
2 E
3

Operating System Concepts – 8th Edition 9.79 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B
2 E 2
3

Operating System Concepts – 8th Edition 9.80 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B
2 E 2
3 R

Operating System Concepts – 8th Edition 9.81 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R

Operating System Concepts – 8th Edition 9.82 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R A

Operating System Concepts – 8th Edition 9.83 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R A R

Operating System Concepts – 8th Edition 9.84 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2
2 E 2 3
3 R A R

Operating System Concepts – 8th Edition 9.85 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3
3 R A R

Operating System Concepts – 8th Edition 9.86 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R

Operating System Concepts – 8th Edition 9.87 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A

Operating System Concepts – 8th Edition 9.88 Silberschatz, Galvin and Gagne ©2009
LFU

Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A R

Operating System Concepts – 8th Edition 9.89 Silberschatz, Galvin and Gagne ©2009
LFU

 7 page faults

Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A R

Operating System Concepts – 8th Edition 9.90 Silberschatz, Galvin and Gagne ©2009
6. NUR (Not-Used-Recently)
 Pages not used recently are not likely to be used in the
near future and they may be replaced with incoming pages.
 This is a scheme for approximating LRU with little
overhead.
 Implementation: NUR is implemented with addition of two
hardware bits per page.
These are:
 (1). Reference bit = 0 if the page has not been referenced
1 if the page has been referenced
 (2). Dirty bit = 0 if the page has not been modified
1 if the page has been modified

Operating System Concepts – 8th Edition 9.91 Silberschatz, Galvin and Gagne ©2009
NUR
 Initially, the reference bits of all pages are set to 0.
 If a particular page is referenced, its reference bit is set to
1.
 Initially, the dirty bits of all pages are also set to 0.
 Whenever a page is modified, its modified bit is set to 1.
 When a page is to be replaced, the OS first tries to find a
page which has not been referenced (otherwise, we have
no choice but to replace a referenced page).
 If a page has not been referenced, we check whether it has
been modified or not.
 If it has not been modified, we replace it on the basis that
there is less overhead involved than in replacing a modified
page that must be written back to secondary storage.
 Otherwise, a modified page is replaced.

Operating System Concepts – 8th Edition 9.92 Silberschatz, Galvin and Gagne ©2009
The Principle of Locality

 The Principle of Locality states that “processes


tend to reference storage in non-uniform, highly
localized patterns”.

 Locality is of two types as follows:


 Temporal Locality
 Spatial Locality

Operating System Concepts – 8th Edition 9.93 Silberschatz, Galvin and Gagne ©2009
Temporal Locality

 Temporal Locality relates to Time.


Example: Weather w.r.t. time.

 It means that storage locations referenced


recently are likely to be referenced in the near
future.
 Supporting this observation stacks, subroutines,
looping and variables are used for counting and
totaling.

Operating System Concepts – 8th Edition 9.94 Silberschatz, Galvin and Gagne ©2009
Spatial Locality

 Spatial Locality relates to Space.


Example: Weather w.r.t. space.
 It means that storage references tend to
be clustered so that once a location is
referenced; it is highly likely that nearby
locations will be referenced. Supporting
this observation are array traversals,
sequential code execution and the
tendency of programmers to place related
variable definitions near one another.

Operating System Concepts – 8th Edition 9.95 Silberschatz, Galvin and Gagne ©2009
Working Set Theory
 Denning formulated the working set theory of program
behavior based upon observations of locality.
 A working set is a collection of pages a process is
actively referencing.
 The real working set of a process is the set of pages that
must be in main memory for a process to execute
efficiently.
 Denning maintained that for a program to run efficiently, its
working set of pages must be maintained in the main
memory.
 Else, excessive paging activity (Thrashing) might occur as
the program repeatedly requests pages from the secondary
storage.

Operating System Concepts – 8th Edition 9.96 Silberschatz, Galvin and Gagne ©2009
Locality In A Memory-Reference Pattern

Operating System Concepts – 8th Edition 9.97 Silberschatz, Galvin and Gagne ©2009
Working-Set Model
   working-set window  a fixed number of page references

 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

 if D > m  Thrashing

 Policy if D > m, then suspend one of the processes

Operating System Concepts – 8th Edition 9.98 Silberschatz, Galvin and Gagne ©2009
Working-set model

Operating System Concepts – 8th Edition 9.99 Silberschatz, Galvin and Gagne ©2009
Page-Fault Frequency Scheme

 Establish “acceptable” page-fault rate


 If actual rate too low, process loses frame
 If actual rate too high, process gains frame

Operating System Concepts – 8th Edition 9.100 Silberschatz, Galvin and Gagne ©2009
Question

 A system has three processes (P1, P2, P3)


and three reusable resources (R1, R2, R3).
There is one instance of R1, two instances of
R2 and three instances of R3. P1 holds an R1
and an R3 and is requesting an R2. P2 holds
an R3 and is requesting an R1 and an R2. P3
holds two R2 and an R3 and is requesting an
R1.
 Draw the resource allocation graph for this
situation.

Operating System Concepts – 8th Edition 9.101 Silberschatz, Galvin and Gagne ©2009
Question

 Given the following stream of page


references by an application, calculate the
number of page faults the application
would incur with the following page
replacement algorithms. Assume that all
pages are initially free. Reference Stream:
ABCDABEABCDEBAB

Operating System Concepts – 8th Edition 9.102 Silberschatz, Galvin and Gagne ©2009
 P is a set of processes. R is a set of
resources. E is a set of request or
assignment edges. The
 sets P, R, and E are as follows:
 P = {P1, P2, P3}, R = {R1,R2,R3}

Operating System Concepts – 8th Edition 9.103 Silberschatz, Galvin and Gagne ©2009
 E = {P1 → R1, P1 → R2, P2 → R2, P2 →
R3, P3 → R2, P3 → R3,R1 → P2,R2 →
P2,R3 → P1}.
 R1 has one instance. R2 has two
instances. R3 has one instances.
 (a) Draw the resource-allocation graph.
 (b) Is there any deadlock in this situation?
Briefly Explain.

Operating System Concepts – 8th Edition 9.104 Silberschatz, Galvin and Gagne ©2009
Question

 Given page reference string:


1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
 Compare the number of page faults for
LRU, FIFO and Optimal page replacement
algorithm

Operating System Concepts – 8th Edition 9.105 Silberschatz, Galvin and Gagne ©2009
End of Chapter 9

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009

You might also like