40
Copyright ©: University of Illinois CS 241 Staff 1 An Introduction to Memory Management: appendix

An Introduction to Memory Management: appendix

Embed Size (px)

Citation preview

Page 1: An Introduction to Memory Management: appendix

Copyright ©: University of Illinois CS 241 Staff 1

An Introduction to Memory Management: appendix

Page 2: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 2

Memory partitioning

n  Nowadays memory management is based on a sophisticated technique known as (paged) virtual memory.

n  Before studying virtual memory, we will review simpler but outdated memory management mechanisms for multiprogramming systems: ¡  Fixed partitioning ¡  Dynamic partitioning

Page 3: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 3

Fixed partitioning

n  A simple scheme to manage the available user memory is to partition it in several regions of equal size (e.g., 8Mb partitions)

n  As another option the memory can be partitioned in fixed regions of different sizes (e.g., 2Mb,4Mb,6Mb,8Mb partitions)

n  Problems: ¡  If a program does not fit in the available fixed size, the program must be

designed by using overlays (only a part of the program needs to be in main memory at any given time)

¡  Internal fragmentation ¡  Not all processes may fit in memory

Page 4: An Introduction to Memory Management: appendix

Multiple Fixed Partitions

Copyright ©: University of Illinois CS 241 Staff 4

Free Space

0k

4k

16k

64k

128k

Fixed boundaries between memory allocations

In use Divide memory into n (possibly unequal) partitions.

Page 5: An Introduction to Memory Management: appendix

Multiple Fixed Partitions

Copyright ©: University of Illinois CS 241 Staff 5

Free Space

Second memory allocation

First memory allocation

0k

4k

16k

64k

128k

Internal “fragmentation” (cannot be reallocated)

In use

Third memory allocation

Fourth memory allocation ?

Page 6: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6

Memory assignment for fixed partitioning

OS OS

New processes

New processes

2Mb

2Mb

4Mb

8Mb

8Mb

2Mb

2Mb

4Mb

8Mb

8Mb

A process is always reloaded in the same partition (relocation is not needed)

A process can be reloaded in any partition (relocation is needed)

Page 7: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 7

n  Separate input queue for each partition ¡  Requires sorting the incoming jobs and putting them into separate

queues ¡  Inefficient utilization of memory

n  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 memory has plenty of free space.

n  One single input queue for all partitions. ¡  Allocate a partition where the job fits in.

n  Best Fit n  Worst Fit n  First Fit

Memory assignment for fixed partitioning

Page 8: An Introduction to Memory Management: appendix

Problem: Insufficient Memory

n  What if there are more processes than could fit into the memory?

n  Swapping

Copyright ©: University of Illinois CS 241 Staff 8

Page 9: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 9

Monitor Disk

User Partition

Page 10: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 10

Monitor Disk

User 1

User Partition

Page 11: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 11

Monitor

User 1

Disk

User 1

User Partition

Page 12: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 12

Monitor

User 2

User 1

Disk

User 1

User Partition

Page 13: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 13

Monitor Disk

User 2

User 2

User Partition

User 1

Page 14: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 14

Monitor Disk

User 2

User 2

User Partition

User 1

Page 15: An Introduction to Memory Management: appendix

Swapping

Copyright ©: University of Illinois CS 241 Staff 15

Monitor Disk

User 1

User 2

User Partition

User 1

Page 16: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 16

Dynamic partitions

n  Partitions are of variable length and number

n  When a process is loaded, it is allocated exactly as much memory as it needs and no more

n  Problems: ¡  External fragmentation ¡  It requires periodic compaction and

task relocation

Free Space

Task A

Task B

Task C

Task D

Page 17: An Introduction to Memory Management: appendix

Storage Placement Strategies

n  First fit ¡  Use the first available hole whose size is sufficient to meet

the need

n  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

n  Worst fit ¡  Use the largest available hole

Copyright ©: University of Illinois CS 241 Staff 17

Page 18: An Introduction to Memory Management: appendix

Example

n  Consider a system in which memory consists of the following hole sizes in memory order: ¡  10K, 4K, 20K, 18K, 7K, 9K, 12K, and 15K. ¡  Which hole is taken for successive segment

requests of: n  12K n  10K n  9K

Copyright ©: University of Illinois CS 241 Staff 18

