mmu
mmu
• Rather they tend to think of their memory in multiple segments, each dedicated to
a particular use, such as code, data, the stack, the heap, etc.
• For example, a C compiler might generate 5 segments for the user code, library code,
global ( static ) variables, the stack, and the heap, as shown in Figure
Hardware
A segment table maps segment-offset addresses to physical addresses, and
simultaneously checks for invalid addresses.
Each entry in the segment table has a segment base and a segment limit. The segment
base contains the starting physical address where the segment resides in memory,
whereas the segment limit specifies the length of the segment.
• A logical address consists of two parts: a segment number, s, and an offset into
that segment, d. The segment number is used as an index to the segment table. The
offset d of the logical address must be between 0 and the segment limit.
When an offset is legal, it is added to the segment base to produce the address in
physical memory of the desired byte.
The segment table is thus essentially an array of baseand limit register pairs.
Virtual-Memory Management
In practice, most real processes do not need all their pages, or at least not all at once,
for several reasons:
1. Error handling code is not needed unless that specific error occurs, some of
which are quite rare.
2. Certain features of certain programs are rarely used.
The ability to load only the portions of processes that are actually needed has several
benefits:
Programs could be written for a much larger address space (virtual
memory space) than physically exists on the computer.
Because each process is only using a fraction of their total address space,
there is more memory left for other programs, improving CPU
utilization and system throughput.
Less I/O is needed for swapping processes in and out of RAM, speeding
things up.
The figure below shows the general layout of virtual memory, which can be much
larger than physical memory:
The figure below shows virtual address space, which is the programmer’s logical
view of process memory storage. The actual physical layout is controlled by the
process's page table.
Virtual memory also allows the sharing of files and memory by multiple processes,
with several benefits:
System libraries can be shared by mapping them into the virtual address
space of more than one process.
Processes can also share virtual memory by mapping the same block of
memory to more than one process.
Process pages can be shared during a fork( ) system call, eliminating the
need to copy all of the pages of the original ( parent ) process.
Demand Paging
The basic idea behind demand paging is that when a process is swapped in, its pages
are not swapped in all at once.
Rather they are swapped in only when the process needs them. ( on demand. ) This is
termed as lazy swapper, although a pager is a more accurate term.
The basic idea behind demand paging is that when a process is swapped in, the pager
only loads into memory those pages that is needed presently.
Pages that are not loaded into memory are marked as invalid in the page
table, using the invalid bit. Pages loaded in memory are marked as valid.
If the process only ever accesses pages that are loaded in memory (
memory resident pages ), then the process runs exactly as if all the
pages were loaded in to memory.
On the other hand, if a page is needed that was not originally loaded up, then a page
fault trap is generated, which must be handled in a series of steps:
1. The memory address requested is first checked, to make sure it was a valid memory
request.
2. If the reference is to an invalid page, the process is terminated. Otherwise, if the
page is not present in memory, it must be paged in.
3. A free frame is located, possibly from a free-frame list.
4. A disk operation is scheduled to bring in the necessary page from disk.
5. After the page is loaded to memory, the process's page table is updated with the new
frame number, and the invalid bit is changed to indicate that this is now a valid page
reference.
6. The instruction that caused the page fault must now be restarted from the
beginning.
In an extreme case, the program starts execution with zero pages in memory. Here NO
pages are swapped in for a process until they are requested by page faults. This is
known as pure demand paging.
The hardware necessary to support demand paging is the same as for paging and
swapping: A page table and secondary memory.
Effective access time = p * time taken to access memory in page fault+ (1-p)* time
taken to access memory
= p * 8000000 + ( 1 - p ) * ( 200 )
= 200 + 7,999,800 * p
Even if only one access in 1000 causes a page fault, the effective access time drops from
200 nanoseconds to 8.2 microseconds, a slowdown of a factor of 40 times. In order to
keep the slowdown less than 10%, the page fault rate must be less than 0.0000025, or
one in 399,990 accesses.
Copy-on-Write
The idea behind a copy-on-write is that the pages of a parent process is shared
by the child process, until one or the other of the processes changes the page.
Only when a process changes any page content, that page is copied for the child.
Some systems provide an alternative to the fork( ) system call called a virtual
memory fork, vfork( ). In this case the parent is suspended, and the child
uses the parent's memory pages. This is very fast for process creation, but
requires that the child not modify any of the shared memory pages before
performing the exec( ) system call.
Page Replacement
In order to make the most use of virtual memory, we load several processes into
memory at the same time. Since we only load the pages that are actually needed
by each process at any given time, there are frames to load many more processes
in memory.
If some process suddenly decides to use more pages and there aren't any free
frames available. Then there are several possible solutions to consider:
1. Adjust the memory used by I/O buffering, etc., to free up some frames for user
processes.
2. Put the process requesting more pages into a wait queue until some free frames
become available.
3. Swap some process out of memory completely, freeing up its page frames.
4. Find some page in memory that isn't being used right now, and swap that page only
out to disk, freeing up a frame that can be allocated to the process requesting it. This
is known as page replacement, and is the most common solution. There are many
different algorithms for page replacement.
The previously discussed page-fault processing assumed that there would be free
frames available on the free-frame list. Now the page-fault handling must be modified
to free up a frame if necessary, as follows:
3. Read in the desired page and store it in the frame. Change the entries in page table.
If the page is not modified the bit is not set. If the dirty bit has not been set, then the
page is unchanged, and does not need to be written out to disk. Many page replacement
strategies specifically look for pages that do not have their dirty bit set.
There are two major requirements to implement a successful demand paging system.
A frame-allocation algorithm and a page-replacement algorithm.
The former centers around how many frames are allocated to each process, and the
latter deals with how to select a page for replacement when there are no free frames
available.
The overall goal in selecting and tuning these algorithms is to generate the
fewest number of overall page faults. Because disk access is so slow relative to
memory access, even slight improvements to these algorithms can yield large
improvements in overall system performance.
Or a FIFO queue can be created to hold all pages in memory. As new pages are
brought in, they are added to the tail of a queue, and the page at the head of the
queue is the next victim.
In the following example, a reference string is given and there are 3 free frames.
There are 20 page requests, which results in 15 page faults.
The discovery of Belady's anomaly lead to the search for an optimal page-
replacement algorithm, which is simply that which yields the lowest of all
possible page-faults, and which does not suffer from Belady's anomaly.
Such an algorithm does exist, and is called OPT or MIN. This algorithm is
"Replace the page that will not be used for the longest time in the future."
The same reference string used for the FIFO example is used in the example
below, here the minimum number of possible page faults is 9.
LRU is considered a good replacement policy, and is often used. There are two simple
approaches commonly used to implement this:
2. Stack. Another approach is to use a stack, and whenever a page is accessed, pull
that page from the middle of the stack and place it on the top. The LRU page will always
be at the bottom of the stack. Because this requires removing objects from the middle
of the stack, a doubly linked list is the recommended data structure.
d) LRU-Approximation Page
Replacement
Many systems offer some degree of hardware support, enough to approximate
LRU.
In particular, many systems provide a reference bit for every entry in a page
table, which is set anytime that page is accessed. Initially all bits are set to zero,
and they can also all be cleared at any time. One bit distinguishes pages that
have been accessed since the last clear from those that have not been accessed.
If a page is found with its reference bit as ‘0’, then that page is selected as the
next victim.
f the reference bit value is ‘1’, then the page is given a second chance and its
reference bit value is cleared (assigned as‘0’).
Thus, a page that is given a second chance will not be replaced until all other
pages have been replaced (or given second chances). In addition, if a page is
used often, then it sets its reference bit again.
This algorithm is also known as the clock algorithm.
This algorithm searches the page table in a circular fashion, looking for the first
page it can find in the lowest numbered category. i.e. it first makes a pass
looking for a ( 0, 0 ), and then if it can't find one, it makes another pass looking
for a ( 0, 1 ), etc.
The main difference between this algorithm and the previous one is the
preference for replacing clean pages if possible.
There are several algorithms based on counting the number of references that have
been made to a given page, such as:
Least Frequently Used, LFU: Replace the page with the lowest
reference count. A problem can occur if a page is used frequently initially
and then not used any more, as the reference count remains high. A
solution to this problem is to right-shift the counters periodically,
yielding a time-decaying average reference count.
Most Frequently Used, MFU: Replace the page with the highest
reference count. The logic behind this idea is that pages that have already
been referenced a lot have been in the system a long time, and we are
probably done with them, whereas pages referenced only a few times
have only recently been loaded, and we still need them.
f) Page-Buffering Algorithms
Maintain a certain minimum number of free frames at all times. When a page-
fault occurs, go ahead and allocate one of the free frames from the free list first,
so that the requesting process is in memory as early as possible, and then select
a victim page to write to disk and free up a frame.
Keep a list of modified pages, and when the I/O system is idle, these pages are
written to disk, and then clear the modify bits, thereby increasing the chance of
finding a "clean" page for the next potential victim and page replacement can
be done much faster.