Page 176 - DCAP103_Principle of operating system
P. 176
Unit 5: Memory Management
Virtual memory uses two methods to free up RAM when needed. Notes
• Swapping: An entire process, including code segment and data segments is expunged
from the system memory.
• Paging: Only single pages are swapped out.
Of course, the simplest way to clear a space in RAM is to terminate some processes, but virtual
memory is more subtle than that. The idea is to free RAM only temporarily, with the intention
of copying the data back again later. All of this should happen in such a way that the user of
the system do not realize that it is happening.
Swapping and paging dump the system memory in special disk caches. Normally these disk
areas are not part of the usual file system structure, since the overhead of maintaining a file
system is inappropriate when only the system needs to use the disk. Instead, the system stores
swap files in large contiguous blocks, sacrificing utilization of space for speed. Some systems
also allow swapping to a special file in the normal filesystem, which has a reserved size.
In UNIX, there both methods are available. On BSD systems, normally a whole disk partition
(see next section) is reserved for swapping and paging. (This is called the swap partition for
historical reasons.) If this fails to provide enough space, under SunOS the system administrator
can either add other partitions, or use the mkfile command to create a swap file on a normal in
a part of the file system where there is sufficient space. In the system 5 based HPUX operating
system, the normal swap area is invisible to the user. Additional swap space can simply be
grabbed from some part of the filesystem, by the kernel, if the system goes short. Eventually
this can lead to a paradoxical situation in which the user sees nothing on the disk, but the OS
declares that the disk is full!
Early versions of UNIX used swapping exclusively when RAM was in short supply. Since BSD
4.3, all systems which have learned something from the BSD project use paging as their main
method of virtual memory implementation.
5.7.2 Demand Paging — Lazy Evaluation
You might ask — if a program has a lot of pages which do not get used, what is the purpose
of loading them in the first place and then swapping them out? One could simply make a
rule that no page should be brought into memory until it were needed. Such a scheme is
possible, but few systems allow a program to run if it cannot be loaded fully into memory
on start-up. One argument against this extreme form of paging is that, it could be dangerous
to start a program which was unable to complete because it was too large to run on the
system, under the conditions of the moment. If it started to run and then crashed or exited,
it could compromise important data. (The BSD UNIX system allocates sufficient space in
its swap area to swap or page out each entire process as it begins. That way, none of them
will ever run out of swap during execution.)
On the other hand, if a program can be loaded in, it is most likely safe — so if we then discover
that large parts of the program are never used, we can page them out and never bother to page
them in again.
This is an example of what is called lazy evaluation. A lazy pager never brings a page back into
memory until is has to, i.e. until someone wants to use it. This can save a considerable amount of
I/O time. Another name for this is demand paging, since it only occurs on demand from user
processes.
It is now easy to see how the paging concept goes hand in hand with the logical memory
concept: each time the system pages out a frame of physical memory, it sets a flag in the page
table next to the logical page that was removed. If a process attempts to read from that page
of logical memory the system first examines the flag to see if the page is available and, if it is
not, a page fault occurs.
LOVELY PROFESSIONAL UNIVERSITY 169