Os Chapter 9
Os Chapter 9
In real scenarios, most processes never need all their pages at once, for
following reasons :
Error handling code is not needed unless that specific error occurs, some
of which are quite rare.
Arrays are often over-sized for worst-case scenarios, and only a small
fraction of the arrays are actually used in practice.
More physical memory available, as programs are stored on virtual memory, so they occupy very
less space on actual physical memory.
● Demand Paging
● Demand Segmentation
What is Demand Paging?
The basic idea behind demand paging is that when a process is swapped in, its
pages are not swapped in all at once.
Rather they are swapped in only when the process needs them(On demand).
Initially only those pages are loaded which will be required the process
immediately.
Demand Paging
PAGE TABLE
● v=>page in memory
● i=> page not in memory
● During MMU address
mapping
○ i=> page fault occurs
Example of Demand Paging
PAGE FAULT
When we try to refer to a page which is not in memory then page fault occurs.
Since actual physical memory is much smaller than virtual memory, page faults
happen.
In case of page fault, Operating System might have to replace one of the existing
pages with the newly needed page.
Different page replacement algorithms suggest different ways to decide which page
to replace. The target for all algorithms is to reduce the number of page faults.
Steps to handle the page fault
When the process requires any of the page that is not loaded into the memory, a page fault
trap is triggered and following steps are followed,
1. The memory address which is requested by the process is first checked, to verify the request made by
the process is valid or invalid.
2. If its found to be invalid, the process is terminated.
3. In case the request by the process is valid, a free frame is located, possibly from a free-frame list,
where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified memory location. (
This will usually block the process on an I/O wait, allowing some other process to use the CPU in the
meantime. )
5. When the I/O operation is complete, the process's page table is updated with the new frame number,
and the invalid bit is changed to valid.
6. The instruction that caused the page fault must now be restarted from the beginning.
Steps to handle the page fault
Performance of Demand Paging
As studied in Demand Paging, only certain pages of a process are loaded initially into the
memory. This allows us to get more number of processes into the memory at the same time. but
what happens when a process requests for more pages and no free memory is available to bring
them in.
Following steps can be taken to deal with this problem :
Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.
Or, remove some other process completely from the memory to free frames.
Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have
some great algorithms to carry on page replacement efficiently.
Basic Page Replacement
reference
references to page p will never cause a page fault.
string?
Page p will be in memory after the first reference; the immediately following
references will not fault.
As new pages are requested and are swapped in, they are added
to tail of a queue and the page which is at the head becomes the
victim.
Its not an effective way of page replacement but can be used for
small systems.
FIFO
Oldest page in main memory is the one which will be selected for replacement.
Easy to implement, keep a list, replace pages from the tail and add new pages at the head.
FIFO Illustrating Belady’s Anomaly
Optimal Page Replacement
An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms. An
optimal page-replacement algorithm exists, and has been called OPT or MIN.
Replace the page that will not be used for the longest period of time in future. Use the time when
a page is to be used.
Optimal Page Replacement
Features of Optimal Page Replacement
Algorithm
Optimal algorithm results in the fewest number of page faults. This algorithm is difficult to
implement.
An optimal page-replacement algorithm method has the lowest page-fault rate of all algorithms.
This algorithm exists and which should be called MIN or OPT.
Replace the page which unlike to use for a longer period of time. It only uses the time when a
page needs to be used.
Least Recently Used (LRU) algorithm
Page which has not been used for the longest time in past is the one which will be selected for replacement.
Least Recently Used (LRU) algorithm
LRU Implememtation
Counter implementation
Each page-table entry has a counter; every time the page is referenced
through this entry, copy the current clock value into the counter
Stack implementation
Keep a stack of page numbers in a double link form, with head and tail
pointers:
No search for replacement; as LRU page is always at the bottom of the stack
NOTE: LRU and OPT are cases of stack algorithms that don’t have Belady’s
Anomaly
Advantages of Virtual Memory
Virtual memory helps to gain speed when only a particular segment of the program is
required for the execution of the program.
Process may become even larger than all of the physical memory.
Disadvantages of Virtual Memory
Assume that the page to be replaced is modified 70 percent of the time. What is
the maximum acceptable page-fault rate for an effective access time of no more
than 200 nanoseconds?
Solution:
0.2 microsec = (1 - P) * 0.1 microsec + (0.3P) * 8 millisec + (0.7P) * 20 millisec
Assume that 80 percent of the accesses are in the associative memory and that, of the
remaining, 10 percent (or 2 percent of the total) cause page faults. What is the effective
memory access time?
In demand paging, a page is not loaded into main memory until it is needed. In pure demand paging,
even a single page is not loaded into memory initially. Hence pure demand paging causes a page fault.
Rather than swapping the entire process into main memory, a lazy swapper is used. A lazy swapper
never swaps a page into memory unless that page will be needed.
Solution:
a. n
b. P
Q5. A certain computer provides its users with a virtual-memory space of 2^32 bytes.
The computer has 2^18 bytes of physical memory. The virtual memory is implemented
by paging, and the page size if 4,096 bytes. A user process generates the virtual address
11123456. Explain how the system establishes the corresponding physical location.
Solution:
Number of frames LRU FIFO Optimal
1 20 20 20
2 18 18 15
3 15 16 11
4 10 14 8
5 8 10 7
6 7 10 7
7 7 7 7
Q. Suppose there is a system with 128KB of memory with no memory initially allocated. Given the
following sequence of requests by the processes, show the memory layout at every stage for
worst-fit and Best fit allocation algorithm.