in/~govind/hp…
--------------------------------------------------------------------------
Lecture Notes scripted by students of the course. All disclaimers and
copyrights apply ! :-) (c) RG/SERC/IISc
--------------------------------------------------------------------------
Page Replacement:
On a page fault, the OS will look for page frames which are free
in main memory. If there are no page frames free, it replaces an existing page
from main memory. Which page is to be replaced is decided by whether we are
following FIFO (first in first out) or LRU (least recently used page
replacement policy). Suppose if the memory locations accessed by a
particular process are:
Ox0000 100C, 0000 10DC, 0000 2008, 0000 3A40, 0000 1B54, 0000 2008
0000 200C, 0000 23DC, 0000 5010, 0000 7A40, 0000 75B4, 0000 8080
Further suppose the page size 4Kbytes, then we use the "reference string" of
the pages accessed to determine the number of page faults and the pages
replaces. The reference string essentially consists of the page numbers
of the memory locations access, removing any duplication among successive
references. This is because, if the last reference is to a page p,
then an immediately following reference to the same page will never
cause a fault. Hence it is sufficient to consider the reference string
with the duplicate entries among successive references removed. For
the above accesses, the reference string is:
1, 2, 3, 1, 2, 5, 7, 8
Let us assume (for illustrative purposes) that only 4 main memory frames
are allocated per process in a multiprogramming environments.
For the above accesses, first, page 1 is required and will cause a
page fault. It is then brought into the memory. The next two references
to pages 2 and 3 will also cause page faults and the pages will be brought
into the memory. The subsequent access to page 1 will not cause any
page fault, as page 1 is already in memory. similarly the next reference
to page 2 will also not cause any page fault.
1 2 3 1 2 5 7 8
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 7 | | 7 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 8 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | 3 | | 3 | | 3 | | 3 | | 3 | | 3 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | 5 | | 5 | | 5 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
The subsequent access to page 5, causes a page faults and gets the last
free frame allocated for it. On the next reference, 7, which cause a
page fault, the memory management needs to find a page to be replaced.
for this if a FIFO page replacement algorithm is used, then the oldest
page, in this case page 1, is replaced, and 7 is brought in that place!
Subsequent reference 8, will replace page 2 as shown above.
…ernet.in/~govind/…/L8-Mem-Mgmt.txt 1/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
(reference to 7 and 8) will result in the replacing the least recently used
pages (3 and 1 respectively, and in that order) to be replaced, as
shown below.
1 2 3 1 2 5 7 8
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 1 | | 8 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 | | 2 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | 3 | | 3 | | 3 | | 3 | | 7 | | 7 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | 5 | | 5 | | 5 |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
Replacing a page may cause, the page (victim page) to written in the
disk. This must be done before the new page is written on the frame!
If a page is to be replaced, then the corresponding page table entry
in the page table is looked up. Associated with each page table entry,
the "Dirty" bit, indicates whether or not the page has been modified.
If it has been been modified (that is dirty bit set to 1), then the
page is written in to the disk.
Working Set:
this require 128MB of memory for matrix 'a'. if the page size is 4KB,
then we require 32K pages for this array. Now to see how (in what
order) the memory locations are accessed, let us understand the order
in which multi-dimensional arrays are arranged in the data segment.
…ernet.in/~govind/…/L8-Mem-Mgmt.txt 2/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
In C language, elements of a multi-dimension array are arranged in
the row-major ordering (in Fortran it is column major ordering),
which means that successive memory locations will have array elements
a[0][0]
a[0][1]
:
:
:
a[0][4095]
a[1][0]
a[1][1]
:
:
:
a[1][4095]
:
:
:
a[4095][0]
a[4095][1]
:
:
:
a[4095][4095]
Now, each page (4KB) will contain 4096/(size of double) = 4096/8 = 512
array elements. Thus if a[0][0] starts at the first byte of a page p,
then page p will end with the element a[0][511]. The element a[0][512]
will be in page p+1. Proceeding this way, we can say that
a[1][0] would be in page p+8 ,
a[2][0] would be in page p+16 , ...
a[4095][0] would be in page p+32760
Now, observe that in the above code, array elements are accessed in
the order a[0][0], a[1][0], ... a[4095][0], a[0][1], ... , a[4095][1],
... a[0][4095], a[1][4095], ... , a[4095][4095]
Now even if the process is allocated 2048 memory frames (8MB memory),
[or, 4095 frames -- 1 less than 4096 ] every access will cause a
page fault!
…ernet.in/~govind/…/L8-Mem-Mgmt.txt 3/4
08/05/2010 http://hpc.serc.iisc.ernet.in/~govind/hp…
p, p+1, p+2, ... p+32767
where each page is accessed 1024 times (512 loads and 512 stores)
before the next page is needed.
…ernet.in/~govind/…/L8-Mem-Mgmt.txt 4/4