copyright ©: nahrstedt, angrave, abdelzaher, caccamo 1 memory management & paging

30
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Upload: magdalene-parker

Post on 08-Jan-2018

216 views

Category:

Documents


2 download

DESCRIPTION

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Four neighbor combinations for the terminating process X More on dynamic partitions: Bit Maps versus Linked Lists

TRANSCRIPT

Page 1: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1

Memory management & paging

Page 2: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

2

More on dynamic partitions: Bit Maps versus Linked Lists

Part of memory with 5 processes, 3 holes tick marks show allocation units shaded regions are free

Corresponding bit map & linked list

Page 3: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

3

Four neighbor combinations for the terminating process X

More on dynamic partitions: Bit Maps versus Linked Lists

Page 4: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

4

Bitmap and linked list Which one occupies more space?

Depends on the individual memory allocation scenario. In most cases, bitmap usually needs more space.

In terms of processing time, on average bitmap is faster because it just needs to update the corresponding bits

Which one is faster to find a free hole? On average, linked list is faster because we can link all free holes

together

More on dynamic partitions: Bit Maps versus Linked Lists

Page 5: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

5

Storage Placement Algorithms

Best fit Use smallest free space equal to or larger than the need Rationale?

First fit Use the first free space found that is large enough to meet the

need Rationale?

Worst fit Use the largest available free space Rationale?

Page 6: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

6

Each strategy has problems Best fit

Creates small free spaces that can’t be used

Worst Fit Removes large free spaces, so large programs may not fit

First Fit Creates average size free spaces

Storage Placement Algorithms

Page 7: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

7

Mentioned approaches can perform better or worse depending on exact sequence of process arrivals and size of those processes

First-fit is very simple and usually performs very well

Surprisingly best-fit is usually the worst performer: it guarantees that smallest amount of memory is always wasted but memory is quickly fragmented in pieces too small to satisfy further

requests

Storage Placement Algorithms

Page 8: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

8

How Bad Is Fragmentation?

Statistical argument: processes have random sizes

Assume to use first-fit and N blocks are allocated

0.5N blocks will be wasted because of internal fragmentation

Known as 50% RULE

Page 9: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

9

Dynamic partitions: solve fragmentation with compaction

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 81

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 82

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 83

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 84

Monitor Job 3 FreeJob 5 Job 6Job 7 Job 85

Page 10: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

10

Fixed vs dynamic partitions

Fixed partitions suffer from internal fragmentation

Dynamic partitions suffer from external fragmentation

Compaction suffers from overhead

Page 11: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

11

Question What if there are more processes than

what could fit into the memory?

Page 12: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

12

SwappingMonitor

Disk

UserPartition

Page 13: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

13

SwappingMonitor

Disk

User 1

UserPartition

Page 14: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

14

SwappingMonitor

User 1

Disk

User 1

UserPartition

Page 15: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

15

SwappingMonitor

User 2

User 1

Disk

User 1

UserPartition

Page 16: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

16

SwappingMonitor

Disk

User 2

User 2

UserPartition

User 1

Page 17: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

17

SwappingMonitor

Disk

User 2

User 2

UserPartition

User 1

Page 18: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

18

SwappingMonitor

Disk

User 1

User 2

UserPartition

User 1

Page 19: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

19

Both fixed and dynamic size partitioning are inefficient; in fact fixed partitioning suffers internal fragmentation while dynamic partitioning suffers external fragmentation

Suppose to partition physical main memory in small equal-size chunks (frames) Suppose each process memory is also divided into small fixed-size chunks (pages)

of the same size Pages of a process can be mapped to available frames of memory We would like to allocate non-contiguous frames to a process

can we do that? if this is the case, is the system suffering internal or external fragmentation?

Paging

Page 20: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

20

Suppose to partition physical main memory in small equal-size chunks (frames) Suppose each process memory is also divided into small fixed-size chunks (pages)

of the same size Pages of a process can be mapped to available frames of memory We would like to allocate non-contiguous frames to a process

can we do that? if this is the case, is the system suffering internal or external fragmentation?

We can implement such a scheme as follows: The programmer uses a contiguous logical address space (Virtual Memory divided in

pages) System uses a process page table to identify page<->frame mapping for each process Translation from logical to physical addressing is performed by CPU at run-time A logical address is composed of (page #, offset); the processor uses active process page

table to produce a physical address (frame #, offset)

Paging

Page 21: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

3 1234

Disk

Main Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main MemoryRequest Page 3

Paging

Page 22: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

3 11 2

34

Disk

Main Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main MemoryRequest Page 1

Paging

Page 23: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

3 116

234

Disk

Main Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main MemoryRequest Page 6

Paging

Page 24: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

3 116

234

Disk

Main Memory

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main MemoryRequest Page 2

2

Paging

Page 25: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

25

3 116

234

Disk

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main MemoryStore Virtual Memory Page1 to disk

2

Request Address within Virtual Memory 8

Paging

Page 26: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Copyright ©: Nahrstedt, Angrave, Abdelzaher

26

3 1

6234

Disk

Virtual Memory Stored on Disk

1 2 3 4 5 6 7 8

1 2 3 4

1

2

3

4

Page TableVM Frame

Main Memory

2

Load Virtual Memory Page 8 to main memory

88

Paging

Page 27: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Contents(P,D)

Contents(F,D)

P D

F D

P→F

0101101

Page Table

Physical Memory

Virtual Address (P,D)

Physical Address (F,D)

P

F

D

D

P

Virtual Memory

Page Mapping Hardware

Page 28: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Page Mapping Hardware

Contents(3006)

Contents(4006)

003 006

004 006

3→4

0101101

Page TableVirtual Memory

Physical Memory

Virtual Address (004006)

Physical Address (F,D)

003

004

006

006

3

Page size 1000Number of Possible Virtual Pages 1000

Page 29: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

Page Fault If CPU tries to access a virtual page that is not mapped

into any memory frame, a page fault is triggered.

Page fault handler (in OS’s VM subsystem) Find if there is any free memory frame available

If not, evict some resident page to disk (swapping space) Allocate a free memory frame Load the faulted virtual page to the prepared memory frame Update the page table

Page 30: Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging

More about Paging It is important to use a page size that is power of 2:

It is quite easy to implement a hw function that performs run-time address translation

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 (1M) pages with 4k (212) bytes per

page

Page table: Assuming each page table entry (PTE) requires 4bytes, 220 pages take 222

bytes (4 MB) The page table of active process must be resident in main memory Page Table base register must be changed for context switch

No external fragmentation; internal fragmentation on last page only