Page 19: An Introduction to Memory Management: appendix

Example

n  Consider a system in which memory consists of the following hole sizes in memory order: ¡  10K, 4K, 20K, 18K, 7K, 9K, 12K, and 15K. ¡  Which hole is taken for successive segment

requests of: n  12K n  10K n  9K

Copyright ©: University of Illinois CS 241 Staff 19

First fit: 20K, 10K, 18K.

Best fit: 12K, 10K, 9K.

Worst fit: 20K, 18K, and 15K.

Page 20: An Introduction to Memory Management: appendix

Storage Placement Strategies

n  Best fit ¡  Produces the smallest leftover hole ¡  Creates small holes that cannot be used

n  Worst Fit ¡  Produces the largest leftover hole ¡  Difficult to run large programs

n  First Fit ¡  Creates average size holes

n  First-fit and best-fit better than worst-fit in terms of speed and storage utilization

Copyright ©: University of Illinois CS 241 Staff 20

Page 21: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21

Fragmentation

n  Internal Fragmentation n  When an allocated block is larger than data it holds

n  External Fragmentation n  When aggregate free space would be large enough to

satisfy request but no single free block is large enough

Monitor Job 3 Free Job 5 Job 6 Job 7 Job 8

Legend Free Space

Page 22: An Introduction to Memory Management: appendix

Fragmentation

n  Internal Fragmentation ¡  Allocated memory may be larger than

requested memory ¡  The extra memory is internal to a partition

and it cannot be used n  External Fragmentation

¡  Memory space exists to satisfy a request, but it is not contiguous

Copyright ©: University of Illinois CS 241 Staff 22

Page 23: An Introduction to Memory Management: appendix

How Bad Is Fragmentation?

n  Statistical analysis - Random job sizes n  First-fit

¡  Given N allocated blocks ¡  0.5*N blocks will be wasted on average,

because of internal fragmentation

n  Known as 50% RULE Copyright ©: University of Illinois CS 241 Staff 23

Page 24: An Introduction to Memory Management: appendix

Compaction

n  Reduce external fragmentation by compaction ¡  Move jobs in memory to place all free

memory together in one large block ¡  Compaction is possible only if run-time

process relocation is supported

Copyright ©: University of Illinois CS 241 Staff 24

Page 25: An Introduction to Memory Management: appendix

Compaction

Copyright ©: University of Illinois CS 241 Staff 25

Monitor Job 3 Free Job 5 Job 6 Job 7 Job 8 5

Monitor Job 3 Job 5 Job 6 Job 7 Job 8 6

Monitor Job 3 Job 5 Job 6 Job 7 Job 8 7

Monitor Job 3 Job 5 Job 6 Job 7 Job 8 8

Monitor Job 3 Free Job 5 Job 6 Job 7 Job 8 9

Page 26: An Introduction to Memory Management: appendix

Storage Management Problems

n  Fixed partitions suffer from

n  Dynamic partitions suffer from

n  Compaction suffers from

Copyright ©: University of Illinois CS 241 Staff 26

Page 27: An Introduction to Memory Management: appendix

Storage Management Problems

n  Fixed partitions suffer from ¡  Internal fragmentation

n  Dynamic partitions suffer from ¡  External fragmentation

n  Compaction suffers from ¡  Overhead

Copyright ©: University of Illinois CS 241 Staff 27

Page 28: An Introduction to Memory Management: appendix

Copyright ©: Nahrstedt, Angrave, Abdelzaher 28 28

Relocation

n  Assume relocation is not supported: when the process is first loaded, all memory references in the code are replaced by absolute main memory addresses ¡  Different processes will be loaded at different absolute addresses or swapping

is necessary ¡  It is a strong limitation since a swapped process must always be reloaded in the

same partition

n  Relocation allows a process to occupy different partitions during its lifetime. It relies on notion of logical and physical addresses (it requires hardware support) ¡  Logical addresses: range from 0 to max ¡  Physical addresses: range from R+0 to R+max (given base value R). ¡  User program never sees the real physical addresses

Page 29: An Introduction to Memory Management: appendix

Relocation: Logical vs. Physical Addresses

n  Logical address ¡  An address meaningful to the user process ¡  A translation must be made to a physical address

