CLICK HERE FOR THOUSANDS OF FREE BLOGGER TEMPLATES »

Monday, September 1, 2008

Virtual Memory






Windows














Windows


Linux


 


Implementation of Virtual Memory


A program instruction can address up to 4GB of
Virtual Memory, those parts of program and data which are currently in
active use need to be loaded into Physical Random Access Memory.


The processor itself translates the virtual
addresses from an instruction into the correct equivalents, as the
instruction is executed.


The processor manages the mapping in terms of pages
of 4 kilobytes each.


 


Page Fault


When a program tries to access some address that is
not currently in physical RAM, it generates an interrupt, called a Page
Fault.


This asks the system to retrieve the 4 KB page
containing the address from the page file. This, a valid page fault,
normally happens quite invisibly. Sometimes, through program or hardware
error, the page is not there either. The system then has an ‘Invalid
Page Fault’ error. This will be a fatal error if detected in a program.


If it is seen within the system itself , it may
manifest itself as a ‘blue screen’ failure with a STOP code.


STOP messages are identified by an 8-digit
hexadecimal number, but also commonly written in a shorthand notation;
e.g., a STOP 0x0000000A may also be written Stop 0xA. This 8-digit
number will help the users to determine the actual error condition.


 


Page Size


How big a file will turn out to be needed depends
very much on work-load. Simple word processing and e-mail may need very
little. Large graphics and movie making may need a great deal.


For a general workload,it is suggested that a
sensible start point for the initial size would be the greater of (a)
100 MB or (b) enough to bring RAM plus file to about 500 MB. EXAMPLE:
Set the Initial page file size to 400 MB on a computer with 128 MB RAM;
250 on a 256 MB computer; or 100 MB for larger sizes.


Windows will expand a file that starts out too
small and may shrink it again if it is larger than necessary, so it pays
to set the initial size as large enough to handle the normal needs of
your system to avoid constant changes of size. This will give all the
benefits claimed for a fixed page file.But no restriction should be
placed on its further growth.


 


Thrashing Issues


Trashing is a scheme in virtual memory where the
processes spends most of its time in swapping pages rather than
executing instructions, this is due to inordinate number of page faults.


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


Implementation of Virtual Memory


To accomplish address translation (paging) the CPU
needs to be told:


a) where to find the address translation
information. This is accomplished by pointing the CPU to a lookup table
called a 'page table'.


b) to activate paging mode. This is accomplished by
setting a specific flag in a control register.


Kernel use of virtual memory begins very early on
in the boot process. head.S contains code to create provisional page
tables and get the kernel up and running.


Every physical page of memory up to 896MB is mapped
directly into the kernel space. Memory greater than 896MB (High Mem) is
not permanently mapped, but is instead temporarily mapped using kmap and
kmap_atomic (see HighMemory).


The descriptions of virtual memory will be broken
into two distinct sections; kernel paging and user process paging.


 


Page Fault


The operating system must provide a page fault
handler that has to deal with the situation and determine how the
process may continue in order to allow access to pages of memory never
accessed before.


The Linux page fault handler relies on acquiring a
read-write semaphore (mmap_sem) and a spin lock, the page_table_lock for
synchronization between multiple threads of a task. A page fault first
acquires a read lock on mmap_sem (which alone would allow other threads
to continue processing page faults) and then acquires a spin lock on the
page_table_lock (which serializes access to the page table and important
data structures) before acquiring a free page from the page allocator.


The page is then cleared by overwriting the
contents with zeros and the page is assigned to the process by creating
a corresponding page table entry in the page table of the process.


The page fault handler is a very hot code path,
sensitive to minor code changes and depends heavily on the organization
of data structures.


 


Page Size


Virtual and physical memory are divided into handy
sized chunks called pages. These pages are all the same size, they need
not be but if they were not, the system would be very hard to
administer. Linux on Alpha AXP systems uses 8 Kbyte pages and on Intel
x86 systems it uses 4 Kbyte pages. Each of these pages is given a unique
number; the page frame number (PFN).


 


A virtual address is composed of two parts; an
offset and a virtual page frame number. If the page size is 4 Kbytes,
bits 11:0 of the virtual address contain the offset and bits 12 and
above are the virtual page frame number. Each time the processor
encounters a virtual address it must extract the offset and the virtual
page frame number. The processor must translate the virtual page frame
number into a physical one and then access the location at the correct
offset into that physical page. To do this the processor uses page
tables.


 


Trashing Issues






Linux
only
kills processes when

thrashing

occurs and the system is out of
swap space. In some sense there is nothing else that
the kernel could do in this case, since memory is
needed but there is no more physical or swap
memory to allocate. When such a case occurs,

Linux

kernel kills the most memory-consuming processes.
This feature is very drastic; hence its implications
might be severe. For example, if a server runs several
applications with mutual dependencies, killing
one of the applications may yield unexpected
results.

 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 




 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 


 



 






0 comments: