Monday, September 1, 2008

SEB070024-Amali 4

Comparison of how Windows and a UNIX/Linux operating system inplements virtual memory.

New Page 1

Windows operating system

UNIX/Linux operating system

Hardware Abstraction Layer: All OSes have a layer called the hardware abstraction layer

(HAL) which does the system-dependent work, and thus enables the rest of the kernel to be coded in platform independent fashion. This eases porting it to other platforms.

Copy-on-write: When a page is to be shared, the system uses only one page with both processes sharing that same copy of the page. However, when one of the process does a write onto the page, a private copy is made for that process, which it can then manipulate individually. This gives a lot better efficiency

Shadow paging: A shadow object is created for an original object such that the shadow object has some of its pages modified from the original object, but shares the rest of the pages with the original object. They are formed as a result of Copy-On-Write action.

A Background daemon: There exists a background daemon which is invoked periodically

and performs tasks like page flushing, freeing unused memory, etc.

Memory mapped Files: A file can be mapped onto memory, which then can be used with simple memory read/write instructions.

Inter-Process Communication: The memory mapped files are allowed to be then shared between processes forming a method for interprocess communication.

The system used by Windows in this case it too sophisticated and complicated.

Up to Linux 2.2, the Linux VM had focused on simplicity and low overhead. Hence it was rather quite primitive and had many problems, especially under heavy load

Windows uses clustered demand paging for fetching pages.

Linux uses a demand paged system with no prepaging. using a disk as an extension of RAM so that the effective size of usable memory grows correspondingly.

In Clustered demand paging, the pages are only brought to memory when they are required. Also, instead of bring 1, Windows, often brings a cluster of them of 1-8 pages, depending on the current state of the system.

When a process starts in Unix, not all its memory pages are read in from the disk at once. Instead, the kernel loads into RAM only a few pages at a time. After the CPU digests these, the next page is requested. If it is not found in RAM, a page fault occurs, signaling the kernel to load the next few pages from disk into RAM. This is called demand paging and is a perfectly normal system activity in Unix.

Windows NT and its variants employ a dynamically allocated pagefile for memory management. A pagefile is allocated on disk, for less frequently accessed objects in memory, leaving more RAM available to actively used objects. This scheme suffers from slow-downs due to disk fragmentation, which hampers the speed at which the objects can be brought back into memory when they are needed.

Most hard drive installations of Linux utilize a "swap partition", where the disk space allocated for paging is separate from general data, and is used strictly for paging operations. This reduces slowdown due to disk fragmentation from general use.

Uses the clock algorithm for the page replacement

Until kernel version 2.2, Linux used NRU algorithm for page replacement, but due to the various shortcomings of the algorithm, they have changed it and implemented an approximate Least Recently Used in 2.4.

The clock algorithm used by Windows is local. When a page fault occurs, and faulting process’s working set is below a minimum threshold, then the page is simply added to the working set. On the

other hand, if the working set is higher than one another threshold, then it reduces the size of working set. Hence the algorithm can be called global. But the system does do some global optimizations too.

For example, it increases the working set of processes that are causing a large number of page faults, and decreasing the working set for those who do not require enough memory.

The aging to effect LRU is brought about by increasing the age (a counter associated with a page) of a page by a constant when the page is found to be referenced during a scan, and, decreased exponentially

(divided by 2) when found not to have been referenced. This method approximates LRU fairly well.

Instead of just working when there is a page fault, just like Unix, Windows has a daemon thread working too, but called in this case as Balance Set Manager. This is invoked every 1 second, and it checks

whether there is enough free memory. If there is not, then it invokes the working set manager. The working set manager maintains to keep the free memory above threshold. It checks the working sets of process from old and big to the young and small. And depending on how many page faults they have generated, it increases or decreases them. If a page’s reference bit is clear, then counter associated with the page

is incremented. If the reference bit is set, the counter

The kernel will write the contents of a currently unused block of memory to the hard disk so that the memory can be used for another purpose. When the original contents are needed again, they are read back into memory. This is all made completely transparent to the user; programs running under Linux only see the larger amount of memory available and don't notice that parts of them reside on the disk from time to time. Of course, reading and writing the hard disk is slower (on the order of a thousand times slower) than using real memory, so the programs don't run as fast. The part of the hard disk that is used as virtual memory is called the swap space.

