Memory Management - III

reelingripehalfSoftware and s/w Development

Dec 14, 2013 (3 years and 10 months ago)

74 views

Memory Management
-

III

Jin zhigang


Apr, 2007


Modern Operating Systems

Modern Operating Systems, Jin zhigang

2

Content


Page replacement algorithms


Multi
-
program memory management


Segmentation

Modern Operating Systems, Jin zhigang

3

Review


Page Replacement algorithms


Optimal


FIFO


LRU


NRU


Second chance

Modern Operating Systems, Jin zhigang

4

Ad Hoc Techniques


Separate Page Out from Page In.


Keep a pool of free frames.


When a page is to be paged in, use a free frame.


Read the faulting page and restart the faulting process while
page out is occurring.


Write dirty pages to disk whenever the paging device is
free and reset the dirty bit. This allows page replacement
algorithms to replace clean pages.


Cache paged
-
out pages in primary memory.


Page out dirty pages as before.


Return pages to a free pool but remember which page frame
they are.


If system needs to map page in again, reuse page.


If system needs to page in data, choose any page in pool.


System V implements this strategy


Modern Operating Systems, Jin zhigang

5

Frame Allocation: Minimum


How are the page frames allocated to individual
virtual memories in a multi
-

programmed
environment?


The simple case would be to allocate a minimum
number of frames per process.


most instructions require two operands


include an extra frame for paging out and one for
paging in


moves and indirection instructions might require more

Modern Operating Systems, Jin zhigang

6

Equal Allocation


Allocate an equal number of frames per
job


but jobs use memory unequally


high priority jobs have same number of page
frames and low priority jobs


degree of multiprogramming might vary


Modern Operating Systems, Jin zhigang

7

Proportional Allocation


Allocate a number of frames per job
proportional to job size


Challenge: how do you determine job size: by
command parameters, user etc. or
dynamically?

Modern Operating Systems, Jin zhigang

8

Page Fault Rate Curve


(
Typical) As page frames per VM space decrease, the
page fault rate increases. Exception…?

Modern Operating Systems, Jin zhigang

9

Thrashing


Computations have locality (temporal locality and spatial
locality).


As page frames decrease, the page frames available are
not large enough to contain the locality of the process.


Working set
of a process=set of pages it needs to
operate without (too many) page faults


The processes start faulting heavily (high page fault rate).


Pages are read in, are used and immediately paged out.

Modern Operating Systems, Jin zhigang

10

Thrashing and CPU Utilization


As the page rate goes up, processes get suspended on
page
-
out and page
-
in queues for the disk.


The system may try to optimize performance by starting
new jobs.


Starting new jobs will reduce the number of page frames
available to each process, increasing the page fault
requests.


System throughput


plunges.

Modern Operating Systems, Jin zhigang

11

Solution: “Working Set” Approach


The working set model
assumes locality.


The
Principle of
Locality

states that a
program clusters its
access to data and text
temporally and
spatially
.


As the number of page
frames increases above
some threshold, the
page fault rate will drop
dramatically
.


Modern Operating Systems, Jin zhigang

12

Working Set in Action


Algorithm:


if # free frames > working set of some
suspended process, then activate process


if working set size of some process increases
and no frame is free, suspend process and
release all its frames


Keep track of working set: Moving window
over reference string used for
determination, e.g., last
k

accesses by this
process


Modern Operating Systems, Jin zhigang

13

Working Set Example

12 references,
8 faults


Window size
is



Modern Operating Systems, Jin zhigang

14

Working Set Solution


Parameter:


time units= locality of reference (typically
several clock ticks)


Each page (PTE) has an R bit, and a time of last use (TOLU)
byte


Every clock int. clears R bit for each frame


Every page access updates TOLU field and sets R bit for
page


On page fault, find a victim: Scan all frames examining R bit


If (R==1) set TOLU to now (current time)


If (R==0 and age=now
-
TOLU >

) remove this page


If (R==0 and age=now
-
TOLU <=

) remember the smallest TOLU


If can’t find another page to evict, evict this “smallest one”


If all pages have R==1, evict a random page



Modern Operating Systems, Jin zhigang

15

Page Fault Frequency

Version of Working Set


Assume that if the working set is correct there
will not be many page faults.


If page fault rate increases beyond assumed
knee of curve, then increase number of page
frames available to process.


If page fault rate decreases below foot of knee of
curve, then decrease number of page frames
available to process.

Modern Operating Systems, Jin zhigang

16

Page Fault Frequency Version
of Working Set

Modern Operating Systems, Jin zhigang

17

WSClock Page Replacement
Algorithm


Carr and Hennessey, 1981


Used very widely (Linux)


