Assume we use a large program (e.g Apache). Apache calls malloc() which uses lots of virtual memory.
for (;;)
r = get_a_request();
if (fork() == 0) { // clones virtual memory
execlp("python"); //process r
}
}
A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual address space, but not loaded in physical memory.
The physical memory is checked and a "victim" is picked. After picking the victim, we save it to the disk in case the victim is useful to other users.
Page fault mechanism can be illustrated by the following graph[1]:
If the memory is random accessed, then any policy works fine since victim can be chosen at random.
We choose the entry that has been in the memory fot the longest amount of time.
Assume the trace is:
0 1 2 3 0 1 4 0 1 2 3 4
Also assume 5 virtual pages and
3 physical pages.
0 |
1 |
2 |
3 |
0 |
1 |
4 |
0 |
1 |
2 |
3 |
4 |
|
A |
^0 |
0 |
0 |
^3 |
3 |
3 |
^4 |
4 |
4 |
4 |
4 |
4 |
B |
^1 |
1 |
1 |
^0 |
0 |
0 |
0 |
0 |
^2 |
2 |
2 |
|
C |
|
^2 |
2 |
2 |
^1 |
1 |
1 |
1 |
1 |
^3 |
3 |
There are 9 page faults in total.
If we increase number of physical page to 4, number of page faults also increases.
This situation is called Belady's anomaly:
0 |
1 |
2 |
3 |
0 |
1 |
4 |
0 |
1 |
2 |
3 |
4 |
|
A |
^0 |
0 |
0 |
0 |
0 |
0 |
^4 |
4 |
4 |
4 |
^3 |
3 |
B |
^1 |
1 |
1 |
1 |
1 |
1 |
^0 |
0 |
0 |
0 |
^4 |
|
C |
|
^2 |
2 |
2 |
2 |
2 |
2 |
^1 |
1 |
1 |
1 | |
D |
^3 |
3 |
3 |
3 |
3 |
3 |
^2 |
2 |
2 |
10 page faults in total.
0 |
1 |
2 |
3 |
0 |
1 |
4 |
0 |
1 |
2 |
3 |
4 |
|
A |
^0 |
0 |
0 |
^3 |
3 |
3 |
^4 |
4 |
4 |
^2 |
2 |
2 |
B |
^1 |
1 |
1 |
^0 |
0 |
0 |
0 |
0 |
0 |
^3 |
3 |
|
C |
|
^2 |
2 |
2 |
^1 |
1 |
1 |
1 |
1 |
1 |
^4 |
Cost is 10 page faults.
Look into the future to see which page is not needed for the longest time.
0 |
1 |
2 |
3 |
0 |
1 |
4 |
0 |
1 |
2 |
3 |
4 |
|
A |
^0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
^2 |
2 |
2 |
B |
^1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
^3 |
3 |
|
C |
|
^2 |
^3 |
3 |
3 |
^4 |
4 |
4 |
4 |
4 |
4 |
7 page faults in total.
Pages are only brought into memory if they are demanded. When a program starts, only the first page is loaded into RAM.
Pros and Cons:
+: Less wait time (Program starts faster)
-: More page faults during execution
Cost Comparison:
N: Number of pages in the program
U: Number of pages used (0 < U <= F)
C: Cost of reading 1 page
F: Cost of faulting
Options | Total Cost | Latency Cost |
No demand paging | N*C | N*C |
Demand paging | U(C+F) | C+F |