CS 423 Operating System Design: Virtual Memory Mgmt Professor - - PowerPoint PPT Presentation

cs 423 operating system design virtual memory mgmt
SMART_READER_LITE
LIVE PREVIEW

CS 423 Operating System Design: Virtual Memory Mgmt Professor - - PowerPoint PPT Presentation

CS 423 Operating System Design: Virtual Memory Mgmt Professor Adam Bates Spring 2018 CS 423: Operating Systems Design Goals for Today Learning Objective: Navigate the history of memory systems in OS Announcements, etc: MP1


slide-1
SLIDE 1

CS 423: Operating Systems Design

Professor Adam Bates Spring 2018

CS 423
 Operating System Design: Virtual Memory Mgmt

slide-2
SLIDE 2

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
slide-3
SLIDE 3

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

slide-4
SLIDE 4

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?

slide-5
SLIDE 5

CS 423: Operating Systems Design

Overlay Area

History: Mem Overlays

5

Overlay Manager Main Program Overlay 1 Overlay 2 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

slide-6
SLIDE 6

CS 423: Operating Systems Design

Overlay 1

History: Mem Overlays

6

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

slide-7
SLIDE 7

CS 423: Operating Systems Design

History: Mem Overlays

7

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay Area Overlay 2

slide-8
SLIDE 8

CS 423: Operating Systems Design

Overlay 2

History: Mem Overlays

8

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

slide-9
SLIDE 9

CS 423: Operating Systems Design

History: Mem Overlays

9

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay Area Overlay 2

slide-10
SLIDE 10

CS 423: Operating Systems Design

History: Mem Overlays

10

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay 3 Overlay 2

slide-11
SLIDE 11

CS 423: Operating Systems Design

History: Mem Overlays

11

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay Area Overlay 2

slide-12
SLIDE 12

CS 423: Operating Systems Design

Overlay 1

History: Mem Overlays

12

Overlay Manager Main Program Overlay 1 Overlay 3 Secondary Storage

0K 5k 7k 12k

Used when process memory requirement exceeded the physical memory space

Overlay 2

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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?
slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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”

slide-19
SLIDE 19

CS 423: Operating Systems Design 19

History: Variable Partition Allocation

Monitor Job 1 Job 2 Job 3 Job 4 Free 1 Monitor Job 1 Job 3 Job 4 Free 2 Monitor Job 1 Job 3 Job 4 Free Job 5 3 Monitor Job 3 Job 4 Free Job 5 Job 6 4 Monitor Job 3 Free Job 5 Job 6 Job 7 Job 8 5 Memory wasted by External Fragmentation

slide-20
SLIDE 20

CS 423: Operating Systems Design

Bad Parking Analogy

20

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

slide-21
SLIDE 21

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.

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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 Fit First Fit

slide-26
SLIDE 26

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 Fit First Fit Next Fit

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

CS 423: Operating Systems Design

Paging

29

1 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame

slide-30
SLIDE 30

CS 423: Operating Systems Design

Paging

30

3 1 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame

Request Page 3…

slide-31
SLIDE 31

CS 423: Operating Systems Design 31

Paging

3 1 1 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame

Request Page 1…

slide-32
SLIDE 32

CS 423: Operating Systems Design 32

Paging

3 1 1 6 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame

Request Page 6…

slide-33
SLIDE 33

CS 423: Operating Systems Design 33

Paging

3 1 1 6 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame 2

Request Page 2…

slide-34
SLIDE 34

CS 423: Operating Systems Design 34

Paging

Request Page 8. Swap Page 1 to Disk First…

3 1 1 6 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame 2

slide-35
SLIDE 35

CS 423: Operating Systems Design 35

Paging

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

3 1 6 2 3 4 Memory Virtual Memory Stored on Disk 1 2 3 4 5 6 7 8 1 2 3 4 Page Table VM Frame 2 8

slide-36
SLIDE 36

CS 423: Operating Systems Design

Page Mapping Hardware

36

Contents(P,D) Contents(F,D) P D F D P→F 1 1 1 1 Page Table Virtual Memory Virtual Address (P,D) Physical Address (F,D) P F D D 4 Physical Memory

slide-37
SLIDE 37

CS 423: Operating Systems Design

Page Mapping Hardware

37

Contents(4006) Contents(5006) 004 006 005 006 4→5 1 1 1 1 Page Table Virtual 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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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