operating systems and computer networks memory management · partitioning, paging, segmentation...

71
Operating Systems and Computer Networks Memory Management Prof. Dr.-Ing. Axel Hunger Alexander Maxeiner, M.Sc. Institute of Computer Engineering Faculty of Engineering University Duisburg-Essen Dr.-Ing. Pascal A. Klein [email protected]

Upload: dinhdiep

Post on 28-Aug-2019

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Operating Systems and Computer Networks

Memory Management

Prof. Dr.-Ing. Axel HungerAlexander Maxeiner, M.Sc.

Institute of Computer EngineeringFaculty of Engineering

University Duisburg-Essen

Dr.-Ing. Pascal A. Klein

[email protected]

Page 2: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

2Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

1 – Swapping|

2 – Segmentation Algorithms|

3 – Memory Allocation|

4 – Virtual Memory|

5 – Paging|

6 – Page Replacement Algorithms

Agenda

Page 3: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

3Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Goals of Memory Management

Convenient abstraction for programming

Allocation of scarce memory resources among competing processes

maximize performance with minimal overhead

Mechanisms

Physical and virtual addressing

Partitioning, Paging, Segmentation

Page replacement algorithms

Memory Management

Page 4: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

4Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory Hierarchy (1)

Power on

Power onvery short term

Power offshort term

Power offmid term

Power offLong term

Power onImmediate term

small sizesmall capacity

small sizesmall capacity

medium sizemedium capacity

medium sizelarge capacity

Large sizevery large capacity

Large sizevery large capacity

processor registersvery fast, very expensive

processor cachevery fast, very expensive

Random access memoryfast, affordable

hard drivesslow, very cheap

flash / USB memoryslower, cheap

Tape backupvery slow, affordable

Page 5: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

5Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory Hierarchy (2)

Power on

Power onvery short term

Power offmid term

Power onImmediate term

small sizesmall capacity

small sizesmall capacity

medium sizemedium capacity

Large sizevery large capacity

processor registersvery fast, very expensive

processor cachevery fast, very expensive

Random access memoryfast, affordable

hard drivesslow, very cheap

Page 6: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

6Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Allocation

Protection from each other, Protecting OS

Translating logical addresses to physical

Swapping(if physical memory is too small for all processes)

Virtual memory(memory abstraction)

Sharing of Memory

Program 1

Free Space

Program 3

Program 2

Free Space

OS

Page 7: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

7Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Fetch strategy

• When AND how much to load at a time (e.g.: prefetching)

Placement (or allocation) strategy

• Determination WHERE data is to be placed

Replacement strategy

• Determination WHICH area can be removed

Variations• Fixed partitions

• Variable partitions

• Segmentation

• paging

Memory Strategies

Program 1

Free Space

Program 3

Program 2

Free Space

OS

Modern PCs

Early computers,

AND embedded, small, etc...

Page 8: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

8Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

MOV REG1, 1000

set of physical addresses from 0 to ...

one program only

– How to execute more than one program?

No Memory Abstraction (1)

Program

OS

Program

OS

Page 9: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

9Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Divide all physical memory into a fixed set ofcontiguous partitions

Place only one process at a time in any partition

Partition boundaries limit the available memory foreach process

Proces either entirely in main memory or not

No sharing between processes

Memory wasting:

– partition internal fragmentation

– Free partitions too small

Without virtual addressing – protection problems!

Without Swapping/virtual memory – size problems!

Assumption: Increased Size

- no Memory Abstraction

12K

2K

6K

OS: 2K

Page 10: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

10Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Swapping

Page 11: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

11Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Issue: memory (RAM) is too small

„swap“ data on slower but bigger memory

Swapper decides which processes should be in main memory

Memory Abstraction – Swapping

Free Space

OS

Page 12: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

12Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Swapping – Example

Free Space:180K

OS: 20K

Page 13: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

13Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Swapping – Example

Free Space:150K

OS: 20K

Process A:30K

Page 14: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

14Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Process B (50K) is created or swapped in

Swapping – Example

Free Space:100K

OS: 20K

Process A:30K

Process B:50K

Page 15: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

15Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Process B (50K) is created or swapped in