before the memory access can be achieved n  Physical address

¡  It is an actual location in main (physical) memory

n  Different processes run at different physical addresses ¡  But logical address can be the same

¡  Program never sees physical addresses Copyright ©: University of Illinois CS 241 Staff 29

Page 30: An Introduction to Memory Management: appendix

Relocation: Dynamic Address Translation

n  Load each process into contiguous regions of physical memory

n  Logical addresses ¡  Logical address space ¡  Range: 0 to MAX

n  Physical addresses ¡  Physical address space ¡  Range: R+0 to R+MAX

for base value R

Copyright ©: University of Illinois CS 241 Staff 30

Page 31: An Introduction to Memory Management: appendix

Dynamic Address Translation

n  Translation enforces protection ¡  One process can’t even refer to another process’s

physical address space

n  Translation enables relocation and protection

Copyright ©: University of Illinois CS 241 Staff 31

User process

Translator (MMU)

Physical memory

Logical addr

Phys addr

Page 32: An Introduction to Memory Management: appendix

MMU

Base Register

Copyright ©: University of Illinois CS 241 Staff 32

Memory

Base Register

CPU Instruction Address

+

BA

MA MA+BA

Physical Address

Logical Address

Base Address

Base: start of the process’s physical memory partition

Page 33: An Introduction to Memory Management: appendix

MMU

Base Register

Copyright ©: University of Illinois CS 241 Staff 33

Memory

Base Register

CPU Instruction Address

+

14000

346 14346

Physical Address

Logical Address

Base Address

Base: start of the process’s memory partition

Page 34: An Introduction to Memory Management: appendix

Protection

n  Problem ¡  How to prevent a malicious process from

writing or jumping into another user's or OS physical partitions

n  Solution ¡  Base bounds register

Copyright ©: University of Illinois CS 241 Staff 34

Page 35: An Introduction to Memory Management: appendix

Base and bounds if (logical_addr > bound) trap to kernel } else {

phys_addr = logical_addr + base

}

n  Process can be relocated at run-time

n  Provides protection from other processes also currently in memory

Copyright ©: University of Illinois CS 241 Staff 35

physical memory

physical memory size

base + bound

base bound

logical addresses

0 0

Page 36: An Introduction to Memory Management: appendix

Base and Bounds Registers

Copyright ©: University of Illinois CS 241 Staff 36

Memory

Bounds Register Base Register

CPU Address < +

Memory Address

MA

Logical Address LA

Physical Address

PA

Fault

Base Address

Limit Address

MA+BA

Base Address

BA

Base: start of the process’s phys. memory partition Limit: max address in the process’s phys. memory partition

Page 37: An Introduction to Memory Management: appendix

Another memory management technique: Segmentation

n  Segment ¡  Region of contiguous memory

n  Segmentation ¡  Generalized base and bounds with

support for multiple segments at once

Copyright ©: University of Illinois CS 241 Staff 37

Page 38: An Introduction to Memory Management: appendix

Segmentation

Copyright ©: University of Illinois CS 241 Staff 38

code

stack

data code

stack

data

physical memory

virtual memory segment 3

Virtual memory segment 1

Virtual memory segment 0

0

0

0

0

fff

4ff

6ff 4ff

2000

2fff

4000

46ff

Seg # Base Bound Description

0 4000 700 Code segment

1 0 500 Data segment

2 Unused

3 2000 1000 Stack segment

Page 39: An Introduction to Memory Management: appendix

Segmentation

n  Segments: advantages over base and bounds?

n  Protection ¡  Different segments can

have different protections n  Flexibility

¡  It can separately grow both a stack and heap

¡  Enables sharing of code and other segments if needed

Copyright ©: University of Illinois CS 241 Staff 39

code

stack

data code

stack

data

physical memory

virtual memory segment 3

Virtual memory segment 1

Virtual memory segment 0

0

0

0

0

fff

4ff

6ff 4ff

2000

2fff

4000

46ff

Page 40: An Introduction to Memory Management: appendix

Segmentation

n  What abstraction is not supported well by segmentation and by B&B? ¡  Supporting an address space larger than the size of

physical memory

n  Note: x86 used to support segmentation, now effectively deprecated with x86-64

Copyright ©: University of Illinois CS 241 Staff 40