memory management - computer science and...

36
Memory Management 1 Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Sections 9.1–9.3, [OSC]

Upload: others

Post on 02-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Management

1

Instructor: Adam C. Champion, Ph.D.CSE 2431: Introduction to Operating SystemsReading: Sections 9.1–9.3, [OSC]

Page 2: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Outline

• Basic Memory Management• Swapping• Variable Partitions• Memory Management Problems

2

Page 3: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Basic Memory Management

• Storage Hierarchy • Memory Management Problems• Fixed Partitions for Multiprogramming • Variable Sized Partitions • Memory Allocation Strategies

3

Page 4: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Storage Hierarchy

4

Cost

$600 a chip

$10–2 per byte

$10–4 per byte

$10–8 per byte

Size

213 bytes

227 bytes

230 bytes

240 bytes

CPU Reg

Cache

Memory

Secondary Storage

32–64 bits

4–128 words

512–16K words

Page 5: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

General Memory Problem

• Limited (expensive) physical resources: Main memory– E.g. Windows 10 needs 1G, prefers 2G RAM–We want to use it as efficiently as possible

• Abundant, slower resources: Disk• OS needs to provide an abstraction of

memory hierarchy to user-level applications

5

Page 6: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Responsibilities of Memory Manager

• Manage memory hierarchy – Monitor used and free

memory– Allocate memory to

processes– Reclaim (De-allocate)

memory– Swapping between

main memory and disk

6

Page 7: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Scenarios: Memory Management Problems

• One program, the size is less than memory size– The simplest case

• One program, using lots of memory– Can you only keep part of the program in memory?

• Many programs, total size is less than memory size– Technically possible to pack them together– Will programs know about each other’s existence?

• Lots of programs, total size exceeds memory size– What programs are in memory, and how to decide?

7

Page 8: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Mono-Programming No Swapping• Run one process at a time

– simplest possible memory management scheme • Memory is shared only between OS and the process. • Three different ways to organize memory

8

0

0xFFF…

OS in RAM

User Program

OS in ROM

User Program

OS in RAM

User Program

Device drivers in ROM

Page 9: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Overlaying

9

Overlay Manager

Overlay Area

Main Program Overlay 1

Overlay 2

Overlay 3

Secondary Storage

0K

5k7k

12k

Used when the process memory requirement exceeds the physical memory space

Page 10: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Multiprogramming with Fixed Partitions

• Divide memory into n (possibly unequal) partitions.

• Problem:– Internal

Fragmentation

10

Free Space

0k

4k

16k

64k

128k

Page 11: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Fixed Partition Allocation • Separate input queue for each partition

– Requires sorting the incoming jobs and putting them into separate queues

– Problems?

• One single input queue for all partitions. – Find a job for fitting in an available partition

• Available Fit• Best Fit

– Problems?

11

Page 12: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Problems?

12

Partition 4

Partition 3

Partition 2

Partition 1

OS

Partition 4

Partition 3

Partition 2

Partition 1

OS0

100K

200K

400K

700K

800K

Page 13: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

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, Virtual 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 • Who translates virtual to physical addresses?

– Program rewriting at loading time– Help from relocation registers at execution time

13

Page 14: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Relocation Register

14

Memory

Base Register

CPU Instruction

Address+

BA

MA MA+BA

PhysicalAddress

LogicalAddress

Page 15: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Protection

• Problem:–How to prevent a malicious process to write

or jump into other user's or OS partitions• Solutions:–Memory protection code– Base bounds registers

15

Page 16: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Base Bounds Registers

16

Memory

Bounds Register Base Register

CPUAddress < +

MemoryAddress MA

LogicalAddress LA

Physical AddressPA

Fault

Base Address

Limit Address

MA+BA

BaseAddressBA

Page 17: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Review

• Basic Memory Management–Memory Manager’s Responsibilities–Mono-Programming–Multi-Programming with Fixed Partitions• Internal fragmentation

– Relocation and Protection

17

Page 18: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Management (More…)

• Batch System–Multiprogramming with fixed partitions– In the memory until job finishes– Keep CPU busy

• Timesharing Systems– Not enough memory to hold all active processes– Swapping (whole process)– Virtual memory (partial process)

18

Page 19: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping

• Move the whole process to/from disk• Allows several processes to share a fixed

partition • Processes that grow can be swapped out

and swapped back in a bigger partition

19

Page 20: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Outline

• Basic Memory Management• Swapping• Variable Partitions• Memory Management Problems

20

Page 21: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (1)

21

MonitorDisk

Job 1

UserPartition

Page 22: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (2)

22

Monitor

Job 1

Disk

Job 1

UserPartition

Page 23: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (3)

23

Monitor

Job 2

Job 1

Disk

Job 1

UserPartition

Page 24: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (4)

24

MonitorDisk

Job 2

Job 2

UserPartition

Job 1

Page 25: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (5)

25

MonitorDisk

Job 2

Job 2

UserPartition

Job 1

Page 26: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Swapping (6)

26

MonitorDisk

Job 1

Job 2

UserPartition

Job 1

Page 27: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Outline

• Basic Memory Management• Swapping• Variable Partitions• Memory Management Problems

27

Page 28: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Variable Partitions and Fragmentation

28

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

• Fixed Partitions ⟺Variable Partitions• External Fragmentation

– How to solve it?

Page 29: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Compaction• What is it? What does it looks like?• Assumes all programs are relocatable• Processes must be suspended during compaction • Need be done only when fragmentation gets very bad

29

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 85

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 86

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 87

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 88

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 89

Page 30: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Outline

• Basic Memory Management• Swapping• Variable Partitions• Memory Management Problems

30

Page 31: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Management with Bitmaps• Use bitmaps for memory status (free or allocated)• Memory is divided into allocation units.

– One allocation unit corresponds to 1bit in the bitmap– 0: free, 1: allocated

• Size of allocation unit– The smaller the allocation unit, the larger the bitmap.

• Problem: allocation– When a new process arrives, the manager must find

consecutive 0 bits in the map. – Searching a bitmap for a run of a given length is a slow

operation.

31

1 0 1 0 1 0 0 0 1 0 1 0 0 1 0

Page 32: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Management with Linked Lists

• Use a linked list of allocated and free memory segments (called hole)– Sorted by the address or by the size

32

Four neighbor combinations for the terminating process X

Page 33: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Allocation Strategies • Best Fit

– Uses 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. • First Fit

– Uses 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 stopped place.– Problem: slightly worse performance than first fit.

• Worst Fit– Uses the largest available hole.– Problem: Gets rid of large holes making it hard to run large programs.

• Quick Fit– Maintains separate lists of holes for some of the more common sizes

requested. When a request comes for placement it finds the closest fit. – A very fast scheme, but merge is expensive. If merge is not done,

memory will quickly fragment in a large number of holes into which no processes fit.

33

Page 34: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

How Bad Is Fragmentation?

• Statistical arguments - Random sizes• First-fit• Given N allocated blocks, 0.5*N blocks

will be lost because of fragmentation• Known as 50% RULE

34

Page 35: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Memory Management Problems• Fixed partitions – Suffer from internal fragmentation

• Variable partitions – Suffer from external fragmentation

• Compaction – Suffer from overhead

• Overlays – Painful for programmers

• Swapping – Requires writing to disk sectors

35

Page 36: Memory Management - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2431/10-MemoryManagement.pdf · 2020-03-29 · Memory Management 1 Instructor: Adam C. Champion, Ph.D

Summary

• Basic Memory Management• Swapping• Variable Partitions• Memory Management Problems

36