NOTE: These notes are by Allan Gottlieb, and are
reproduced here, with superficial modifications, with his permission.
"I" in this text generally refers to Prof. Gottlieb, except
in regards to administrative matters.
================ Start Lecture #12
Transfer of Control : Page Faults
Note: Different architectures differ in detail. In many cases,
for concreteness, I have chosen one particular method below, and
not discussed all variant techniques.
Also, I ignore instruction backup. See Tanenbaum, p. 244.
Process P issues a request for page PG. PG is not in the TLB. P's
page table shows the valid bit for PG is 0; PG is not in memory.
1. Save current value of PC on stack or other fixed location.
2. Set flag to indicate that a page fault has occurred. Optionally,
save requested page in register.
3. Change to kernel mode. In consequence, the machine will be using the
kernel's page table.
4. Trap to kernel.
Assembly language in kernel
4. Save registers in P's PTE.
5. Using flag set in step (2), jump to page fault handler in kernel.
Regular routine in kernel
6. Determine page PG, either by retrieving it from register, or by
examining instruction that is being executed.
7. There are three cases as regards the new page PG.
- A. PG is a new empty data page.
- B. PG is a page of executable code. Can be found in executable file.
Mapping from page address of code to block of executable file
can be in table in executable.
- C. PG is a data page previously created and paged out. Can
be found in paging area of disk. Use disk page table (or scheme
for translating page number to disk address --- see Tanenbaum p. 246.)
to find disk address of PG.
8. There are three cases as regards the frame to put PG into.
9. These combine into four cases:
- X. There exists a free frame FR.
- Y. Otherwise, the PRA must be run. Suppose the PRA chooses page PGV
of process Q (Q may be the same as P or may be different). Two cases:
- Y.1: PGV is clean.
- Y.2: PGV is dirty.
In all cases except Case 1:
- Case 1 --- (A) and (X or Y.1): No I/O needed, no need to block P.
Zero out frame FR.
In P's page table, mark page P as being in frame FR. Return to process P.
- Case 2 --- (A) and (Y.2). Necessary to write out page PGV.
Find a disk address for PGV in swap space. Schedule disk driver to
write out PGV. Record that, once this write is complete, P may be unblocked.
- Case 3 --- (B or C) and (X or Y.1). Necessary to read PG from disk.
Schedule disk read for PG into FR. Record that, once this read is
complete, P may be unblocked.
- Case 4 --- (B or C) and (Y.2). Necessary first to write out PGV then
to read in PG. Find disk address for PGV. Schedule disk driver to write
out PGV. Record that, once this write is complete, it must be followed
by reading in PG; and when that read is complete, P may be unblocked.
10. P must be blocked. Run the process scheduler to choose a new process to
run. Let R be the new process.
11. Record in Q's page table that PGV is now invalid (because it will
be overwritten at some undetermined time.)
12. The change from P's page table to R's page table is handled in one of
- The page table for the running process is kept at a fixed memory address.
In this case, first copy the running page table into P's permanent page
table; then copy R's permanent page table into the running page table.
- There is a register that points to the page table for the running
page table. In this case, reset the register to point to R's page table.
13. Clear the TLB.
14. Load the registers from R's PTE.
15. Resume R by changing back to user mode and jumping to the address in
R recorded in R's PTE.
Suspending: Taking an active process entirely out of
memory and saving its state on disk.
Resuming: Loading a suspended process from disk to
A suspended process may be blocked (waiting for some other event to
complete) or it may be suspended and ready (i.e. ready to run
as soon as it is swapped in.)
In an OS that uses variable-length partitions, swapping can be done
when some waiting process can't fit in memory; especially when
the memory requirements of all active process is larger than total
memory. In an OS that uses paging, swapping can be done when the
working set sizes of the active processes are together larger than
physical memory, so that keeping all processes active leads to
Unlike previous similar issues, such as short-term scheduling and
page replacement, I am not going to give a list of a half-dozen
simple swapping algorithms. Swapping is more complicated and
doesn't lend itself to simple algorithms. (Also, it is done
comparatively infrequently, so one can afford to spend time on a complicated
algorithm.) Rather, there are a number of considerations that must be
First, note that there are two decisions to be made: which,
if any, jobs to swap in and which, if any, to swap out. With variable
length partitioning, these are always combined. The only reason to swap
one job out is to swap another in. You can only swap a job in if
either an active job terminates or if you swap some other job out.
With a paging system, it may become desirable to swap one job out and not
bring any in, if the working sets of the active jobs grow; or it may
become feasible to swap a job in without any other job terminating or
being suspended, if the working sets shrink.
Criteria for choosing jobs to swap in / out
The bottom line is that if you have to do a lot of swapping, you're
- Fairness. You want to play fair among the various jobs. Debatable
whether you want jobs to have equal time in memory; or equal time waiting
suspended; or equal time running the CPU; etc.
- Size. It's always easier and less costly to swap in a small job.
In choosing a job to swap out, there's a trade-off: Swapping out a large
job takes longer (to write to disk); on the other hand, it clears up
more space that you can use for other jobs. In particular, swapping
in a large job may require swapping out several small jobs.
- Blockedness. If the job is blocked in any case waiting for some
event that may be slow, why not swap it out?
- Real-time/interactive. If a job is supposed to run in real time
or if it is supposed to be interactive, it had better not be swapped out.
- Page faulting: If all the processes are page-faulting a lot, then
you have better swap one or more of them out.