39
CS 423: Operating Systems Design Professor Adam Bates Spring 2018 CS 423 Operating System Design: Virtual Memory Mgmt

CS 423 Operating System Design: Virtual Memory Mgmt

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Professor Adam BatesSpring 2018

CS 423 Operating System Design:

Virtual Memory Mgmt

Page 2: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 2

Goals for Today

Reminder: Please put away devices at the start of class

• Learning Objective: • Navigate the history of memory systems in OS

• Announcements, etc: • MP1 Due Tonight! • MP2 Out later This Week

Page 3: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Storage Hierarchy

3

CPU Registers

Cache

Memory

Secondary Storage

Performance Size

32-64 bits

4-128 words

512-16k words

Page 4: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Problem Statement

4

We have limited amounts of fast resources, and large amounts of slower resources…

How to create the illusion of an abundant fast resource?

Page 5: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Overlay Area

History: Mem Overlays

5

Overlay Manager

Main ProgramOverlay 1

Overlay 2

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Page 6: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Overlay 1

History: Mem Overlays

6

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

Page 7: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Mem Overlays

7

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay Area

Overlay 2

Page 8: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Overlay 2

History: Mem Overlays

8

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

Page 9: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Mem Overlays

9

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay Area

Overlay 2

Page 10: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Mem Overlays

10

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay 3

Overlay 2

Page 11: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Mem Overlays

11

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay Area

Overlay 2

Page 12: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Overlay 1

History: Mem Overlays

12

Overlay Manager

Main ProgramOverlay 1

Overlay 3

Secondary Storage0K

5k

7k

12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

Page 13: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 13

0k

4k

16k

64k

128k

• Approach: Multiprogramming with fixed memory partitions

• Divides memory into n fixed partitions (possible unequal)

• Problem?

Free Space

History: Fixed Partitions

Page 14: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 14

0k

4k

16k

64k

128k

Program 1

Program 2

Program 3 Free Space

History: Fixed Partitions

• Approach: Multiprogramming with fixed memory partitions

• Divides memory into n fixed partitions (possible unequal)

• Problems?

Page 15: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 15

Free Space

0k

4k

16k

64k

128k

• Approach: Multiprogramming with fixed memory partitions

• Divides memory into n fixed partitions (possible unequal)

• Problems? • Internal Fragmentation! Also, • Level of Multiprogramming

Program 1

Program 2

Program 3

History: Fixed Partitions

Page 16: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 16

History: Fixed Partition Allocation

Placement Algorithms for Fixed Partitions ■ Trivial for equal size partitions. For unequal… ■ Multiple Queues:

■ Assign (i.e., enqueue) each incoming job to the smallest partition within which it fits

■ Decreases fragmentation ■ when the queue for a large partition is empty but the queue for a small

partition is full. Small jobs have to wait to get into memory even though plenty of memory is free.

■ Single Queue: ■ Assign each process to the smallest available partition within which it

fits ■ Increases amount of multiprogramming on the expense of fragmentation

Page 17: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 17

History: Relocation

■ Correct starting address when a program should start in the memory

■ Different jobs will run at different addresses ■ When a program is linked, the linker must know at what address the

program will begin in memory. ■ Logical addresses

■ Logical address space , range (0 to max) ■ Physical addresses, Physical address space range (R+0 to R+max) for

base value R. ■ User program never sees the real physical addresses

■ Relocation register ■ Mapping requires hardware with the base register

Page 18: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 18

History: Relocation Register

Memory

Base Register

CPU Instruction

Address+

BA

MA MA+BA

Physical Address

Logical Address

Relocation => “Variable Partition Allocation”

Page 19: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 19

History: Variable Partition Allocation

Monitor Job 1 Job 2 Job 3 Job 4 Free1

Monitor Job 1 Job 3 Job 4 Free2

Monitor Job 1 Job 3 Job 4 FreeJob 53

Monitor Job 3 Job 4 FreeJob 5 Job 64

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 85

Memory wasted by External Fragmentation

Page 20: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Bad Parking Analogy

20

Source: https://xkcd.com/562/

