Page 275 - DCAP403_Operating System
P. 275
Operating System
Notes If all the buffers are freed, then the pages that contain them are also be freed. If the examined
page is in the Linux page cache, it is removed from the page cache and freed.
When enough pages have been freed on this attempt then the kernel swap daemon will wait
until the next time it is periodically woken. As none of the freed pages were part of any process’s
virtual memory (they were cached pages), then no page tables need updating. If there were not
enough cached pages discarded then the swap daemon will try to swap out some shared pages.
13.5.14 Swapping Out System V Shared Memory Pages
System V shared memory is an inter-process communication mechanism which allows two or
more processes to share virtual memory in order to pass information amongst themselves. For
now it is enough to say that each area of System V shared memory is described by a shmid_ds
data structure. This contains a pointer to a list of vm_area_struct data structures, one for each
process sharing this area of virtual memory. The vm_area_struct data structures describe where
in each processes virtual memory this area of System V shared memory goes. Each vm_area_struct
data structure for this System V shared memory is linked together using the vm_next_shared and
vm_prev_shared pointers. Each shmid_ds data structure also contains a list of page table entries
each of which describes the physical page that a shared virtual page maps to.
The kernel swap daemon also uses a clock algorithm when swapping out System V shared
memory pages. Each time it runs it remembers which page of which shared virtual memory
area it last swapped out. It does this by keeping two indices, the first is an index into the set of
shmid_ds data structures, the second into the list of page table entries for this area of System V
shared memory. This makes sure that it fairly victimizes the areas of System V shared memory.
As the physical page frame number for a given virtual page of System V shared memory is
contained in the page tables of all of the processes sharing this area of virtual memory, the kernel
swap daemon must modify all of these page tables to show that the page is no longer in memory
but is now held in the swap file. For each shared page it is swapping out, the kernel swap daemon
finds the page table entry in each of the sharing processes page tables (by following a pointer
from each vm_area_struct data structure). If this processes page table entry for this page of
System V shared memory is valid, it converts it into an invalid but swapped out page table entry
and reduces this (shared) page’s count of users by one. The format of a swapped out System V
shared page table entry contains an index into the set of shmid_ds data structures and an index
into the page table entries for this area of System V shared memory.
If the page’s count is zero after the page tables of the sharing processes have all been modifi ed,
the shared page can be written out to the swap file. The page table entry in the list pointed at by
the shmid_ds data structure for this area of System V shared memory is replaced by a swapped
out page table entry. A swapped out page table entry is invalid but contains an index into the
set of open swap files and the offset in that file where the swapped out page can be found. This
information will be used when the page has to be brought back into physical memory.
13.5.15 Swapping Pages In
The dirty pages saved in the swap files may be needed again, for example when an application
writes to an area of virtual memory whose contents are held in a swapped out physical page.
Accessing a page of virtual memory that is not held in physical memory causes a page fault to
occur. The page fault is the processor signalling the operating system that it cannot translate a
virtual address into a physical one. In this case this is because the page table entry describing
this page of virtual memory was marked as invalid when the page was swapped out. The
processor cannot handle the virtual to physical address translation and so hands control back to
the operating system describing as it does so the virtual address that faulted and the reason for
the fault. The format of this information and how the processor passes control to the operating
system is processor specifi c.
268 LOVELY PROFESSIONAL UNIVERSITY