Page 265 - DCAP403_Operating System
P. 265
Operating System
Notes If the faulting virtual address was valid but the page that it refers to is not currently in memory,
the operating system must bring the appropriate page into memory from the image on disk. Disk
access takes a long time, relatively speaking, and so the process must wait quite a while until
the page has been fetched. If there are other processes that could run then the operating system
will select one of them to run. The fetched page is written into a free physical page frame and
an entry for the virtual page frame number is added to the processes page table. The process is
then restarted at the machine instruction where the memory fault occurred. This time the virtual
memory access is made, the processor can make the virtual to physical address translation and
so the process continues to run.
Linux uses demand paging to load executable images into a processes virtual memory.
Whenever a command is executed, the file containing it is opened and its contents are mapped
into the processes virtual memory. This is done by modifying the data structures describing
this processes memory map and is known as memory mapping. However, only the first part of
the image is actually brought into physical memory. The rest of the image is left on disk. As the
image executes, it generates page faults and Linux uses the processes memory map in order to
determine which parts of the image to bring into memory for execution.
13.5.2 Swapping
If a process needs to bring a virtual page into physical memory and there are no free physical
pages available, the operating system must make room for this page by discarding another page
from physical memory.
If the page to be discarded from physical memory came from an image or data file and has not
been written to then the page does not need to be saved. Instead it can be discarded and if the
process needs that page again it can be brought back into memory from the image or data fi le.
However, if the page has been modified, the operating system must preserve the contents of that
page so that it can be accessed at a later time. This type of page is known as a dirty page and
when it is removed from memory it is saved in a special sort of file called the swap fi le. Accesses
to the swap file are very long relative to the speed of the processor and physical memory and
the operating system must juggle the need to write pages to disk with the need to retain them in
memory to be used again.
If the algorithm used to decide which pages to discard or swap (the swap algorithm is not effi cient
then a condition known as thrashing occurs. In this case, pages are constantly being written to
disk and then being read back and the operating system is too busy to allow much real work to
be performed. If, for example, physical page frame number 1 in Figure 13.2 is being regularly
accessed then it is not a good candidate for swapping to hard disk. The set of pages that a process
is currently using is called the working set. An efficient swap scheme would make sure that all
processes have their working set in physical memory.
Linux uses a Least Recently Used (LRU) page aging technique to fairly choose pages which might
be removed from the system. This scheme involves every page in the system having an age which
changes as the page is accessed. The more that a page is accessed, the younger it is; the less that it
is accessed the older and more stale it becomes. Old pages are good candidates for swapping.
13.5.3 Shared Virtual Memory
Virtual memory makes it easy for several processes to share memory. All memory access are
made via page tables and each process has its own separate page table. For two processes sharing
a physical page of memory, its physical page frame number must appear in a page table entry in
both of their page tables.
Figure 13.2 shows two processes that each share physical page frame number 4. For process X
this is virtual page frame number 4 whereas for process Y this is virtual page frame number 6.
258 LOVELY PROFESSIONAL UNIVERSITY