Process C (20K) is created or swapped in

Swapping – Example

Free Space:80K

OS: 20K

Process A:30K

Process B:50K

Process C:20K

Page 16: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

16Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Process B (50K) is created or swapped in

Process C (20K) is created or swapped in

Process D (20K) is created or swapped in

Swapping – Example

Free Space:60K

OS: 20K

Process A:30K

Process B:50K

Process C:20K

Process D:20K

Page 17: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

17Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Process B (50K) is created or swapped in

Process C (20K) is created or swapped in

Process D (20K) is created or swapped in

Process A is swapped out to disk

Swapping – Example

Free Space:60K

OS: 20K

Process B:50K

Process C:20K

Process D:20K

Free Space:30K

Page 18: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

18Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assumption: 180K of Memory, OS: 20K

Process A (30K) is created or swapped in

Process B (50K) is created or swapped in

Process C (20K) is created or swapped in

Process D (20K) is created or swapped in

Process A is swapped out to disk

Process C is swapped out to disk

What is the problem?

Swapping – Example

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

Page 19: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

19Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory Fragmentation

• Memory Compaction

Problem: CPU time

• Fixed size of partitions

Problem: growing memoryBigger partitions with room for grow

(here: heap)

• Variable size of partitions

Management with Bit-Mapsor Linked Lists

Trade-off: Space requirements vs. time for allocation

Swapping Issue

Free Space

OS

Free Space

Process C

Process D

Page 20: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

20Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory division into allocation units

Corresponding bit for each allocation unit (0,1)telling whether it is free or not

Example: Allocation units of 10KiB

• For current 20 bits: 00000011 00111110 0011

Trade-off: • Bit-Map size depends on allocation unit

• searching for k consecutive 0-bits in map costly,

• fixed size

Managing free space

with Bit-Maps

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

Page 21: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

21Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Linked list of allocated andfree memory segments

Each record has

– Process ID/ Free (H: hole)

– Start address

– length

– pointer to next record

Managing free space

with Linked Lists

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

2H 3 5B 5 10H 2

12D 2 14H X6

Page 22: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

22Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Update Process:

– When a process is swapped out, neighbouring blocks need to be examined

