You are on page 1of 2

Suppose we have a paging system with 4 frames and 12 pages, where the number of frames denotes

the number of pages that can be held in RAM at any given time. Assume the pages are accessed by
some program in the order shown below, from left to right. Also, assume that the program has just
started, so the frames are initially empty. How many page faults will be generated assuming that the
LRU (Least Recently Used) algorithm is being used?

Order in which pages are accessed


3, 4, 2, 1, 4, 7, 2, 5, 3, 6, 1, 3

A page fault occurs when a program tries to access a page that is mapped in address space, but not
loaded in the physical memory (the RAM). In other words, a page fault occurs when a program can
not find a page that its looking for in the physical memory, which means that the program would have
to access the paging file (which resides on the hard disk) to retrieve the desired page.
The term page fault is a bit misleading as it implies that something went seriously wrong. Although
page faults are undesirable as they result in slow accesses to the hard disk they are quite common
in any operating system that uses virtual memory.
Now, we need to actually solve the problem. The easiest way to do this is to break the problem down
into 12 steps (where 12 is the number of pages) to see what happens each time a page is referenced by
the program, and at each step see whether a page fault is generated or not. Of course, we want to keep
track of what pages are currently in the physical memory (the RAM). The first four page accesses will
result in page faults because the frames are initially empty. After that, if the program tries to access a
page thats already in one of the frames then theres no problem. But if the page that the program is
trying to access is not already in one of the frames then that results in a page fault. In this case, we
have to determine which page we want to take out (or swap) from the RAM, and for that we use the
LRU algorithm.
Some other algorithm could be used as well FIFO and NRU are other possibilities and as a group
these are known as page replacement algorithms. Applying the LRU algorithm to this problem is
fairly straightforward you simply remove the page that was least recently used. Proceeding in this
manner leads to the chart shown below you should try this out yourself before looking at the answer.
Page referenced

Page Fault

Resulting List

3,4

3,4,2

3,4,2,1

3,2,1,4

2,1,4,7

1,4,7,2

4,7,2,5

7,2,5,3

2,5,3,6

5,3,6,1

5,6,1,3

We can see that 9 page faults will be generated in this scenario.


Why does a page fault occur?
The concept of Paging divides the available memory to each program into segments of a fixed size
and each of them is called a page. The concept of virtual memory allows a computer to compensate
for the shortage of memory by temporarily transferring pages from physical memory to disk.
Eventually the OS will need to retrieve these pages from the disk when the program needs them. But
the OS removed these pages from the RAM because it was running out of memory. So in order to
bring these pages back into the memory, it now needs to remove other pages in the memory in order
to facilitate space for these pages. This process is called Paging.
So whenever a running process wants to access a page and the page is not present in the memory but
rather on the disk, a page fault will occur. To resolve a page fault, the OS will select a page from the
pages present in the memory and replace it by the page that the program wants to access (present on
the disk). The way we choose the page to be replaced when a page fault occurs determines the Paging
algorithm. The total amount of page fault depends on the number of page frames as well as the Paging
algorithm.
In case of first-in first-out, we choose the page which came into the memory before all other pages.
That is, the page that comes first will leave the memory first when swapping is called.
In the optimal case, we choose the page which occurs in the reference string behind all other pages
that are in the memory or doesn't occur at all. This essentially means choosing the page which will be
called by the running program after all other pages or which is not called in the future at all. The
problem with this algorithm is that it requires us to know the reference string in advance. But the
reference string cannot be known in advance especially for a program that involves branches.
Finally, coming to calculating the number of page faults. We have to simulate the algorithm to
calculate the number of page faults. The overview of the implementation is:
1. For each page request in the reference string, Check whether it is present in one of the memory
frames.
2. If yes, then continue with other pages in the reference string. Otherwise, increment the count of
page faults.
3. If there is a page frame available in the memory which is free, bring the page from the disk to this
free frame. Otherwise choose one of the page present in the memory frames in accordance with the
Paging algorithm and replace that page with the page being called by the program. Go to 1 and repeat
this procedure.

You might also like