Lecture 25

advertisement
Lecture 25

Reminders: Homework 5 due today, Case
Study outline with references due next
Wednesday

Posted: Homework 6, Virtual Memory Project

Questions?
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
1
Outline

Other issues in virtual memory management

Frame allocation

Global vs. local replacement

Thrashing

Working set model

Effect of program structure
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
2
Frame Allocation


How is the fixed amount of memory allocated
among the various running processes?
Consider simplest case of a single-user system.
For example, 128KB of memory divided into
128 1KB pages. The OS may need as many as
35 frames, leaving a minimum of 93 frames for
the user process.


What if the OS does not use 35 frames at all times?
Should the OS compete with user process?
If not, what would be a fair division of the frames?
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
3
Frame Allocation


In general, what would a fair division of frames
among multiple processes?
Some things to note:


Maximum allocation is the maximum of the system
Performance is better with more frames. In
particular, for any particular architecture, can
determine the minimum number of frames needed
to avoid really bad performance. E.g., PDP-11
move instruction could straddle two pages and have
two indirect address operands, thus requiring 6
pages for one instruction.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
4
Frame Allocation


What are some ways to divide m frames among
n processes?
How do we make the division "fair"?
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
5
Frame Allocation


Equal allocation gives equal-sized shares to
each process. I.e. m/n frames per process.
Any leftover frames can be used as the buffer
pool. Continuing the example, 93 frames
divided among 5 processes gives 18
frames/process and a 3 frame buffer pool.
Of course, not all programs are the same size.
Suppose 2 processes, one is 10KB (like an
editor) and another is 127KB (like a database).
Conceptually, "wastes" 36 (93/2 - 10) frames.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
6
Frame Allocation

Can do proportional allocation instead, where
allocation is based on size of each process' VM
size as follows:

Let the VM size of process Pi be si.

Define S = sum of all si

Allocate ai frames to Pi where ai = si / S x m
Or the minimum number of frames, whichever is
larger.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
7
Frame Allocation



Continuing the example, this would allocate
10/137 x 93 = 7 frames to the 10KB process
and 127/137 x 93 = 86 frames to the 127KB
process.
The actual allocation will depend on the amount
of multiprogramming. Also additional
processes reduce the allocation to existing
processes and vice versa.
Could also proportionally allocated on the basis
of priority or other factors rather than size.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
8
Global vs. Local Replacement



In addition to allocation, need to decide how to
handler page replacement when there are
multiple processes.
Global replacement - choose victim from the
set of all frames, even ones currently allocated
to another process
Local replacement - choose victim from set of
frames currently allocated to the process.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
9
Global vs. Local Replacement



Local replacement seems "fairer", but if frames
are unused, results in lower utilization
Global replacement causes the page fault rate
of a process to be affected by other processes.
It also can cause a process to lose "too many"
pages and fall below the minimum, especially in
priority-based schemes.
But generally, global replacement increases
throughput, so is more commonly used (and is
what is to be simulated in the final project).
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
10
Thrashing



When a process loses too many pages under
global replacement, the system should suspend
the process by swapping it out and releasing its
remaining frames.
Later the process can be swapped back in and
resumed. The intermediate CPU scheduler
handles this.
"Not enough" is simply more active pages than
allocated frames. Then each new reference
causes a page fault that throws out a page that
will be used soon.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
11
Thrashing


This high paging activity is called thrashing.
The process/system spends more time paging
than executing. CPU utilization is very low.
Cause of thrashing is:


Too much multiprogramming - processes keep
stealing frames from each other, or the fixed
number of frames is not enough for current activity.
Even worse, some systems may interpret low CPU
utilization as a need to increase multiprogramming.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
12
Thrashing

Difference is that there is very high disk activity
when process/system is thrashing. (Causes a
"thrashing" noise as disk seeks from frame to
frame on the backing store.)
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
13
Thrashing



When a local replacement strategy is used,
thrashing may be limited to one process.
However, this may still affect overall paging
performance, since the backing store is shared
by all processes.
The only way to deal with thrashing is to reduce
the amount of multiprogramming. I.e., suspend
or abort some processes to release memory
resources.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
14
Working Set Model



How does a system determine if it is thrashing
or could start thrashing if it admits another
process?
The locality model says that as a program
executes, it moves from one locality to another
where is locality is a set of pages. (Have
always assumed this.)
Localities are defined by program and data
structures. If do not allocate enough frames for
current locality, process thrashes.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
15
Working Set Model


Model locality using a working set model.
Define a parameter  called the working set
window. The working set is then the set of
pages referenced in the last  references.
Idea is that if a page currently is active, it will be
in the working set. After last use, the page
drops out of the working set after  references.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
16
Working Set Model

For example, suppose  = 10, and we have the
following reference string:
…2615777751…3444343444…
<------------------>|
WS(t1) = {1,2,5,6,7}

<------------------>|
WS(t2) = {3,4}
Of course,  must be the correct size. Too
small and will still get thrashing. Too large and
will have low utilization as unused pages
continue to take up frames.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
17
Working Set Model

The system needs to keep track of the total
demand for frames. Define WSSi as the
working set size for process Pi, then the total
demand D =  WSSi

If D > m, the thrashing will occur and system
needs to suspend a process. If D < m by
enough frames, system can initiate a new
process or resume a suspended one.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
18
Page-Fault Frequency

A more direct way of preventing thrashing is to
just keep track of the page-fault rate of each
process.


When it is excessively high, process needs more
frames. Either allocate more frames or suspend.
When it is very low, process has too many frames,
so free some.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
19
Pre-Paging



Pure demand paging incurs a large number of
page faults at the beginning of process
execution.
Pre-paging (or pre-fetching) tries to bring in
more than one page to prevent this. Some
OS's pre-page small files. Can also pre-page
the working set of a resumed process.
Main question is whether cost of pre-paging is
less than cost of page faults, since not all of the
pre-loaded pages may be used.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
20
Program Structure


Usually programmers do not know or care
about memory management. But sometimes
how a program is written can have great effect.
Suppose we declare a 2D array:
int array [1024][1024];

In most languages, most notably except
FORTRAN, this array is laid out in row major
order. Suppose ints are 4 bytes and pages are
4KB bytes. Each row takes up a page as
shown on the next slide.
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
21
Program Structure
array[0][0]
array[0][1]
:
array[0][1023]
­­­­­­­­­­­­­­­­­­­­­­­ page boundary
array[1][0]
array[1][1]
:
array[1][1023]
­­­­­­­­­­­­­­­­­­­­­­­ page boundary
array[2][0]
:
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
22
Program Structure

Consider initializing this array with 0's. Could
write:
for (int i = 0; i < 1024; i++)
for (int j = 0; j < 1024; j++)
array[i][j] = 0;

How many page faults will this cause?
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
23
Program Structure

Also could write:
for (int j = 0; j < 1024; j++)
for (int i = 0; i < 1024; i++)
array[i][j] = 0;

How many page faults will this cause?
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
24
Program Structure

Careful selection of data and programming
structures can increase locality and hence
lower page-fault rates and size of working set.
Consider whether the following are "good" or
"bad" with regard to page faults:







Stack
Hashed symbol table
Sequential search
Binary search
Pure code
Vector operations
Indirection (pointers)
Wednesday, March 14
CS 470 Operating Systems - Lecture 25
25
Download