Circular list of pages, with a
clock hand

(pointer) pointing
to one of these pages


Initially, list is empty.


As pages are loaded into memory, pages are added to the list


Each entry contains
Time of last use

as well as reference and
dirty bits.


Clock hand updated according to the following algorithm…

Modern Operating Systems, Jin zhigang

18

WSClock Page Replacement
Algorithm (Contd.)



Algorithm: on a page fault,


(Step 1) Examine the page pointed to by the clock hand


If R bit is 1, then the page has been referenced during the current
tick, so it is in working set and not a candidate for removal


If R bit is 0, then check if it is in working set window (i.e., if now
-
TOLU is less than the working set window size time)


If page is not in the working set and the page is clean, then replace it


If the page is not in working set and page is dirty, request write to disk,
and move on to the next page in the circular list.


If page is in Working Set, advance Clock Hand by 1 element and
Repeat (Step 1)



Clock hand may cycle around multiple times until some scheduled
page write has completed (then that page is chosen as the victim)


Modern Operating Systems, Jin zhigang

19

Page Size Considerations


Small pages


Reason


Locality of reference tends to be small (256B)


Less fragmentation (what kind


internal or external?)


Problem


Require large page tables


Large pages


Reason


Small page table


I/O transfers have high seek time, so better to transfer more
data per seek


Problem


Internal fragmentation

Modern Operating Systems, Jin zhigang

20

Segmentation


Another Virtual Memory scheme


Different from paging


Provide a user with a two dimensional virtual
memory


dimension 1: an address space or segment


dimension 2: a collection of segments


addresses look like (s,d)


s=segment number


d=offset within segment


Segment sizes are
decided by user

Modern Operating Systems, Jin zhigang

21

Segmentation

Array A

Funct B

Stack C

Array D

Stack E

Seg 1

Seg 2

Seg 3

Seg 4

Seg 5

(4,r) refers to

segment 4, location r

r

(1,x) doesn’t exist if

x is larger than segment

size

x

Modern Operating Systems, Jin zhigang

22


Use of Segments


Segment for each user entity: stack, code, data, array


Protect each segment independently: stack, code, data,
array


Allow each segment to grow independently


Share each segment independently



Cache parts of virtual memory based on execution
pattern


Avoid paging inefficiency: avoid fetching pages from disk
that may not be used

Modern Operating Systems, Jin zhigang

23

Segment Mapping

Real Memory

1

2

3

Disk

Cache

Virtual Memory Stored on Disk

1

2

3

4

5

2

3

1

Segment Table

Modern Operating Systems, Jin zhigang

24

Segment Mapping Hardware

p

+

<

s

d

(s,d)

Virtual Memory

Virtual Memory Address

d

1

1

Base

Limit

1

0

(s,d)

d

Physical Memory

Segment Table

s

Physical Addr

STBR=segment table


base register

Modern Operating Systems, Jin zhigang

25


Implementation Issues


Can cache segment table in


registers (if few segments)


or keep segment table in memory at per
-
process location given
by STBR


Length of segment table given by segment table length register
(STLR)


STBR, STLR changed at context switch time


Caching segment table entries (similar to TLB for paging)


associative registers,


look
-
aside memory


Segment hit ratio
: percentage of time segment found in
associative memory


If not found in associative memory, must load from
segment tables:


requires additional memory reference


Suffers
external

fragmentation
-

use compaction?


Modern Operating Systems, Jin zhigang

26

Sharing Segments

Modern Operating Systems, Jin zhigang

27

Sharing and Protection


Code and data must not contain segment
references if the segment VM mappings for them
are different


Can add protection to segment table to protect
memory


Check is done by hardware during access


Shared segments can have different protections
from different processes by having different
segment table protection access bits


Modern Operating Systems, Jin zhigang

28

Segment Protection

Legend:


reference


seg. has been accessed


valid


seg. exists


resident


seg. is in primary memory


dirty


seg. has been changed since page
-
in






from disk

Modern Operating Systems, Jin zhigang

29

Segmented Paged Virtual
Memory


Implement segmentation
by using paging
underneath


Addresses fragmentation problem of segmented
memory


On average, half a page per segment is wasted by
internal fragmentation


Sharing and Protection can still be provided


Can have variable segment sizes


Segment consists of multiple pages


Each page has a fixed size (some allow variable sizes
too)

Modern Operating Systems, Jin zhigang

30

Segmented Paged Virtual Memory

Modern Operating Systems, Jin zhigang

31

Sun: Example of VM Scheme

Other examples in text:

Pentium, Multics (read yourself)

Modern Operating Systems, Jin zhigang

32

Segmentation with Paging: Pentium

Protection on the Pentium