Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

Operating

Systems
MEMORY MANAGEMENT
Memory Management
•An average home computer has 10,000 times more memory than the
IBM 7094, the largest computer in the world in the early 1960s.
•Every programmer would like a private memory, of infinite size and
speed, which is also non-volatile, that is, it does not lose its content
when disconnected.
•Computers have a few megabytes of very fast, expensive, and volatile
cache memory, a few gigabytes of medium-speed, mid-priced, volatile
main memory, a few terabytes of slow, inexpensive, non-volatile disk
storage, and the removable storage, such as DVDs and USB sticks.

LUIS H. CRUZ A.
OPERATING SYSTEMS
2
Memory Management
The part of the operating system that manages
(part of) the memory hierarchy is known as the
memory manager.
Its job is to manage memory efficiently:
• Keep track of which parts of memory are in
use.
• Allocate memory to processes when they
need it.
• Deallocate it when done.

LUIS H. CRUZ A.
OPERATING SYSTEMS
3
Administration Schemes
No Memory Abstraction.
•The first mainframe computers (before 1960), the first minicomputers
(before 1970), and the first personal computers (before 1980) did not
have memory abstraction. Each program simply looked at physical
memory.
•Under these conditions, it was not possible to have two programs
running in memory at the same time. There are 3 ways to use memory
to host the OS:

LUIS H. CRUZ A.
OPERATING SYSTEMS
4
No Memory Abstraction
•Even without memory abstraction it is
possible to run multiple programs at
the same time.
•What the operating system must do is
save all the contents of memory in a
file on disk, and then fetch and run the
next program.
•As long as there is only one program in
memory at a time, there are no
conflicts.
•What the IBM 360 did as a solution was
to modify the second program
instantly, as it was loaded into memory,
using a technique known as static
relocation that consisted of using the
same memory with different In the example, 2 16Kb programs are
addresses. consecutively loaded into memory.

LUIS H. CRUZ A.
OPERATING SYSTEMS
5
Address Spaces
•An address space is the set of addresses that a process can use to
address memory.
•Each process has its own address space, independent of those
belonging to other processes (except in certain special circumstances
where processes want to share their address spaces).
•The simple solution uses a very simple version of dynamic relocation.
What it does is associate the address space of each process on a
different part of physical memory, in a simple way.

LUIS H. CRUZ A.
OPERATING SYSTEMS
6
Base and Limit Registers
•The classic solution, which has been used on
machines since the CDC 6600, is to equip each
CPU with two special hardware registers,
commonly known as the base and limit
registers.
•When these registers are used, the programs
are loaded into consecutive memory locations
where there is space and no relocation during
loading.
•A disadvantage of relocating using the base and
limit registers is the need to perform a sum and
a comparison on each memory reference.
•Base and limit registers can be used to give each
process a separate address space, as in the
example.

LUIS H. CRUZ A.
OPERATING SYSTEMS
7
Swapping
There are two general schemes for dealing with memory
overhead:
•The simplest strategy, called swapping, consists of bringing in each process
in its entirety, running it for a while, then putting it back on the disk.
•Virtual memory allows programs to run even when they are only partially in
memory.
•In the following swap example, the programs exit and enter memory as they
finish.

LUIS H. CRUZ A.
OPERATING SYSTEMS
8
Swapping
•When swapping creates multiple memory holes, it is possible to
combine them all into one large one by shifting the processes as far
down as possible.
•This technique is known as memory compaction.

LUIS H. CRUZ A.
OPERATING SYSTEMS
9
Managing Free Memory
Memory Management with Bitmaps - With a bitmap, memory is
divided into allocation units as small as a few words and as large as
several kilobytes.
For each allocation unit there is a corresponding bit in the bitmap,
which is 0 if the unit is free and 1 if it is occupied (or vice versa).

LUIS H. CRUZ A.
OPERATING SYSTEMS
10
Managing Free Memory
Memory Management with Linked Lists- The memory register is kept in
a linked list of allocated and free memory segments, where a segment
contains a process or is an empty gap between two processes.
◦ The simplest algorithm is the first fit algorithm: the memory manager scans
the list of segments until it finds a gap that is large enough.
◦ A small variation of the first fit algorithm is the next fit algorithm which
works the same way as the first fit except that it keeps track of where pointer
is each time it discovers a suitable gap.

Hole Starts in 18 Length 2 Process

LUIS H. CRUZ A.
OPERATING SYSTEMS
11
Managing Free Memory
• Another well known and widely used algorithm is the best fit algorithm.
• This algorithm searches the entire list from beginning to end and takes the
smallest suitable gap.
• Rather than splitting a large gap that might be needed later, the best fit
algorithm tries to find a gap that is close to the actual size needed, that best
matches the request and available gaps.

LUIS H. CRUZ A.
OPERATING SYSTEMS
12
Managing Free Memory
• To solve the problem of dividing almost exact matches into a process and
into a small gap, we could consider the worst-fit algorithm, that is, always
take the largest gap available so that the new gap is large enough to be
useful.
• One more allocation algorithm is the so-called quick fit, which maintains
separate lists for some of the most common requested sizes. For example,
OS could have a table with n entries, where the first entry is a pointer to the
head of a list of holes.
• With the quick fit algorithm, finding a hole of the required size is extremely
fast, but it has the same disadvantage as all the schemes that are sorted by
hole size.

LUIS H. CRUZ A.
OPERATING SYSTEMS
13
Virtual Memory
•There is a need to run programs that are too large to fit in memory and
there is certainly also a need to have systems that can support multiple
programs running at the same time, each of which fits in memory, but
collectively exceed the size of it.
•One solution that was adopted in the 1960s was to divide programs into
small parts, known as overlays.
•Some overlay systems were very complex, allowing multiple overlays in
memory at the same time.
•Overlays were kept on disk, swapping them first into memory and then
out of memory using the overlay manager.

LUIS H. CRUZ A.
OPERATING SYSTEMS
14
Virtual Memory
•The method created (Fotheringham, 1961) is
now known as virtual memory.
•The basic idea is that each program has its own
address space, which is divided into chunks
called pages.
•Each page is a contiguous range of addresses.
•These pages are associated with physical
memory, but not all of them have to be in
physical memory in order to run the program.

LUIS H. CRUZ A.
OPERATING SYSTEMS
15
Virtual Memory
•When the program refers to a part of its address space that is in physical
memory, the hardware makes the necessary association instantly.
•When the program references a part of its address space that is not in
physical memory, the operating system receives an alert to find the
missing part and rerun the failed instruction.

LUIS H. CRUZ A.
OPERATING SYSTEMS
16
Paging
Virtual memory systems use paging, which is that programs refer to a set
of memory addresses that can be found on disk.
• These addresses generated by the program are known as virtual addresses
and form the virtual address space.
• The virtual addresses do not go directly to the memory bus but instead go to
a Memory Management Unit (MMU) that associates the virtual addresses
with the physical memory addresses.

LUIS H. CRUZ A.
OPERATING SYSTEMS
17
Page Frames
•The virtual address space is
divided into fixed-size units
called pages.
•The corresponding units in
physical memory are called page
frames.
•Pages and page frames are
generally the same size.

LUIS H. CRUZ A.
OPERATING SYSTEMS
18
Page Fault
•When a program refers to addresses that are not associated
in the MMU then the CPU makes a “trap” to the OS.
•This "trap" is called a page fault.
•The operating system selects a rarely used page frame and
writes its contents back to disk (if not already there).
•Then it gets the page that was just referenced in the page
frame that was just freed, changes the association and
restarts the instruction that originated the trap.

LUIS H. CRUZ A.
OPERATING SYSTEMS
19
Page Table
•The page number is used as an index in the
page table, leading to the number of the page
frame that corresponds to that virtual page.
•If the present / absent bit is 0, a “trap” is sent
to the OS.
•If the bit is 1, the page frame number found in
the page table is copied to the highest order 3
bits of the output register, along with the 12-
bit offset, which is copied without
modification of the incoming virtual address .
•Together they form a 15-bit physical address.
•The output register is then placed on the
memory bus as the physical memory address.

LUIS H. CRUZ A.
OPERATING SYSTEMS
20
Page Table
•It is the association of virtual addresses to physical addresses.
•The virtual page number is used as an index to the page table to find
the entry for that virtual page.
•Using the result of this function, the virtual page field in a virtual
address can be replaced by a page frame field, thus forming a physical
memory address.

LUIS H. CRUZ A.
OPERATING SYSTEMS
21
Structure of a Page Table Entry
The size varies from computer to computer, but 32-bit is a common size.
• The most important field is the page frame number.
• Next we have the present / absent bit. If this bit is 1, the input is valid and can be used. If it is 0,
the virtual page to which the entry belongs is not currently in memory.
• Accessing an entry in the page table with this bit set to 0 causes a page fault.
• The protection bits indicate what type of access is allowed. In its simplest form, this field
contains 1 bit, with 0 for read / write and 1 for read-only.
• The modified and referenced bits keep track of page usage. When OS write to a page, the
hardware automatically sets the modified bit.
• The referenced bit is set every time a page is referenced, either for reading or writing.

LUIS H. CRUZ A.
OPERATING SYSTEMS
22
Speeding Up Paging
Any paging system must address two main issues:
1. The association of a virtual address to a physical address must
be fast.
2. If the virtual address space is large, the page table will be
large.
•The simplest design is to have a single page table with one entry for
each virtual page, indexed by page number.
•One disadvantage is that it is extremely expensive for the page table to
be long.
• Another is that having to load the entire page table at each context
switch affects performance.
•The entire page table can be in main memory.

LUIS H. CRUZ A.
OPERATING SYSTEMS
23
Translation Lookaside Buffers
•The solution that has been devised is to equip computers with a small hardware
device to associate virtual addresses with physical addresses without going
through the page table.
•The device, called TLB (Translation Lookaside Buffer) or sometimes associative
memory.
•It is usually located within the MMU and consists of a small number of inputs.
•These fields have a one-to-one correspondence with the fields in the page
table, except for the virtual page number, which is not needed in the page table.

LUIS H. CRUZ A.
OPERATING SYSTEMS
24
Software TLB Management
Several strategies have been developed to improve
performance on computers that perform TLB management
through software.
• One method focuses on reducing TLB failures and the cost of a TLB failure
when it does occur (Bala et al., 1994).
• To reduce TLB failures, sometimes the OS finds out "intuitively" which pages
are most likely to be used next and preload entries for them in the TLB.
• For example, when a client process sends a message to a server process on
the same computer, the server will most likely have to run soon.
• Knowing this while processing the "trap" to perform the "send" operation,
the system can also check where the code pages, data and server stack are,
associating them before they have a chance to cause TLB failures.

LUIS H. CRUZ A.
OPERATING SYSTEMS
25
Software TLB Management
• When using software TLB management, there are two
types of failures.
• A soft miss occurs when the referenced page is not in the TLB, but
in memory. All that is needed here is for the TLB to be updated.
• In contrast, a hard miss occurs when the same page is not in
memory (and certainly not in the TLB). Disk access is required to
fetch the page, which takes several milliseconds.
• A hard miss is ultimately a million times slower than a soft miss.

LUIS H. CRUZ A.
OPERATING SYSTEMS
26
Page Tables for Large
Memories
A multilevel page table has a 32-bit
virtual address that is partitioned
into a 10-bit TP1 field, a 10-bit TP2
field, and a 12-bit Offset field. Since
the offsets are 12 bits, the pages are
4KB and there are a total of 220.
• The secret of the multilevel page table
method is to avoid keeping them in
memory all the time, and especially
those that are not needed.

LUIS H. CRUZ A.
OPERATING SYSTEMS
27
Page Tables for Large
Memories
•For 64-bit computers it is required to occupy 30 million
gigabytes just for the page table.
•One solution is the inverted page table, with one entry for
each page frame in real memory, instead of having one
entry per page of virtual address space.