The kernel receives 5 kinds of page faults -1. The page referenced is not committed.

2. A protection violation has occurred.

3. A shared page has been written.

4. The stack needs to grow.

5. The page referenced is committed but

not currently mapped in.

The first two are irrecoverable errors. The third indicates an attempt to write to read-only page. Copy that page somewhere else and make the new one read/write. This is how copy-on-write works. The fourth needs to be responded by finding an extra page.

When a page fault occurs, the processor's memory management unit (MMU) signals the kernel through an exception and asks it to handle the fault: this can either result in a resolved page fault or in an error. Assuming that all memory accesses are correct (and hence there are no errors), the kernel needs to bring the requested page into memory.

Windows uses structured exception handling to report page fault-based invalid accesses as access violation exceptions

UNIX systems typically use signals, such as SIGSEGV, to report these error conditions (page faults) to programs. UNIX operating systems typically report these conditions to the user with error messages such as "segmentation violation", or "bus error".

Windows divides the list of pages into four lists:-

1. Modified Page List

2. Stand-bye Page list

3. Free Page list

4. Zeroed Page List

The first is list of dirty pages, stand-bye is a list of clean pages, are currently associated with a process. Whereas Free Pages are those clean pages which are not even associated with some process. The Zeroed list is the list of zeroed out pages, if needed. The transitions between these lists is handled by working set manager and some other daemon thread such as - swapper thread, mapped page write and

modified page writer.

Linux 2.4 divides the virtual pages into 4 lists

1. Active list

2. Inactive-dirty list

3. Inactive-clean list

4. Free list

To separate the pages which were chosen for eviction, the inactive-dirty list was made. Normally, the active pages are on the list 1. But as time passes, if some of the pages are not active, then their age decreases and goes down to 0, which indicates it is a candidate for eviction. Such pages are moved from list 1 to list 2.

All the three systems distribute the process virtual address space in a similar manner. Higher part of it is used by the kernel, while the process can use the lower part. The kernel part of the space of all process

usually point to the same kernel code. So while switching a process, we need to switch the page table entries of the lower part, while the upper part can remain the same. In Windows, 2GB are kept for both process and kernel.

In Linux 3GB is kept for the process and 1 GB given to the kernel,

Windows NT provides a page-based virtual memory management scheme that allows applications to realize a 32-bit linear address space for 4 gigabytes (GB) of memory. As a result, each application has its own private address space from which it can use the lower 2 GB—the system reserves the upper 2 GB of every process's address space for its own use.

The Unix kernel divides the memory into manageable chunks called pages. A single page of memory is usually 4096 or 8192 bytes (4 or 8KB). Memory pages are laid down contiguously across the physical and virtual memory.

Windows NT's memory management system virtualizes memory such that to each application it appears as though there is 2 GB of memory available, regardless of how much physical memory actually exists. In order to do this, Windows NT must manage memory in the background without regard to the instantaneous requests that each application makes. In fact, the memory manager in Windows NT is a completely independent process consisting of several threads that constantly manage available resources.

Paging: When a process starts in Unix, not all its memory pages are read in from the disk at once. Instead, the kernel loads into RAM only a few pages at a time. After the CPU digests these, the next page is requested. If it is not found in RAM, a page fault occurs, signaling the kernel to load the next few pages from disk into RAM. This is called demand paging and is a perfectly normal system activity in Unix. (Just so you know, it is possible for you, as a programmer, to read in entire processes if there is enough memory available to do so.)





Reference:

http://www.answers.com/topic/page-fault
http://msdn.microsoft.com/en-us/library/ms810616.aspx
http://en.wikipedia.org/wiki/Comparison_of_Windows_and_Linux
http://www.inf.fu-berlin.de/lehre/SS01/OS/Lectures/Lecture14.pdf
http://ldp.linux.no/LDP/sag/html/vm-intro.html
http://www.uwsg.iu.edu/UAU/memory/pageswap.html

No comments: