3. 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
4. Background
• Virtual memory is a technique that allows the
execution of processes that may not be
completely in memory.
• Advantage is -programs can be larger than
physical memory.
• Virtual memory is not easy to implement
• May substantially decrease performance if it is
used carelessly.
11/15/2014 SDM SVV OS 4
5. 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
11/15/2014 SDM SVV OS 5
6. Background
• The instructions being executed must be in
physical memory.
• Examination of real programs shows us that
– Programs often have code to handle unusual error
conditions.
– Arrays, lists, and tables are often allocated more
memory than they actually need.
• Even if entire program is needed, it may not all
be needed at the same time.
11/15/2014 SDM SVV OS 6
7. Background
• Benefits: A program would no longer be
constrained by the amount of physical memory
that is available.
• More programs could be run at the same time -
increase in CPU utilization and throughput.
11/15/2014 SDM SVV OS 7
9. • Virtual memory makes the task of
programming much easier.
• The programmer no longer needs to worry
about the amount of physical memory
available.
• Virtual memory is commonly implemented by
demand paging.
• Demand segmentation can also be used to
provide virtual memory.
11/15/2014 SDM SVV OS 9
10. Demand Paging
• Bring a page into memory only when it is needed
– Less I/O needed
– Less memory needed
– Faster response
– More users
• 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
11. Transfer of a paged memory to contiguous disk space.
11/15/2014 SDM SVV OS 11
12. 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:
11/15/2014 SDM SVV OS 12
13. Valid-Invalid Bit
• During address translation, if valid–invalid bit
in page table entry is I page fault
11/15/2014 SDM SVV OS 13
v
v
v
v
i
i
i
….
Frame # valid-invalid bit
page table
15. The procedure for handling this page fault is straightforward
11/15/2014 SDM SVV OS 15
16. Steps for handling the page fault
• We check an internal table – request is valid/
invalid.
• If invalid terminate the process, If valid not in
memory.
• Find the free frame.
• Read the desired page.
• Modify the table.
• Restart the instruction.
11/15/2014 SDM SVV OS 16
17. • In the extreme case we could start executing a
process with no pages in memory
• First instruction of the process immediately
faults for the page.
• This scheme is pure demand paging
• The hardware to support demand paging
– Page Table
– Secondary memory
11/15/2014 SDM SVV OS 17
18. • If a page fault occurs while –fetching-operand,
fetch and decode again.
• Ex. C=A+B; all are memory locations.
• Page fault when storing in C!
11/15/2014 SDM SVV OS 18
19. 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)
20. What happens if there is no free frame?
• Page replacement – find some page in
memory, but not really in use, swap it out
– algorithms
– performance – want an algorithm which will result
in minimum number of page faults
• Same page may be brought into memory
several times.
11/15/2014 SDM SVV OS 20
21. A page fault causes the following sequence to occur
1.Trap to the operating system.
2. Save the user registers and process state.
3. Determine that the interrupt was a page fault.
4. Check that the page reference was legal and
determine the location of the page on the disk.
11/15/2014 SDM SVV OS 21
22. 5. Issue a read from the disk to a free frame:
a. Wait in a queue for this device until the read
request is serviced.
b. Wait for the device seek and/or latency time.
c. Begin the transfer of the page to a free frame.
6. While waiting, allocate the CPU to some other
user (CPU scheduling; optional).
7. Interrupt from the disk (I/O completed).
11/15/2014 SDM SVV OS 22
23. 8. Save the registers and process state for the
other user (if step 6 is executed).
9. Determine that the interrupt was from the disk.
10. Correct the page table and other tables to
show that the desired page is now in memory.
11. Wait for the CPU to be allocated to this
process again.
12. Restore the user registers, process state, and
new page table, then resume the interrupted
instruction.
11/15/2014 SDM SVV OS 23
24. 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
11/15/2014 SDM SVV OS 24
26. 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
11/15/2014 SDM SVV OS 26
28. • We must solve two major problems to
implement demand paging: develop
– a frame-allocation algorithm
– a page-replacement algorithm.
• Many algorithm - How do we select a
particular?
• We want the one with the lowest page-fault
rate.
11/15/2014 SDM SVV OS 28
29. • Algorithm evaluation done using the string of
mem references and computing No of page
faults.
• The string of memory references is called a
reference string.
– 0100, 0232, 0101, 0612, 0103, 0104, 0101, 0611,
0102, 0103, 0104, 0101, 0610, 0102, 0103, 0104,
0101, 0609, 0102, 0105
• 100 bytes per page, is reduced to the following
reference string
11/15/2014 SDM SVV OS 29
31. Page Replacement Algorithms
• Want lowest page-fault rate. (Like CPU scheduling lowest
waiting time…)
• Evaluate algorithm by running it on a
particular string of memory references
(reference string) and computing the number
of page faults on that string.
• The reference string considered is (Frames1:5)
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
11/15/2014 SDM SVV OS 31
32. Graph of Page Faults Versus The Number of Frames
11/15/2014 SDM SVV OS 32
33. First-In-First-Out (FIFO) Algorithm
• Simplest page replacement algorithm
• Each page associated with time –brought into
mem
• Replace the oldest page
• Not necessary to record the time, Ex.Que,
• Create -FIFO queue
• Replace the page at the head of the queue,
insert page at the tail of the queue.
11/15/2014 SDM SVV OS 33
34. First-In-First-Out (FIFO) Algorithm
• Reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
• 3 frames
– (3 pages can be in memory at a time per process)
• see
11/15/2014 SDM SVV OS 34
36. • Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• Find the number of page faults for the no of
frames – 1,2,3,4,5,6
11/15/2014 SDM SVV OS 36
38. FIFO Illustrating Belady’s Anomaly
11/15/2014 SDM SVV OS 38
• No of page faults for four frames is more than
the no of page faults for three frames.
• This most unexpected result is known as
Belady’s anomaly.
39. Optimal Algorithm
• Replace page that will not be used for longest period of
time
• 4 frames example see
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
How do you know this?
• Used for measuring how well your algorithm performs
1
2
3
6 page faults
4
1 1
2
1
2
3
1
2
3
5
5
2
3
4
43. Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2,
• 5, 1, 2, 3, 4, 5
• 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
5
2
4
3
1
2
3
4
1
2
5
4
1
2
5
3
1
2
4
3
48. • LRU is considered to be good.
• Major problem is how to implement LRU
replacement.
• Two implementations are feasible:
– Counters
– Stack
• Counters
– we associate with each page-table entry a time-of-
use field
11/15/2014 SDM SVV OS 48
49. • In this way, we always have the “time” of the
last reference to each page. We replace the
page with the smallest time value.
• Overflow of the clock must be considered.
Stack:
• keep a stack of page numbers.
• Whenever a page is referenced, it is removed
from the stack and put on the top.
11/15/2014 SDM SVV OS 49
50. • The top of the stack is always the most recently
used page and the bottom is the LRU page
11/15/2014 SDM SVV OS 50
51. • Neither optimal replacement nor LRU
replacement suffers from Belady's anomaly.
• There is a class of page-replacement
algorithms, called stack algorithms, that can
never exhibit Belady's anomaly.
11/15/2014 SDM SVV OS 51
52. LRU Approximation Page Replacement
• Reference bit
–With each page associate a bit, initially = 0
–When page is referenced bit set to 1
–Replace the one which is 0
• We do not know the order
11/15/2014 SDM SVV OS 52
53. LRU Approximation Page Replacement
• Additional-Reference-Bits Algorithm
– We can keep an 8-bit byte for each page in a table
in memory
– Shifts the reference bit, shifting the other bits
right-1 bit, discarding the low-order bit.
• 11000100 has been used more recently than
has one with 01110111
11/15/2014 SDM SVV OS 53
54. LRU Approximation Page Replacement
• Second chance
–If page to be replaced (in clock order) has
reference bit = 1 then:
• set reference bit 0
• leave page in memory
• replace next page (in clock order), subject
to same rules
11/15/2014 SDM SVV OS 54
56. 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
57. Allocation of frames
• How do we allocate fixed amount of free
frames to processes?
• If only one process –simple; If Demand paging
with multiprogramming?
• Minimum No of frames?
– Ex C=A+B ; worst case No of pages needed
• The mini no of frames defined by the
architecture.
• The max no of frames defined by the amount
of physical memory
11/15/2014 SDM SVV OS 57
59. Allocation Algorithm
• Easy way-m-frames; n processes allocate
each process m/n frames.
• Ex 93 frames 5 processes then each process
will get 18 frames (3 frames free)
• 3 frames used as free frame buffer pool.
• Total frames are 64; only two process are
there.
• Student program may take 1 frame remaining
are waste.
11/15/2014 SDM SVV OS 59
60. • Solution is Proportional Allocation.
• Allocate frames to each process according to its
size.
• Allocation Algorithm
– Global
– Local
• Local replacement requires that each process
select from only its own set of allocated frames.
• Global replacement allows a process to select a
frame of other process.
11/15/2014 SDM SVV OS 60
61. • Higher priority process may take frame of
lower priority process.
• Local-the number of frames allocated to a
process does not change.
• Global- The number of frames of a process
may increase
– Pblm- a process cannot control its own page-fault
rate.
11/15/2014 SDM SVV OS 61
62. 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
11/15/2014 SDM SVV OS 62
65. CAT 2 Scheme VII sem
1 a) What do you mean by Deadlock? List and
explain the necessary conditions for the deadlock
to occur.
Ans: Deadlock 1 Mark;
• A process requests resources; if the resources are
not available at that time, the process enters a wait
state. Waiting processes may never again change
state, because the resources they have requested
are held by other waiting processes. This situation
is called a deadlock.
Conditions and Explanation – 4Marks. MHNC
11/15/2014 SDM SVV OS 65
66. • Mutual exclusion: At least one resource must
be held in a non-sharable mode
• Hold and wait: A process must be holding at
least one resource and waiting to acquire
additional resources that are currently being
held by other processes.
• No preemption: Resources cannot be
preempted -released only voluntarily.
• Circular wait: A set {Po, PI, ..., P,) of waiting
processes must exist such
11/15/2014 SDM SVV OS 66
67. 1b) Explain paging concept for a 32 –byte
memory with 4-bytes pages.
11/15/2014 SDM SVV OS 67
68. 2a) What do you mean by Internal and External
fragmentation? Suggest the methods to reduce the
fragmentation. 2+2+1 Marks
• Ans: 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.
• Reduce external fragmentation by compaction
11/15/2014 SDM SVV OS 68
69. b) Explain the following i) Dynamic loading ii) Overlays
Ans: i) So far-the size of a process is limited to the size of
physical memory. To obtain better memory-space utilization -
can use dynamic loading. A routine is not loaded until it is
called.
Main pgm loaded in main mem -routines are kept on disk.
Advantage of dynamic loading - unused routine is never loaded.
ii) Keep in memory only those instructions and data that are
needed at any given time.
Needed when process is larger than amount of memory allocated
to it.
Implemented by user, no special support needed from operating
system, programming design of overlay structure is complex.
11/15/2014 SDM SVV OS 69
71. • What do you mean by critical section
problem? Explain with example. Write and
explain the Bakery algorithm.
• Ans: Critical section problem – 2, With
example -3; Bakery Algorithm – 5 marks
11/15/2014 SDM SVV OS 71
73. Mistakes
• In Deadlock – critical section problem
• Bakery algorithm – safe sequence
• Process are allocated to
• 50% Bakery and 50% Bankers
11/15/2014 SDM SVV OS 73