– doubly linked lists(containing a pointer to the next AND previousnode

Managing free space

with Linked Lists

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

2H 3 5P 5 10H 2

12P 2 14H X6

Page 23: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

23Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Assuming a new process E requests 15K

Which hole should it use?

Available algorithms

First fit

Next fit

Best fit

Worst fit

Algorithms to allocate memory

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

Page 24: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

24Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

simplest algorithm

scanning along the list (from beginning) until itfinds sufficient hole

Breaking hole in two pieces:

• One for process

• One for unused memory (new hole)

Memory Allocation: First Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

Page 25: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

25Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

simplest algorithm

scanning along the list (from beginning) until itfinds sufficient hole

Breaking hole in two pieces:

– One for process

– One for unused memory (new hole)

Fitting into 30K hole

Very fast – searches as little as possible

Memory Allocation: First Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free: 15K

Free Space:20K

Process E:15K

start

stop

Page 26: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

26Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Variation of First Fit

Keeps track of where it found a hole

Next time: start at position where it left off last time

Assumption that prospective fitting holes comeafter already found holes

However, slight worse performancethan first fit

Memory Allocation: Next Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space:20K

Page 27: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

27Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Variation of First Fit

Keeps track of where it found a hole

Next time: start at position where it left off last time

Assumption that prospective fitting holes comeafter already found holes

However, slight worse performancethan first fit

Fitting into 30K hole

Memory Allocation: Next Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free: 15K

Free Space:20K

Process E:15K

start

stop

Page 28: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

28Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Variation of First Fit

Keeps track of where it found a hole

Next time: start at position where it left off last time

Assumption that prospective fitting holes comeafter already found holes

However, slight worse performancethan first fit

Fitting into 30K hole

Then, fitting into 20K hole

Memory Allocation: Next Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free: 15K

Free: 5K

Process E:15K

start

stop

Process F:15K

Page 29: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

29Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Searches entire list (from beginning to end)

Takes smallest hole

Assumption of best memory spacial performance

Memory Allocation: Best Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space: 20K

Page 30: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

30Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Searches entire list (from beginning to end)

Takes smallest hole

Assumption of best memory spacial performance

Fitting into 20K hole

Takes much CPU time (for searching)

Resulting in more memory wasting because ofnumerous tiny useless holes.

Memory Allocation: Best Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free: 5K

Process E:15K

start

stop

Page 31: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

31Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Variation of best fit

Always takes the largest hole

Assumption to get around splitting into tinyholes, to be big enough for other processes

Memory Allocation: Worst Fit

Free Space:60K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space: 20K

Page 32: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

32Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Variation of best fit

Always takes the largest hole

Assumption to get around splitting into tinyholes, to be big enough for other processes

Fitting into 60K hole

Takes much CPU time (for searching)

Still memory wasting

Memory Allocation: Worst Fit

Free Space:45K

OS: 20K

Process B:50K

Process D:20K

Free Space:30K

Free Space: 20K

Process E:15K

start

stop

Page 33: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

33Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Maintaining separate lists for processes AND holes

• Algorithms devote energy to inspect holes NOT processes

• BUT higher effort for deallocating (changing both lists)

Sorting of lists regarding size

enhancing speed of Best Fit/Worst Fit

In practice:

FF is usually better

NF is pointless when sorted lists are used

Memory Allocation:

Enhancing performance

Page 34: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

34Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory encapsulated in units of powers of 2

Process requests rounded up to fit into units

Possible hole sizes: ..., 4K, 8K, 16K, 32K, 64K, 128K, ...

Buddy System

64K

32K

16K

8K4K4K

Page 35: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

35Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Buddy System

128K

Page 36: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

36Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

128K

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Buddy System

64K

32K

16K

8K

A: 8K

Page 37: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

37Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Process C requests 24K rounded to 32K(8K wasted)

Buddy System

64K

32K

16K

B: 8K

A: 8K

Page 38: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

38Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Process C requests 24K rounded to 32K(8K wasted)

Process D requests 3K rounded to 4K(1K wasted)

Buddy System

64K

C: 32K

16K

B: 8K

A: 8K

Page 39: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

39Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Process C requests 24K rounded to 32K(8K wasted)

Process D requests 3K rounded to 4K(1K wasted)

Process A exits

Buddy System

64K

C: 32K

8K

B: 8K

A: 8K

D: 4K4K

Page 40: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

40Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Process C requests 24K rounded to 32K(8K wasted)

Process D requests 3K rounded to 4K(1K wasted)

Process A exits

Process B exits

Buddy System

64K

C: 32K

8K

B: 8K

8K

D: 4K4K

Page 41: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

41Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Example: Suppose a memory of 128K One hole of 128K

Process A requests 6K rounded to 8K (2K wasted)

Process B requests 5K rounded to 8K(3K wasted)

Process C requests 24K rounded to 32K(8K wasted)

Process D requests 3K rounded to 4K(1K wasted)

Process A exits

Process B exits

Buddy System

64K

C: 32K

8K

16K

D: 4K4K

Page 42: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

42Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Virtual Memory

Page 43: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

43Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Issue: By using physical addresses all executions are absolutely adressedNOT relatively!

Better memory management with logicaladdresses

Independancy of actual physical location ofdata in physical memory

OS determines logical location of data

CPU instructions using virtual addresses(don‘t care about location)

Translated by hardware into physical address

Virtual Addressing

Program 1

Free Space

Program 3

Program 2

Free Space

OS

addressspace

addressspace

addressspace

Page 44: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

44Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Software size much bigger than available RAM

Each program‘s address space broken up into chunks

Pages

Page: contiguous range of addresses mapped onto physicalmemory

Not all pages have to be in physical memory

If OS notices, a program part is not in physical memory, it needs to be loaded and re-executed

In Multiprogramming: While program is waiting for pieces ofitself to be read in, CPU can execute another process

Virtual Memory

Page 45: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

45Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Page: Fixed size unit in virtual memory

Page frame: corresponding units in physical memory

Same size (here: 4KB)

Transparent to the program

Protection – a program cannot leave Virtual Address Space

Example: 64KB virtual address space, 32KB physical space, 4KB page size

• How many pages/page frames?

Answer: 16 virtual pages, 8 page frames

Paging

Page 46: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

46Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

4.096 - 8.191

Page 47: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

47Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

0 – 4.095

MOV REG, 0

MMU checks VM

page „0“ corresponds to page frame 1

Address is changed to „4096“

Page 48: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

48Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

8.192 – 12.287

MOV REG, 8192

MMU checks VM

page „8192“ corresponds to page frame 3

Address is changed to „12288“

Page 49: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

49Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

20.480 – 24.576

MOV REG, 20500

MMU checks VM

page „20500“ corresponds to page

frame 4 with an offset of 20 Byte

(20.480 + 20)

Address is changed to „16404“

(16.384 + 20)

Page 50: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

50Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory Management Unit (MMU)

p: page number

d: offset

r: page frame

Page 51: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

51Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Virtual address has two parts: virtual page number and offset

Virtual page number is an index into a page table

Page table determines page frame number

Present/Absent bit if page is in physical memory

What happens, if program references an unmapped address? (page fault)

Paging – translating addresses

Page 52: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

52Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

12.288 – 16.383

MOV REG, 12288

MMU checks VM

page fault!

page fault!

Page 53: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

53Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

6

2

X

4

5

X

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

12.288 – 16.383

MOV REG, 12288

MMU checks VM

page fault!

Choose a rarely used page frame (in

phys. mem)

Save content on hard drive

page fault!

e.g.

...001010100011..

Page 54: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

54Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

X

2

X

4

5

6

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

12.288 – 16.383

MOV REG, 12288

MMU checks VM

page fault

Choose a rarely used page frame (in

phys. mem)

Save content on hard drive

Load requested page into phys. Mem

page fault!

e.g.

...111010100100..

Page 55: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

55Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Relationship between

virtual and physical memory

X

X

X

7X

X

X

2

X

4

5

6

3

0

1

virtual

address

space

0KB-4KB

4KB-8KB

...

60KB-64KB

28KB-32KB

0KB-4KB

4KB-8KB

...

page

frame

Addresses No.

12.288 – 16.383

MOV REG, 12288

MMU checks VM

page fault

Choose a rarely used page frame (in

phys. mem)

Save content on hard drive

Load requested page into phys. Mem

Restart trapped

instruction

page fault!

e.g.

...111010100100..

Page 56: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

56Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Internal MMU operation

0 0 01 0 0 0 0 0 0 0 0 0 01 0

000

000

000111

000

101

000

000

000

011

100

000

110

001

010

0

0

0

10

1

0

0

0

1

1

1

1

1

10

1

2

3

4

5

6

7

8

9

10

11

12

13

14

000 015

110

1 1 0 0 0 0 0 0 0 0 0 0 01 0

4 Bit 16 pages

3 Bit page

frame

12 Bit Offset 12 Bit 4096 byte

4 Bit page

number

present bit

Page 57: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

57Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Strongly machine dependent

M/R – bit protocol access to the page

– M-bit is set when a program writes to a page

to know if it needs to be saved before physical space canbe reused

Structure of a page table entry

M R P Prot Page frame number

M – modified bit

R – reference bit

P – present/absent bit

Protection bits

Page 58: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

58Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Strongly machine dependent

M/R – bit protocol access to the page

– R-bit is set when read/write access occurs

for decision if space should be used for overwriting

Structure of a page table entry

M R P Prot Page frame number

M – modified bit

R – reference bit

P – present/absent bit

Protection bits

Page 59: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

59Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Strongly machine dependent

P – bit shows if page exists or not

Structure of a page table entry

M R P Prot Page frame number

M – modified bit

R – reference bit

P – present/absent bit

Protection bits

Page 60: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

60Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Strongly machine dependent

Prot – bits are setting rights which operations are allowed on a page

– E.g.: read, write, execute

Structure of a page table entry

M R P Prot Page frame number

M – modified bit

R – reference bit

P – present/absent bit

Protection bits

Page 61: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

61Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Easy to allocate memory

• Memory comes from a free list of fixed size chunks

• Allocating a page is just removing it from the list

• External fragmentation not a problem

Easy to swap out chunks of a program

• All chunks are the same size

• Use present bit to detect references to swapped pages

• Pages are a convenient multiple of the disk block size

Paging advantages

Page 62: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

62Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Memory reference overhead

• 2 references per address lookup (page table, then memory)

Solution – use a hardware cache of lookups

Memory requirements to hold page tables can be significant

• 32 bit address space á 4KB pages = 220

• 4 bytes/PTE = 4MB per page table

• 25 processes = 100MB just for page tables!

Solution – multilevel page tables

Paging disadvantages

Page 63: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

63Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Observation, programs have most access on just a few pages

Hardware Implementation – TLB

Part of MMU

Small list of most important pages of a process(not more than 64)

e.g. variables used in a loop

Virtual page number, M-bit, Protect-bit, page frame, Validity

Access:

• check TLB, if needed page can be found

• If NOT: fetch entry from page table, write entry to TLB (overwrite older entries)

Translation Lookaside Buffer (TLB)

Page 64: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

64Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

To be used for big virtual address spaces

Dividing a page table into mulitple tables

Multilevel Page Table

P=1

P=0

P=0

...

P=1

1

2

0

1023

...

...

...

• Two 4MB tables

vs. of 1024*1024 entries

Page 65: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

65Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Page fault! Load page into physical memory! Which page schould be replaced?

Answer: pages which are rarely used!

Possible Algorithms:

– Not Recently Used (NRU)

– Least Recently Used (LRU)

– First In First Out (FIFO)

– Second Chance (SC)

Page Replacement Algorithms

Page 66: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

66Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Using status bits – referenced (R) AND modified (M)

R-bit cleared periodically by OS

M-bit cleared when hard disk updated

Clearing random Entry of smallest availabe class:

Best entry class 0.

here: entry 1 or 3 is cleared!

Not Recently Used Algorithm (NRU)

M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001

Class 0: R=0, M=0Class 1: R=0, M=1Class 2: R=1, M=0Class 3: R=1, M=1

Page 67: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

67Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Oldest page entry (that loaded first)

Sorted list ordered by loading time

Clear oldest entry no matter if referenced or modified

here: entry 5

First In First Out Algorithm (FIFO)

M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001

Loaded12

25612411626145

Page 68: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

68Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Enhancement of FIFO (by using R-Bit)

Linked list ordered by loading time

Clear oldest entry if not referenced

If entry is referenced – clear R-bit and continue searching

here: entry 5 and 0 will be unreferenced AND loading time reset, then, entry 2 will be cleared and used

Second Chance Algorithm (SC)

M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001

Loaded12

25612411626145

Page 69: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

69Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Clears page that has been unused for the longest time

Linked list ordered by access time

based on asusmption, that pages used in the last few instructions will beused in the next few future instructions

Problem: Entries have to be updated every accesslinked-list must be reordered ;-(

here: entry 2 is cleared. (updated on hard drive first)

Least Recently Used Algorithm (LRU)

M R P Prot Page framePage Index Offset0 1 1 111 10015 100010101011 0 1 111 10114 000001000010 0 1 111 01113 000110000011 0 1 111 00012 011100010010 0 1 111 10101 000000010011 1 1 111 01010 00110000001

Loaded12

25612411626145

Access245256163116267245

Page 70: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

70Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Questions?

Page 71: Operating Systems and Computer Networks Memory Management · Partitioning, Paging, Segmentation Page replacement algorithms Memory Management. Dr.-Ing. Pascal A. Klein University

Dr.-Ing. Pascal A. KleinUniversity Duisburg-Essen

71Prof. Dr.-Ing. Axel HungerInstitute of Computer Engineering

OSCN – Memory Management

Tanenbaum, Andrew S., “Modern Operating Systems”, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2008.

Tanenbaum, Andrew S., “Moderne Bertriebssysteme”, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2009.

Lee, Insup, „CsE 380 Computer Operating Systems“, Lecture Notes, University of Pennsylvania, 2002.

Snoeren, Alex C., „Lecture 10: Memory Management“, LectureNotes, UC San Diego, 2010.

Resources