LUIS H. CRUZ A.
OPERATING SYSTEMS
28
Page Replacement Algorithm
•When a page fault occurs, the operating system has to
choose a page to evict (remove it from memory) and make
room for the incoming page.
•If the page to delete was modified while in memory, it must
be written back to disk to update the copy of the disk.
•However, if the page has not been modified (for example, if
it contains the text of the program), the copy is already up
to date and does not need to be rewritten.
•The page to be read only overwrites the page to be evicted.

LUIS H. CRUZ A.
OPERATING SYSTEMS
29
Optimal Algorithm
•The optimal page replacement algorithm states that the
page with the highest tag should be removed.
•If one page is not going to be used for 8 million instructions
and another is not going to be used for 6 million
instructions, removing the first will send the page fault that
will get it back as far into the future as possible.
•The only problem with this algorithm is that it cannot be
done.
•At the time of page fault, the operating system has no way
of knowing when the next reference to each page will be.

LUIS H. CRUZ A.
OPERATING SYSTEMS
30
The Not Recently Used Page
Replacement Algorithm
The algorithm NRU (Not Recently Used) classify all pages in the following
categories:
• Class 0: it has not been referenced, it has not been modified.
• Class 1: it has not been referenced, it has been modified.
• Class 2: it has been referenced, it has not been modified.
• Class 3: it has been referenced, it has been modified.
•Then remove a random page from the lowest numbered class that is not
empty.
•Implicit in this algorithm is the idea that it is better to remove a modified
page that has not been referenced in at least one clock cycle (typically
about 20 msec.) than a frequently used clean page.
•The main attraction of the NRU is that it is easy to understand, moderately
efficient to implement, and provides performance that, while not optimal,
may be adequate.

LUIS H. CRUZ A.
OPERATING SYSTEMS
31
The First-In, First-Out (FIFO)
Page Replacement Algorithm
•With the First-In, First-Out, (FIFO) low overhead page
replacement algorithm, the operating system maintains a
list of all pages currently in memory, where the most recent
arrival is in the final part and the least recent in the front
part.
•In a page fault, the page at the front is removed and the
new page is added to the bottom of the list.
•When applied to computers a page that was recently
entered and requires to be used can be removed, for this
reason it is rare that FIFO is used in its pure form.

LUIS H. CRUZ A.
OPERATING SYSTEMS
32
The Second-Chance Page
Replacement Algorithm
•A simple modification to the FIFO algorithm that avoids the problem of
discarding a frequently used page is to inspect the oldest page.
•The operation of this algorithm, known as second chance, are kept in a
linked list and are ordered based on the time they reached memory.
•What the second chance algorithm is looking for is an old page that has not
been referenced in the most recent clock interval.
•If all the pages have been referenced, the second chance algorithm
degenerates into pure FIFO.

LUIS H. CRUZ A.
OPERATING SYSTEMS
33
The Clock Page Replacement
Algorithm
•Although the second chance algorithm is reasonable, it is also
unnecessarily inefficient because OS are constantly moving pages on
the list.
•A better method would be to keep all the page frames in a circular
clock-like list.
•The hand points to the oldest page.

LUIS H. CRUZ A.
OPERATING SYSTEMS
34
The Least Recently Used (LRU)
Page Replacement Algorithm
•Pages that have not been used for a long time will likely
remain unused for a long time.
•When a page fault occurs, the page that has not been
used for the longest length of time should be
discarded.
•This strategy is known as LRU paging (Least Recently
Used, Less Recently Used).
•To fully implement LRU, OS need to keep a linked list of
all pages in memory, with the most recently used page
at the front and the least recently used at the bottom.

LUIS H. CRUZ A.
OPERATING SYSTEMS
35
The Least Recently Used (LRU)
Page Replacement Algorithm
•For a machine with n page frames, the LRU hardware can hold a matrix of n x n
bits (all are 0 initially).
•Every time page k is referenced, the hardware first sets all bits in row k to 1 and
then all bits in column k to 0.
•At any time, the row with the lowest binary value is the least recently used, the
row with the next-lowest value is the next least recent use, and so on.
•The operation of this algorithm is shown for four page frames and page
references in the following order: 0 1 2 3 2 1 0 3 2 3

