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 (Mar. 4) ================

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.

Page fault!


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.

8. There are three cases as regards the frame to put PG into.

9. These combine into four cases: In all cases except Case 1:

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 two ways:

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.

4.2: Swapping

Medium-term scheduling:

Suspending: Taking an active process entirely out of memory and saving its state on disk.

Resuming: Loading a suspended process from disk to memory.

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 thrashing.

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 combined.

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 in trouble.