• Aucun résultat trouvé

How the Kernel Executes Processes using Demand Paging

A compiled program has a header containing information on the size of the data and code regions.

As a process is created from the compiled code (by fork and exec), the kernel sets up its data structures and the process starts executing its instructions from user mode.

A page fault occurs when the process tries to access an address that is not currently in main memory.

The kernel switches execution from user mode to kernel mode and tries to resolve the page fault by locating the pregion containing the sought-after virtual address. The kernel then uses the pregion's offset and region to locate information needed for reading in the page.

In main memory, the kernel also looks for a free page in which to load the requested page. If no free page is available, the system swaps or pages out selected used pages to make room for the requested page.

The kernel then retrieves (pages in) the required page from file space on disk.

It also often pages in additional (adjacent) pages that the process might need.

Then the kernel sets up the page's permissions and protections, and exits back to user mode. The process executes the instruction again, this time finding the 7 page and continuing to execute.

Pages are not loaded in memory until they are "demanded" by a process-hence the term, demand paging.

copy-an-write

An HP- UX enhancement to earlier UNIX implementations is the technique of

"copy-on-write." The system used to copy the entire data segment of a process every time the process forked, increasing fork time as the size of the data and code segments increased.

On the Series 300/400, HP- UX implements copy-on-write, which enables the system to create processes more quickly. When a process forks, the parent process makes a duplicate image of itself at child virtual addresses. But instead 7 -24 Memory Management

of actually copying the pages, the parent marks them "copy-on-write." The pages continue to reside in the parent address space, and are not copied until the process actually writes on t.he page. Then, the old page is copied to a new page, giving the process access to the new page. More specifically, data pages being copied-on-write are shared until written, while for code pages, a private copy is created on write.

On Series 700/800 systems, HP- UX implements a variation called

copy-on-access. Only one translation of a physical page is maintained; the parent process can point to and read a physical page, but copies it only when writing on the page. The child process does not have a page translation and must copy the page for either read or write access.

Maintaining Page Availability-vhand and swapper Daemons Two daemons (background processes )-vhand and swapper-are involved in paging; the actual paging being performed by vhand, which is also known as the pageou t daemon.

vhand monitors free pages and tries to keep their number above a threshold (shown in Figure 7-10). This ensures sufficient memory for the most efficient operation of demand paging.

LG200211_ 006

Figure 7-9. The Pageout Daemon vhand

Memory Management 7-25

7

7

As shown in Figure 7-9 the pageout daemon is like a "two-handed" clock, with the distances between the hands representing the size of main memory available to user processes. The first hand clears reference bits on a group of pages in an active pregion. If the bits are still clear by the time the second hand reaches them, the pages are paged out.

On the Series 800, the distance between the two hands is governed by a factory-set parameter called vhandsk, or "vhand skew." The larger the parameter, the longer pages can reside in main memory without being paged out.

On the Series 300/400/700, the kernel automatically keeps an appropriate distance between the hands.

LG200211_ 007

100% free .... , . - - - , - - - -...

memory at boot-up

Decreasing Available

Memory

vhand begins .... ...,...,....,...,...,..,..."...-II"'"""'~,..,...,....,~

paging

swapper begins .... ~~~~~~~~~~

swapping

Figure 7-10. Maintaining Memory Availability

vhand decides when to start paging by determining how much free memory is available.

7 -26 Memory Management

At the upper limit of the value gpgslim, paging occurs. The swapper daemon's upper limit is desiree. If free memory falls below desiree, the swapper detects the condition and becomes active also. The swapper deactivates processes and prevents them from running, thus reducing the rate at which new pages are accessed. Once STNapper detects that available memory has risen above desiree, swapper reactivates the deactivated processes and continues monitoring memory availability.

Thrashing

On systems with very demanding memory needs (for example, systems that run many large processes), the paging daemons can become so busy swapping pages in and out that the system spends too much time swapping and not enough time running processes.

When this happens, system performance degrades rapidly, sometimes to such a degree that nothing seems to be happening. At this point, the system is said to be thrashing, because it is doing more overhead than productive work.

Consider, for example, that your system might be thrashing because it is handling a lot of disk activity. Perhaps a data base is in constant use, yet on the same disk is swap space. (On the Series 800, you can check system activity using the sar( 1) command; see the manual page in the HP - UX Reference.) If so, you can minimize disk activity by moving a busy file system to a different disk, so that you are distributing busy disk activity among different spindles.

In many cases, even this approach is insufficient. Thrashing is often eliminated 7 by adding more main memory to the system, thus alleviating the need to swap.

This reduces the amount of time the system spends paging and swapping.

Memory Management 7·27

How the Memory-Management System Handles