LUIS H. CRUZ A.
OPERATING SYSTEMS
36
Simulating LRU in Software
•The NFU (Not Frequently Used) algorithm requires a
software counter associated with each page, which is
initially zero.
• At each clock interrupt, the operating system scans all the
pages in memory.
•For each page the bit that is 0 or 1 is added to the counter.
•Accountants keep a rough count of how often each page is
referenced.
•When a page fault occurs, the page with the lowest counter
is selected for replacement.

LUIS H. CRUZ A.
OPERATING SYSTEMS
37
Simulating LRU in Software
•The modified algorithm is known as aging.
• It takes after the first clock pulse the R bits for pages 0 through 5 that have the
values ​1, 0, 1, 0, 1, and 1, respectively (page 0 is 1, page 1 is 0, page 2 is 1, etc.).
• In other words, between pulses 0 and 1, pages 0, 2, 4 and 5 were referenced, setting
their R bits to 1, while those of the other pages stayed at 0.
• After the six corresponding counters have been shifted and the R bit has been inserted
to the left, they have the values ​shown.
• The remaining four columns show the six counters after the next four clock pulses.

LUIS H. CRUZ A.
OPERATING SYSTEMS
38
The Working Set Page
Replacement Algorithm
•Many paging systems try to keep track of the working set of
each process and make sure it is in memory before allowing
the process to run.
•This method is known as the working set model (Denning,
1970).
•It is designed to greatly reduce the proportion of page
faults.
•The process of loading pages before allowing the processes
to run is also known as prepaging.
•The working set changes over time.

LUIS H. CRUZ A.
OPERATING SYSTEMS
39
The Working Set Page
Replacement Algorithm
•To implement the working set model, the operating system needs to
keep track of which pages are in the working set.
•Having this information also immediately leads to a possible page
replacement algorithm: When a page fault occurs, OS have to find a
page that is not in the working set and evict it.
•Implementing such an algorithm requires a precise way of determining
which pages are in the working set.
•The basic idea is to find a page that is not in the working set and evict it.
•Since only pages that are in memory are considered as candidates for
eviction, this algorithm ignores pages that are not in memory.

LUIS H. CRUZ A.
OPERATING SYSTEMS
40
The Working Set Page
Replacement Algorithm
•The algorithm assumes that
the hardware must set the R
and M bits.
•Similarly, a periodic clock
interrupt is assumed to cause
software to be run to clear
the Referenced bit on each
clock pulse.
•At each page fault the page
table is scanned for a suitable
page to evict.

LUIS H. CRUZ A.
OPERATING SYSTEMS
41
The WSClock Page
Replacement Algorithm
•An improved algorithm, based on the clock algorithm but also using the
information from the working set, is known as WSClock (Carr and
Hennessey, 1981).
•The required data structure is a circular list of page frames, as in the
clock algorithm.
•At first this list is empty. When the first page loads, it is added to the list.
•As more pages are added, they are listed to form a ring.
•Each entry contains the Working Set Basic Algorithm Last Use Time
field, as well as the R bit (shown) and the M bit (not shown).

LUIS H. CRUZ A.
OPERATING SYSTEMS
42
The WSClock Page
Replacement Algorithm
•As with the clock algorithm, at
each page fault the page pointed
to by the hand is examined first.
•If the R bit is 1, the page has been
used for the current pulse, so it is
not an ideal candidate for
deletion.
•After the R bit is set to 0, the
hand advances to the next page
and the algorithm for that page is
repeated.

LUIS H. CRUZ A.
OPERATING SYSTEMS
43
Summary of Page
Replacement Algorithms

LUIS H. CRUZ A.
OPERATING SYSTEMS
44
Local versus Global Allocation
Policies
•Three processes (A, B and C) make up
the set of executable processes.
Suppose A gets a page fault. Should the
page replacement algorithm try to find
the least recently used page?
•The local page replacement algorithm
would remove the most recent page
from the same working set.
•The global algorithm would take the
most recent page from any working set.
•Thus, the number of page frames
assigned to each process varies over
time.