Page 21: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 21

History: Storage Placement Strategy■ Best Fit

■ Use the hole whose size is equal to the need, or if none is equal, the hole that is larger but closest in size.

■ Problem: Creates small holes that can't be used. ■ Worst Fit?

■ Use the largest available hole. ■ Problem: Gets rid of large holes making it difficult to run

large programs. ■ First Fit

■ Use the first available hole whose size is sufficient to meet the need.

■ Problem: Creates average size holes. ■ Next Fit.

■ Minor variation of first fit: search from the last hole used. ■ Problem: slightly worse performance than first fit.

Page 22: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Example Policies

22

• Allocate 12K block • Red is allocated • Green is free

Last allocation

8K

12K

26K

8K

20K

8K

20K

Page 23: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Example Policies

23

Best Fit

• Allocate 12K block • Red is allocated • Green is free

Last allocation

8K

12K

26K

8K

20K

8K

20K

Page 24: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Example Policies

24

Best Fit

• Allocate 12K block • Red is allocated • Green is free

Last allocation

8K

12K

26K

8K

20K

8K

20K

Worst Fit

Page 25: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Example Policies

25

Best Fit

• Allocate 12K block • Red is allocated • Green is free

Last allocation

8K

12K

26K

8K

20K

8K

20K

Worst FitFirst Fit

Page 26: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Example Policies

26

Best Fit

• Allocate 12K block • Red is allocated • Green is free

Last allocation

8K

12K

26K

8K

20K

8K

20K

Worst FitFirst Fit

Next Fit

Page 27: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

History: Summary

27

Overlay Fixed Partitions Relocation

• No multi-programming support

• Supports multi-programming

• Internal fragmentation

• No internal fragmentation

• Introduces external fragmentation

Page 28: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Virtual Memory

28

■ Provide user with virtual memory that is as big as user needs

■ Store virtual memory on disk ■ Cache parts of virtual memory being used in real

memory ■ Load and store cached virtual memory without user

program intervention

Page 29: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Paging

29

1234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

Page 30: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Paging

30

3 1234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

Request Page 3…

Page 31: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 31

Paging

3 11 2

34

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

Request Page 1…

Page 32: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 32

Paging

3 116

234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

Request Page 6…

Page 33: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 33

Paging

3 116

234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

2

Request Page 2…

Page 34: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 34

PagingRequest Page 8. Swap Page 1 to Disk First…

3 116

234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

2

Page 35: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design 35

PagingRequest Page 8. … now load Page 8 into Memory.

3 1

6234

Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

Page TableVM Frame

2

8

Page 36: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Page Mapping Hardware

36

Contents(P,D)

Contents(F,D)

P D

F D

P→F

0101101

Page TableVirtual MemoryVirtual Address (P,D)

Physical Address (F,D)

P

F

D

D

4

Physical Memory

Page 37: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Page Mapping Hardware

37

Contents(4006)

Contents(5006)

004 006

005 006

4→5

0101101

Page TableVirtual Memory

Physical Memory

Virtual Address (004006)

Physical Address (F,D)

004

005

006

006

4

Page size 1000 Number of Possible Virtual Pages 1000 Number of Page Frames 8

Page 38: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Page Faults

38

■ Access a virtual page that is not mapped into any physical page ■ A fault is triggered by hardware

■ Page fault handler (in OS’s VM subsystem) ■ Find if there is any free physical page available

■ If no, evict some resident page to disk (swapping space) ■ Allocate a free physical page ■ Load the faulted virtual page to the prepared physical

page ■ Modify the page table

Page 39: CS 423 Operating System Design: Virtual Memory Mgmt

CS 423: Operating Systems Design

Paging Issues

39

■ Page size is 2n

■ usually 512 bytes, 1 KB, 2 KB, 4 KB, or 8 KB ■ E.g. 32 bit VM address may have 220 (1 MB) pages with

4k (212) bytes per page ■ Page table:

■ 220 page entries take 222 bytes (4 MB) ■ Must map into real memory ■ Page Table base register must be changed for context

switch ■ No external fragmentation; internal fragmentation on

last page only