Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging.
-
Upload
magdalene-parker -
Category
Documents
-
view
216 -
download
2
description
Transcript of Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging.
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher
3
Four neighbor combinations for the terminating process X
More on dynamic partitions: Bit Maps versus Linked Lists
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
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?
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
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
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
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
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher
11
Question What if there are more processes than
what could fit into the memory?
Copyright ©: Nahrstedt, Angrave, Abdelzaher
12
SwappingMonitor
Disk
UserPartition
Copyright ©: Nahrstedt, Angrave, Abdelzaher
13
SwappingMonitor
Disk
User 1
UserPartition
Copyright ©: Nahrstedt, Angrave, Abdelzaher
14
SwappingMonitor
User 1
Disk
User 1
UserPartition
Copyright ©: Nahrstedt, Angrave, Abdelzaher
15
SwappingMonitor
User 2
User 1
Disk
User 1
UserPartition
Copyright ©: Nahrstedt, Angrave, Abdelzaher
16
SwappingMonitor
Disk
User 2
User 2
UserPartition
User 1
Copyright ©: Nahrstedt, Angrave, Abdelzaher
17
SwappingMonitor
Disk
User 2
User 2
UserPartition
User 1
Copyright ©: Nahrstedt, Angrave, Abdelzaher
18
SwappingMonitor
Disk
User 1
User 2
UserPartition
User 1
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
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
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
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
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
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
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
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
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 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 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
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