LUIS H. CRUZ A.
OPERATING SYSTEMS
45
Local versus Global Allocation
Policies
•In general, global algorithms work best, especially when the
size of the working set can vary over the lifetime of a
process.
•If a local algorithm is used and the working set grows, an
overpaging will occur, even with many free page frames.
•If the working set is reduced, local algorithms waste
memory.
•If a global algorithm is used, the system must continually
decide how many page frames to allocate to each process.

LUIS H. CRUZ A.
OPERATING SYSTEMS
46
Local versus Global Allocation
Policies
•If a global algorithm is used, it is possible to start each process with a certain
number of pages proportional to the size of the process, but the allocation has
to be dynamically updated as the processes run.
•One way to manage allocation is by using the PFF (Page Fault Frequency)
algorithm which indicates when to increase or decrease the allocation of pages
to a process, but does not say anything about which page to replace in a fail.
•For a large class of page replacement algorithms, including LRU, the failure rate
is known to decrease as more pages are allocated.

LUIS H. CRUZ A.
OPERATING SYSTEMS
47
Load Control
•Even with the best page replacement algorithm and optimal global
allocation of page frames to processes, it can happen that the system
thrashes.
•A good way to reduce the number of processes competing for memory
is to swap some of them to the disk and free up all the pages they are
holding.
•Swapping processes out to relieve the load on memory is reminiscent of
two level scheduling, in which some processes are put on disk and a
short-term scheduler is used to schedule the remaining processes.
•Periodically, some processes are brought in from disk and other ones
are swapped out.

LUIS H. CRUZ A.
OPERATING SYSTEMS
48
Page Size
•The page size is often a parameter that the operating system can
choose.
•Even if the hardware is designed with, for example, 512-byte pages, the
operating system can easily consider page pairs 0 and 1, 2 and 3, 4 and
5, and so on.
•Determining the best page size requires balancing several competing
factors.
•As a result, there is no overall optimal size.
•A randomly chosen segment of text, data or stack will not fill an integral
number of pages, unused space is a waste which is known as internal
fragmentation.

LUIS H. CRUZ A.
OPERATING SYSTEMS
49
Separate Instruction and Data
Spaces
•Most computers have a single address space that contains both
programs and data.
•If this address space is large enough, everything works fine.
•However, it is often too small, which forces programmers to stand on
their heads trying to fit everything in the address space.

LUIS H. CRUZ A.
OPERATING SYSTEMS
50
Separate Instruction and Data
Spaces
•On a computer with this layout, both address spaces can be paginated
independently.
•Each has its own page table, with its own mapping of virtual pages to
physical page frames.
•When the hardware wants to get an instruction, it knows that I-space
must be used and the page table in that space.
•Similarly, references to data must pass through the D-space page table.
•Apart from this distinction, having separate I-space and D-space does
not introduce any special complications and creates double the
available address space.

LUIS H. CRUZ A.
OPERATING SYSTEMS
51
Shared Pages
•In a large multiprogramming system, it is
common for several users to run the same
program at the same time.
•It is obviously more efficient to share the
pages to avoid having two copies of the
same page in memory at the same time.
•One problem is that not all pages can be
shared.
•In particular, only pages that are read-only
like program text can be shared, but data
pages cannot.
•Two processes share the same program
and its page tables.

LUIS H. CRUZ A.
OPERATING SYSTEMS
52
Shared Libraries
•In modern systems there are many extensive libraries used by many
processes, for example the library that handles the dialog for browsing
through files to be opened and various graphics libraries.
•If these libraries were statically linked with each executable program on
disk they would be even larger.
•Instead, a common technique is to use shared libraries (which are
called DLLs or Dynamic Link Libraries on Windows)
•Any functions called in the object files but not present there (e.g.,
printf) are called undefined externals and are sought in the libraries.

