Thus far, the way we have described how replacements occur assumes that the OS waits until
memory is entirely full, and only then replaces or evicts a page to make room for some other pages.
As you can imagine, this is a little bit unrealistic, and there are many reasons for the OS to keep a
small portion of memory free more proactively. To keep a small amount of memory free, most OSs
thus have some kind of high watermark (HW) and low watermark (LW) to help decide when to start
evicting pages from memory. How this works is as follows: When the OS notices that there are fewer
than LW pages available, a background thread that is responsible for freeing memory runs. The thread
evicts pages until there are HW pages available. The background thread, sometimes called the Swap
daemon or page daemon, then goes to sleep, happy that it has freed some memory for running
processes and the OS to use. By performing a number of replacements at once, new performance
optimizations become possible. For example, many systems will cluster or group a number of pages
and write them out at once to the swap partition, thus increasing the efficiency of the disk; as we will
see later when we discuss disks in more detail, such clustering reduces seek and rotational overhead
of a disk and thus increases performance noticeably.
Tips: Do Work In The Background
When you have some work to do, it is often a good idea to do it in the background to increase efficiency
and to allow for grouping of operations. Operating Systems often do work in the background; for example,
many systems buffer file writes in memory before actually writing the data to the disk, Doing so has many
possible benefits: increased disk efficiency, as the disk may now receive many writes at once and thus better
be able to schedule them; improved latency of writes, as the application thinks the writes completed quite
quickly; the possibility of work reductions, as the writes may need never to go to disk (if the file is deleted);
and better use of idle time, as the background work may possibly be done when the system is otherwise
idle, thus better utilizing the hardware.
Summary:
In this brief chapter, we have introduced the notion of accessing more memory than is physically present
within a system. To do so requires more complexity in page-table structures, as a present-bit (of some kind)
must be included to tell us whether the page is in memory or not. When not, the operating system page-fault
handler runs to service the page fault, and thus arranges for the transfer of the desired page from disk to
memory, perhaps first replacing some pages in memory to make room for these soon to be swapped in. Recall,
importantly (and amazingly), that these actions all take place transparently to the process. As far as process
is concerned, it is just accessing its own private, contiguous virtual memory. Behind the scenes, pages are
placed in arbitrary (non-contiguous) locations in physical memory, and sometime they are not even present
in memory, requiring a fetch from disks. While we hope that in the common case a memory access is fast, in
some cases it will take multiple disk operations to service it; something as simple as performing a single
instruction can, in the worst case, take many milliseconds to complete.