Upload
branden-nash
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
1
Memory Management Memory Management
COSC513 – Spring 2004COSC513 – Spring 2004
Student Name: Nan QiaoStudent Name: Nan Qiao
Student ID#: 104454Student ID#: 104454
Professor: Dr. Morteza AnvariProfessor: Dr. Morteza Anvari
2
Goals of Memory ManagementGoals of Memory Management
Relocation Protection Sharing Logical Organization Physical Organization
3
RelocationRelocation
Location of a program is not known until the program is executed Where the program is loaded depends on what
other programs are in the system A program may be swapped out and then back into
a different location Need to be able to execute the program
regardless of where it is loaded
4
ProtectionProtection
User processes cannot be allowed to read or write outside their boundaries
Protection must be done at runtime Programs can be loaded anywhere in memory Programs can dynamically allocate memory
Check for valid addresses during address translation Verify that address is within its bound
5
SharingSharing
Several processes may need access to the same data Shared operating system library (i.e. DLL) Shared data buffer
More efficient to allow shared access than to copy the data
Contrary to protection schemes Cannot just allow all processes access to all
memory
6
Logical StructureLogical Structure
A program is typically constructed based on different types of modules i.e. code, data, stack, heap Access to each type may be different (I.e.
read/write/shared/execute) Modules are written and compiled
separately References are resolved at runtime Typically implemented with segmentation
7
Physical StructurePhysical Structure
Need the ability to swap between main and secondary storage Secondary storage is large and non-volatile Main memory is fast
Solution: Divide memory into units that can be moved between disk and memory
Movement is coordinated by user or the OS OS knows current system resources OS can perform movement more efficiently
8
Memory PartitioningMemory Partitioning
Fixed partitioning Processes loaded into regions of fixed size
Dynamic partitioning Processes loaded into variable sized regions
Paging Fixed sized regions of small size
Segmentation Large dynamic regions
Virtual memory Uses pages and/or segments
9
Fixed PartitioningFixed Partitioning
Memory is divided into a number of fixed sized partitions Equal sized: all pages are the same Unequal sized: small and large pages
The entire process is loaded into contiguous memory
Advantage: Simple to implement Disadvantage: Can be wasteful
Page sizes are determined by the a-priori
10
Equal Sized PartitionsEqual Sized Partitions
Load processes into first available region Memory utilization can be low Large processes may not fit
8M
8M
8M
8M
8M
8M Process
10M Process
Too largeCannot be
loaded
4M Process
2M Process
Operating System
InternalFragmentation
11
Unequal Sized PartitionsUnequal Sized Partitions
Load process into: Smallest free region that fits Region closest to size (may require waiting)
Advantages Accommodates large and small processes May have better utilization than a fixed scheme
Disadvantages Memory utilization low if processes are small Limits the number of active processes
12
Unequaled Sized Partitions (example)Unequaled Sized Partitions (example)
Only processes larger than their partitions waste memory Less internal fragmentation
2M
4M
8M
16M
Operating System
10M Process
8M Process
4M Process
2M Process
InternalFragmentation
13
Dynamic PartitioningDynamic Partitioning
Partitions are of variable size Allocate only the amount of memory needed
Number of partitions only limited by the amount of free memory
Load entire process into contiguous memory Advantages:
No internal fragmentation Disadvantages:
Fragmentation still possible (external) More complex (requires placement scheme)
14
Dynamic Partitioning (example)Dynamic Partitioning (example)
Load processes contiguously in memory Holes are created when processes release
memory Operating System
32M
8M Process
24M
3M Process
21M
1M Process
20M
11M Process
9M
10M Process
Does notfit
ExternalFragmentation
7M Process
2M
32M
2M
1M
8M
15
Placement AlgorithmPlacement Algorithm
The OS must decide which hole to fill when allocating memory to a process Best fit: Find the smallest hole that satisfies the
allocation Worst fit: Use the biggest free region First fit: Use the first region in memory that satisfies
the allocation Next fit: like first fit, but search starting from the last
allocation
16
Internal FragmentationInternal Fragmentation
i.e. wasted memory within a partition Occurs for fixed sized partitions Occurs when a process does not use the
entire partition Prevent by dynamically partitioning
However this causes external fragmentation
17
External FragmentationExternal Fragmentation
i.e. wasted memory outside of the partition Occurs in variable sized partitions Occurs when holes are not large enough to
hold a new process Solution:
Compaction
18
CompactionCompaction
Eliminate holes by moving processes Copy operation is expensive
Operating System
32M
8M Process
24M
3M Process
21M
1M Process
20M
11M Process
9M7M Process
1M
32M
2M
1M
8M
Operating System
3M Process
11M Process
7M Process
11M
19
Disadvantages of PartitioningDisadvantages of Partitioning
Fragmentation is unavoidable Must load entire process into memory
Limits the number of active processes Solution: use paging and/or segmentation Why study partitioning
Historical reasons Paging and segmentation are based on it May still be used for dynamic (heap) allocation
20
PagingPaging
Memory is divided into small page frames
Pages of process are loaded into frames (need not be contiguous)
Less external fragmentation Small pages limit internal
fragmentation Page table translates page
addresses to frames
Main MemoryFrame
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
21
Paging (Example)Paging (Example)
Load pages into empty frames
Sufficient frames must exist for entire process
No compaction needed (pages can be loaded into non-contiguous frames)
Main MemoryFrame
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Process A (1)
Process A (2)
Process A (3)
Process A (4)
Process B (1)
Process B (2)
Process B (3)
Process B (4)
Process B (5)
Process B (6)
Process C (1)
Process C (2)
Process C (3)
Process C (4)
Process D (1)
Process D (2)
Process D (3)
Process D (4)
Process D (5)
Process D (6)
22
Page TablesPage Tables
One page table for each process
Page table translates logical addresses to physical addresses
Main MemoryFrame
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Process B (1)
Process B (2)
Process B (3)
Process B (4)
Process B (5)
Process B (6)
Process C (1)
Process C (2)
Process C (3)
Process C (4)
Process D (1)
Process D (2)
Process D (3)
Process D (4)
Process D (5)
Process D (6)
Page Frame0 41 52 63 74 85 9
Process B
Page Frame0 101 112 123 13
Process C
Page Frame0 01 12 23 34 145 15
Process D
23
Logical to Physical TranslationLogical to Physical Translation
Translation done in hardware Upper N bits translated to frame number Lower M bits contain offset into page
Pag
e 0
Pag
e 1
Pag
e 2
...
0 0 0 0 1 0 1 1 0 0
0 0 0 0 1 0 1 1 0 0
Page OffsetPage Number
Logical Address
0 0 1 0 1 0 1 1 0 0
0 0 1 0
0 0 1 1
Physical Address
Page Table
Frame Number
Frame Number
Page Offset
24
Advantages of Logical AddressingAdvantages of Logical Addressing
Logical addressing provides a means for process relocation
Each process uses the same logical addresses Separate page tables map logical addresses of
each process to different physical addresses Process segments start from a fixed location Makes compiling/loading easier
25
SegmentationSegmentation
Process memory is divided into segments One segment for each type of data
(data,code,stack,heap…) Each segment can be loaded anywhere
Segments are of variable size Address translation is required
Address contains segment number and offset Must ensure that process does not address beyond
the segment limit
26
Comparing Paging and SegmentationComparing Paging and Segmentation
Size of regions Small and fixed size for paging Large and variable size for segmentation
Address translation Simple bit shift for paging More complicated addition for segmentation
Advantages Paging: very little fragmentation Segmentation: logical structure
27
Combining Paging and SegmentationCombining Paging and Segmentation
Combine to gain advantages of both
A process’s segments consist of a number of fixed sized pages
Requires two address translations First logical segment to logical page address Next logical page to physical page address
28
How Paging/Segmentation Meet How Paging/Segmentation Meet RequirementsRequirements Relocation
Logical to physical translation allow segments or pages to be loaded anywhere
Protection Address translation gives hardware an opportunity
to check for valid addresses Sharing
Shared regions can be divided into segments Two logical segments can be mapped to one
physical shared segment
29
How Paging/Segmentation Meet How Paging/Segmentation Meet RequirementsRequirements Logical Organization
Program is divided into segments Physical Organization
Pages are convenient units to swap in and out of memory