LUIS H. CRUZ A.
OPERATING SYSTEMS
53
Shared Libraries
•The problem with shared libraries is
that they use absolute references to
addresses, when a process invokes
them it may not find them in the
correct space.
•A better solution is to compile the
shared libraries with a special
compiler flag, to instruct the
compiler not to produce statements
that use absolute addresses.
•Instead, only instructions with
relative addresses are used.

LUIS H. CRUZ A.
OPERATING SYSTEMS
54
Mapped Files
•Shared libraries are really a special case of a more general facility called
memory-mapped files.
•The idea here is that a process can issue a system call to map a file onto
a portion of its virtual address space.
•Associated files provide an alternative model for I / O, instead of doing
reads and writes, the file can be accessed as a large array of characters
in memory.
•If two or more processes are associated with the same file and at the
same time, they can communicate through shared memory.
•Writes made by one process to shared memory are immediately visible
when the other reads from the part of its virtual address space
associated with the file.

LUIS H. CRUZ A.
OPERATING SYSTEMS
55
Cleaning Policy
•Pagination works best when there are many free page frames that can
be reclaimed at the time of page faults.
•To ensure a plentiful supply of free page frames, many paging systems
have a background process known as a paging daemon, which is idle
most of the time but wakes up periodically to inspect the state of
memory.
•Keeping a supply of page frames on hand yields better performance
than using all memory and then trying to find a frame when OS need it.
•The paging daemon ensures that all free frames are clean, so they don't
need to be written to disk in a rush when they are required.

LUIS H. CRUZ A.
OPERATING SYSTEMS
56
Virtual Memory Interface
•Yet another advanced memory management technique is distributed
shared memory (Feeley et al., 1995; Li, 1986; Li and Hudak, 1989; and
Zekauskas et al., 1994).
•The idea here is to allow multiple processes to share a set of pages over
the network, possibly (but not necessary) as a single shared linear
address space.
•When a process refers to a page that is not associated, it gets a page
fault.
•The page fault handler, which can be in kernel or user space, then
locates the host containing the page and sends it a message asking OS
to detach it and send it over the network.

LUIS H. CRUZ A.
OPERATING SYSTEMS
57
Operating System Involvement
with Paging
•There are four times the OS has to do paging-related work: when creating a
process, when running a process, when a page fault occurs, and when
terminating a process.
•When creating a process on a paging system, the OS has to determine how
large the program and data will be (at first), and create a page table for
them.
•The page table does not need to be resident when the process is swapped
out, but does need to be in memory when the process is running.
•When a page fault occurs, the OS has to read the hardware registers to
determine which virtual address caused the fault. Based on this information
OS must calculate which page is needed and locate it on disk.
•When a process finishes, the OS must free up its page table, its pages and
the disk space that the pages occupy when they are on disk.

LUIS H. CRUZ A.
OPERATING SYSTEMS
58
Locking Pages in Memory
•A process that issues a system call to read some file or device and place
it in a buffer within its address space waits for the I / O to complete, the
process is suspended, and another process is allowed to run. This other
process receives a page fault.
•If an I / O device is in the process of making a DMA transfer to that
page, deleting it will write some of the data to the buffer to which it
belongs and some to the page that was just loaded.
•One solution to this problem is to lock the pages involved in in-memory
I / O operations so that they are not deleted. Locking a page is often
called pinning it in memory.

LUIS H. CRUZ A.
OPERATING SYSTEMS
59
Backing Store
•Page replacement algorithms select a page to delete but should be
saved for when it is needed.
•The pages that are not used are stored on disk in a different position
than the one they were contiguous to make it easier to retrieve them
using only the reference to the first backed up page.

LUIS H. CRUZ A.
OPERATING SYSTEMS
60
Separation of Policy and
Mechanism
•An important tool for managing the complexity of any system is to
separate policy from mechanism.
•This principle can be applied to memory management, by making most
of the memory manager run as a user-level process.
•The memory management system is divided into three parts:
1. A low-level MMU handler.
2. A page fault handler that is part of the Kernel.
3. An external pager that runs in user space.

•Policy is largely determined by the external pager, which runs as a user


process.

