memory management techniques 1)single contiguous 2)overlays 3)fixed (static) partitions 4)relocation...

32
Memory Management Techniques 1) Single Contiguous 2) Overlays 3) Fixed (static) partitions 4) Relocation (Dynamic) Partitions 5) Paging 6) Demand Paging 7) Segmented 8) Segmented/ Demand Paging For each technique, observe: Algorithms, Advantages, Disadvantages and Special Requirements

Upload: francis-george

Post on 13-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Memory Management Techniques

1) Single Contiguous2) Overlays3) Fixed (static) partitions4) Relocation (Dynamic) Partitions5) Paging6) Demand Paging7) Segmented8) Segmented/ Demand Paging

For each technique, observe: Algorithms, Advantages, Disadvantages and Special Requirements

Page 2: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

1. Single ContiguousWhile (job is ready) Do

If (JobSize <= MemorySize)Then Begin

Allocate MemoryLoad and Execute JobDeallocate MemoryEnd

Else Error

Advantages:Simplicity

DisadvantagesCPU wasted, Main memory not fully used, Limited job size

Page 3: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

II OverlaysPrograms can be sectioned into modules

Not all modules need to be in main memory at the same time

Programmer specifies which modules can overlay each other

Linker inserts commands to invoke the loader when the modules are referenced

The “parent” must stay in memory

A

C

B

D

E

Page 4: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Overlays(Cont)

Advantages:

Reduced memory usage

Disadvantages:

Overlap map must be specified by programmer

Programmer must know memory requirements

Overlapped modules must be completely disjoint

Page 5: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Fixed Partition(1)

Must ensure that a process does not access memory space dedicated to another process

Base Register – Holds the address of the beginning of the partition

Bounds Register – Hold the size of the job

Each relative address is compared to the bounds register and if in range it is added to the base register to produce a physical address

Page 6: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Fixed Partition(2)

Partition

A

A + L

+

A

Virtual Address

L

Bounds Reg

Page 7: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Dynamic Partitions(1)

Consider the following scenario (100K memory):1. Job 1 arrives; size = 22 K2. Job 2 arrives; size = 24 K3. Job 3 arrives; size = 30 K4. Job 4 arrives; size = 10 K5. Job 1 terminates6. Job 3 terminates7. Job 5 terminates

Page 8: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Dynamic Partitions(2)

Lets trace our what happens:

0

100

Where should job 5 be put? What are pros/cons of each placement?

Page 9: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Table Sort Illustration(1)

FREE – 22K

IN USE – 24K

IN USE – 10K

FREE - 30K

FREE 14K

a

c

b

d

e

Page 10: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Table Sort Illustration(2)

First fitStart addr Length A 22 C 30 E 14

Worst fitStart addr Length C 30 A 22 E 14

Best fitStart addr Length E 14 A 22 C 30

Page 11: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Partition Selection Algorithm

Implementation requires a free block table

Sorting table in a particular manner results in a specific selection algorithm

1) First FitSort by location

2) Best FitSort by size (ascending)[don’t break up big blocks)

3) Worst FitSort by size (descending)[break up big blocks]

Page 12: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

What if we cannot find a big enough hole for an arriving job?

Suppose a 35K job arrives?

Suppose a 90K job arrives?22K

24K

30K

10K

14K

Free

2

Free

4

Free

Page 13: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

CompactionShuffle jobs to create larger contiguous free memory

Do the values of pointers need changing?

Consider the arrival of a 40K job:

Job A 15K

Job B 20K

Job C 7K

Job A 15K

Job B 20K

Job C 7K

Page 14: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Dynamic PartitionsRequires two OS operations:

• Allocation:

Form a partition from a free partition of ample size

• Deallocation:

Return partition to free table and merge where possible

Page 15: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Pros/Cons of Dynamic Partitions

Advantages:

Efficient memory usage

Disadvantages:

Partition Management

Compaction or external fragmentation

Page 16: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Demand Page Management1) Page Map Table (PMT)

Maps page to blockStatus: Main Memory

Secondary MemoryIn Transit

2) Memory Block Table (MBT)Maps block to pageReference BitChange Bit

3) File Map Table (FMT)Maps a job’s pages to secondary memoryPMT for the disk1 FMT/ job1 entry / page

Page 17: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Demand Paging Schematic(1)

3

M

M

S

0

1

2

What does 3 mean?PMTAR

PMT

OS0

1

2

3

4

5

6

7

M = In Memory

S = On secondary storage

I = In transit

Page 18: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Demand Paging Schematic(2)

Disk

0

1

2

FMT

Page 19: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Demand Paging Schematic(3)

OS

J1 P0

J1 P1

0

1

2

3

4

5

6

7

R C

CR

Referenced

Changed

Page 20: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Sharing Pages of Reentrant Code or Data Between Processes

Q. What problem is caused if a shared page frame corresponds to two different page #s? (Hint: consider page replacement)

A. Alias or synonym problem

Page 21: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Address Translation

Steps:

Look up p in TLB

if not found look up p in PMT (must be found)

Replace p by p’ to form PA

Page 22: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Page Replacement

Local VS Global Page Replacement

LocalRequires that each process removes a page from its own set of allocated blocks

GlobalA replacement page may be selected from the set of all blocks

Page 23: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

LocalityAt any given time, the locality of a process is the set of pages that are actively being used together

SpacialThere is a high probability that once a location is

referenced, the one after it will be accessed in the near future

EX: Sequential code, array processing

Temporal A referenced location is likely to be accessed again in the near future

EX: Loops. Single data elements

Page 24: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Working Set Theory

W (t,) = The set of pages referenced between times t and t+

The number of elements in W is called the working Set Size

Working set size is an estimation of locality size

A job should not be scheduled unless there is room for its entire working set

Page 25: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Page Replacement Algorithms1) Optimal Replacement

Replace the page which will not be used for the longest period of timeLowest page fault rate of all algorithmsRequires future knowledge

2) FIFOReplace the “oldest” pageA frequently used page may be swapped out

Belady’s Anomaly:For some page replacement algorithms, the page fault rate may increase as the number of blocks increase

Page 26: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Page Trace: 4 3 2 1 4 3 5 4 3 2 1 5

4 3 2 1 4 3 5 4 3 2 1 5

4 3 2 1 4 3 5 4 3 2 1 5

0

1

2

0

1

2

3

Page 27: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Replacement Algorithms Continued

3) Least Recently Used (LRU) Uses the recent past as an approximation of the near future Statck algorithm Does NOT suffer from Belady’s Anomaly Hardware/ Overhead intensive

4) LRU Approximation Uses reference bits in the MBT and a static reference pointer(RP) The reference pointer is not reinitialized between calls to LRU Approximation

Page 28: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

LRU Approximation Algorithm

Initially: RP 0;Begin

Done := FALSE;While (NOT Done) Do

If (MBT[RP].ref = 1) Then MBT [RP].ref := 0 Else Begin

Return(RP);Done :== TRUE;End;

RP:=RP+1;End;

Page 29: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

LRU Approximation Example

Page Trace: 4 3 2 1 4 3 5 4 2 1 5

NOTE: Slightly different from previous example

Page Reference Bit

0

1

2

RP = 0Page Fault Rate =

Page 30: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

Page Replacement

Local VS Global page replacement

LocalRequires that each process remove a page from its own set of allocated blocks

Global A replacement page may be selected from the set of all blocks

Page 31: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

VIII Segmentation

-An alternative to paging

-Recall variable Size partitioning:

- Program allocation contiguous

- Use first fit, etc

-Segmentation:

- Put subroutines, arrays, etc. into separate segments

- Each segment must occupy contiguous locations, but segments may be scattered throughout memory

-Segmentation = Paging with variable size pages

-Why do this? What problems arise?

Page 32: Memory Management Techniques 1)Single Contiguous 2)Overlays 3)Fixed (static) partitions 4)Relocation (Dynamic) Partitions 5)Paging 6)Demand Paging 7)Segmented

VIII SegmentationDivide each process into logical segments (procedures, arrays, etc)

Logical breakdown gives an intuitive structure to main memory

Managing segments:

Segment Map Table ( SMT )

Length Address

1 SMT/job

1 entry/segment