Page 288 - DCAP403_Operating System
P. 288
Unit 13: Case Study: Linux
Each process that wishes to share the memory must attach to that virtual memory via a system Notes
call. This creates a new vm_area_struct data structure describing the shared memory for this
process. The process can choose where in its virtual address space the shared memory goes or it
can let Linux choose a free area large enough. The new vm_area_struct structure is put into the
list of vm_area_struct pointed at by the shmid_ds. The vm_next_shared and vm_prev_shared
pointers are used to link them together. The virtual memory is not actually created during the
attach; it happens when the first process attempts to access it.
Figure 13.11: System V IPC Shared Memory
The first time that a process accesses one of the pages of the shared virtual memory, a page
fault will occur. When Linux fi xes up that page fault it fi nds the vm_area_struct data structure
describing it. This contains pointers to handler routines for this type of shared virtual memory.
The shared memory page fault handling code looks in the list of page table entries for this shmid_
ds to see if one exists for this page of the shared virtual memory. If it does not exist, it will allocate
a physical page and create a page table entry for it. As well as going into the current process’s
page tables, this entry is saved in the shmid_ds. This means that when the next process that
attempts to access this memory gets a page fault, the shared memory fault handling code will use
this newly created physical page for that process too. So, the first process that accesses a page of
the shared memory causes it to be created and thereafter access by the other processes cause that
page to be added into their virtual address spaces.
When processes no longer wish to share the virtual memory, they detach from it. So long as other
processes are still using the memory the detach only affects the current process. Its vm_area_
struct is removed from the shmid_ds data structure and deallocated. The current process’s page
tables are updated to invalidate the area of virtual memory that it used to share. When the last
process sharing the memory detaches from it, the pages of the shared memory current in physical
memory are freed, as is the shmid_ds data structure for this shared memory.
Further complications arise when shared virtual memory is not locked into physical memory. In
this case the pages of the shared memory may be swapped out to the system’s swap disk during
periods of high memory usage.
LOVELY PROFESSIONAL UNIVERSITY 281