LUIS H. CRUZ A.
OPERATING SYSTEMS
61
Separation of Policy and
Mechanism
• Once the process starts running, OS may
get a page fault.
• The fault handler finds out which virtual
page is needed and sends a message to the
external pager, indicating the problem.
• The external pager then reads the
necessary page from disk and copies it to a
portion of its own address space.
• The bug handler is told where the page is.
• The fault handler then deallocates the
page from the external pager's address
space and asks the MMU handler to put it
in the user's address space, in the correct
place.
• Then the user process can be restarted.

LUIS H. CRUZ A.
OPERATING SYSTEMS
62
Segmentation
•The virtual memory that we have analyzed so far is one-
dimensional, because virtual addresses go from 0 to a certain
maximum address, one address after the other.
•A compiler has many tables that are generated as compilation
proceeds.
1. The source code text that is saved for the printed listing (in batch
systems).
2. The symbol table, which contains the names and attributes of the
variables.
3. The table that contains all the integer and floating point constants used.
4. The parsing tree, which contains the program's parsing.
5. The stack used for procedure calls within the compiler.

LUIS H. CRUZ A.
OPERATING SYSTEMS
63
Segmentation
•What if a program has a much larger number of
variables than usual, but a normal amount of
everything else.
•What is really needed is a way to free the
programmer from having to manage expanding
and collapsing tables.
•A simple and extremely general solution is to
provide the machine with many completely
independent address spaces, called segments.
• Each segment consists of a linear sequence of
addresses, from 0 to a certain maximum value.
• The length of each segment can be any value from 0
to the maximum allowed.

LUIS H. CRUZ A.
OPERATING SYSTEMS
64
Segmentation
• A segmented memory for compiler tables displays five independent
segments.
• A segment might contain a procedure, or an array, or a stack, or a collection
of scalar variables, but it generally does not contain a mixture of different
types.
• A segmented memory has other advantages besides simplifying the handling
of data structures that increase or decrease their size.

LUIS H. CRUZ A.
OPERATING SYSTEMS
65
Pure Segmentation
Comparison of pagination and segmentation.

LUIS H. CRUZ A.
OPERATING SYSTEMS
66
Segmentation with Intel Pentium
Paging
•Virtual memory in the Pentium has 16K independent segments, each of
which contains up to one billion 32-bit words.
•The heart of the Pentium virtual memory consists of two tables, called
LDT (Local Descriptor Table) and GDT (Global Descriptor Table).
•Each program has its own LDT, but there is only one GDT shared by all
programs on the computer.
•The LDT describes the local segments for each program, including its
code, data, stack, and so on, while the GDT describes the segments of
the system, including the operating system itself.

LUIS H. CRUZ A.
OPERATING SYSTEMS
67
Segmentation with Intel
Pentium Paging
•When a selector is loaded into a segment register, the corresponding descriptor is
obtained from the LDT or GDT and stored in firmware registers, so it can be accessed
quickly.
•The LDT or GDT is selected first, based on bit 2 of the selector.
•The selector is then copied to an internal temporary register, and the 3 lowest-order
bits are set to 0.
•Finally, the address of the LDT or GDT table is added to it to provide a pointer to the
descriptor.
•For example, selector 72 refers to input 9 in GDT, which is at GDT address 72.

LUIS H. CRUZ A.
OPERATING SYSTEMS
68
Segmentation with Intel
Pentium Paging
• Assuming the segment is in memory and the offset is in range, the Pentium
adds the 32-bit Base field in the descriptor to the offset to form what is
known as a linear address.
• The Base field is divided into three parts and is spread throughout the
descriptor for compatibility with 286, where the Base field only has 24 bits.
• The Base field allows each segment to start at an arbitrary place within the
32-bit linear address space.

LUIS H. CRUZ A.
OPERATING SYSTEMS
69
Segmentation with Intel
Pentium Paging
• Each running program has a page directory consisting of 1024 32-bit
entries.
• It is located at an address pointed to by a global record.
• Each entry in this directory points to a page table that also is containing
1024 32-bit entries.
• Entries in the page table point to page frames.

LUIS H. CRUZ A.
OPERATING SYSTEMS
70

You might also like