Monday, August 25, 2008

SEB 070012 - Tutorial 4

Virtual Memory: Windows VS Linux

Virtual Memory

Windows

Linux
Implement of virtual memory

How each one handles page faults, page sizes

How it reconciles thrashing issues.

All processes running under 32 bit Windows gets virtual memory addresses ( virtual address space) going from 0 to 4 GB, no matter how much RAM is actually installed on the computer. 2 GB of this virtual address space are designated for each process’ private use and the other 2 GB are shared between all processes and the operating system.

requires elevated privileges, cooperation from the application making the large allocation (usually setting a flag to ask the operating system for huge pages), or manual administrator configuration; operating systems commonly, sometimes by design, cannot page them out to disk. Windows Server 2003 (SP1 and newer), Windows Vista and Windows Server 2008 support huge pages under the name of large pages, but Windows XP does not.

When the memory in use by all the existing processes exceeds the amount of RAM available, the operating system will move pages (4 KB pieces) of one or more virtual address spaces to the computer’s hard disk

Windows uses structured exception handling to report page fault-based invalid accesses as access violation exceptions. If the program that received the error does not handle it, then Windows typically performs some sort of default action, typically involving the termination of the running process that caused the error condition, and a notification to the user that the program has malfunctioned.

To prevent thrashing, Windows provide a process with as many frames as it needs. Windows will also increase the amount of RAM in the system to eliminate the cause of trashing. Besides, it also reduce the amount of RAM needed by reconfiguring the applications, removing unneeded system services, or running fewer applications at a time.

Virtual memory divides the virtual address space into equal-sized pieces called virtual pages. Virtual pages have a fixed size that is an integer power of 2. Linux can be configured to use a size of 4, 8, 16, or 64 kb to maximize the performance. Linux kernel creates and maintains page table and also employs the CPU memory management unit (MMU) to translate the virtual address of a process into physical address in order to store the data. Linux has supported huge pages on several architectures since the 2.6 series.

Linux could create appropriate page-table entries whenever a range of virtual memory is allocated. However, this is wasteful because most programs allocate much more virtual memory than they ever use at any given time. So Linux uses a method called demand paging. With this, the virtual address space starts out empty.

Page fault is intercepted by the Linux kernel and causes the page fault handler to be activated. Linux uses signals, such as SIGSEGV, to report these error conditions to programs. Because Linux uses demand paging and page-fault-based optimizations such as copy-on-write, page faults occur during the normal course of operation and do not necessarily indicate an error. Thus, when the page fault handler is invoked, it first needs to determine whether the page fault is caused by an access to a valid page. If not, the page fault handler simply sends a segmentation violation signal to the faulting process and returns. Otherwise, it takes one of several possible actions:

-If the page is being accessed for the first time, the handler allocates a new page frame and initializes it, e.g., by reading its content from disk. Page faults caused by first-time accesses are called demand_page_faults.

-If the page has been paged out to swap space, the handler reads it back from disk into a newly allocated page frame.

-If the page fault occurred because of a page-fault-based optimization (such as copy-on-write), the handler takes the appropriate recovery action (such as performing the delayed page copy).

Linux picks a page frame that backs a virtual page that has not been accessed recently, writes it out to a special area on the disk called the swap space, and then reuses the page frame to back the new virtual page. Then marked the page swapped out as not present. Linux can support multiple swap space areas, each of which can be either an entire disk partition or a specially formatted file on an existing file system. With this swapping method, thrashing is reconciled.



References:
http://www.informit.com/articles/article.aspx?p=29961
http://en.wikipedia.org/wiki/Virtual_memory
http://en.wikipedia.org/wiki/Page_fault
http://support.microsoft.com/kb/555223
http://en.wikipedia.org/wiki/Page_size#Page_size_trade-off


No comments: