41
Memory Management

Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Embed Size (px)

Citation preview

Page 1: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Management

Page 2: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory ManagementProcess must be loaded into memory before

being executed.Memory needs to be allocated to ensure a

reasonable supply of ready processes to consume available processor time

Page 3: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Management RequirementsMemory Management Techniques determine:

How the memory is to be (logically) subdivided? Where and how a process resides in memory? How addressing is performed? How process can be relocated? How memory is to be protected? How memory can be shared by processes? How to logical and physically organize memory

Requirements Minimize executable memory access time Maximize executable memory size Executable memory must be cost-effective

Page 4: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Requirements of a Process

Page 5: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Mono-programming

Page 6: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Multi-Programming

Page 7: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Creating an Executable Program

Page 8: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

AddressesThe symbolic addresses are the addresses used

in a source program. The variable names, symbolic constants and instruction labels are the basic elements of the symbolic address space.

The compiler converts a symbolic addresses into a relative/fixed address.

The physical address consists of the final address generated when the program is loaded and ready to execute in physical memory; the loader generates these addresses.

Page 9: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Binding of Instructions and Data to MemoryAddress binding of instructions and data to memory

addresses can happen at three different stages.Compile time: If memory location known a priori,

absolute code can be generated; must recompile code if starting location changes.

Load time: The compiler must generate relocatable code if memory location is not known at compile time. The final binding is delayed until load time.

Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers).

Page 10: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Absolute Object Module Code Segment Relative Address Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 call 2334 ... 1399 (End of proc_a) ... (Other modules) 2334 entry put_record ... 2670 (optional symbol table) ... 2999 (last location in the code segment) Data Segment Relative Address Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment)

Relocatable Object Module Code Segment Relative Address Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 call ‘put_record’ ... 0400 External reference table ... 0404 ‘put_record’ 0232 ... 0500 External definition table ... 0540 ‘proc_a’ 0008 ... 0600 (symbol table) ... 0799 (last location in the code segment) Data Segment Relative Address Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment)

Addresses are FIXED at compile/assemble time.

You need an absolute loader to load this program

A relocatable loader is required

Addresses are fixed by the loader

Page 11: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Logical vs. Physical Address SpaceThe concept of a logical address space that is

bound to a separate physical address space is central to proper memory management.Logical address – generated by the CPU; also

referred to as virtual address.Physical address – address seen by the memory

unit. Logical and physical addresses are the same in

compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme.

Page 12: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Management Unit - (MMU)MMU is a hardware

device that maps virtual to physical address at run-time.

 The user program deals with logical addresses; it never sees the real physical addresses.

Page 13: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Management Techniques

MMT

Contiguous Non-contiguous

Single Partition Multiple Partition

Fixed

Paging Segmentation Combined

Dynamic

Virtual

Page 14: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Single-Partition AllocationA single processes in loaded into the memory

at a time.Commercially available in 1940s and 1950sEntire program loaded into memory

Page 15: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Single-Partition AllocationAdvantages:

SimplicityNo special hardware required

Disadvantages:CPU wastedMain memory not fully usedLimited job sizeNo support for multiprogramming

Page 16: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Multiple-Partition Allocation – Fixed Partitions

Page 17: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Multiple-Partition Allocation – Fixed Partitions

Page 18: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Multiple-Partition Allocation – Fixed PartitionsDisadvantages

Requires contiguous loading of entire program Job allocation method

First available partition with required sizeTo work well:

All jobs must be same size and memory size known ahead of time

Arbitrary partition size leads to undesired resultsPartition too small - Large jobs have longer

turnaround timePartition too large - Memory waste

Page 19: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Multiple-Partition Allocation – Dynamic Partitions

Page 20: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Merging of holes

Page 21: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Pros and Cons of Dynamic AllocationAdvantages

Efficient use of memory

DisadvantagesPartition managementCompaction or external fragmentationInternal fragmentation

Page 22: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Dynamic Allocation Placement AlgorithmsFirst-fit: Allocate the first hole that is big

enough. The search can start either at the beginning of the set of holes or where the previous first-fit search was ended.

If entire list searched in vain

Then job is placed into waiting queue

Else Memory Manager fetches next job

Page 23: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Dynamic Allocation Placement AlgorithmsBest-fit: Allocate the smallest hole that is

big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole.

Entire table searched before allocation

Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.

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

Page 24: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

FragmentationExternal Fragmentation – total memory

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

Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used.

Page 25: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

External fragmentation, Can I run 7KBH process?

Page 26: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Solutions to External FragmentationCompactionContiguous Allocation – Paging,

Segmentation

Page 27: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Compaction

Page 28: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Paging

Page 29: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Allocation in Paging

Page 30: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Address Translation

Page 31: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Address Translation

Page 32: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Translating Logical Address into Physical Address

Page 33: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Implementation of Page TablePage table for each process is kept in main memory.Page-table base register (PTBR) points to the page

table.Page-table length register (PTLR) indicates size of the

page table.In this scheme every data/instruction access requires

two memory accesses. One for the page table and one for the data/instruction.

The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs).

Page 34: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Associative Memory

Page 35: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Pros and Cons of PagingAdvantages:

Efficient memory useSimple partition management due to discontinuous

loading and fixed partition sizeNo compaction is necessaryEasy to share pages

DisadvantagesJob size <= memory sizeInternal fragmentationNeed special hardware for address translationSome main memory is used for page tableAddress translation lengthens memory cycle times

Page 36: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Segmentation

Page 37: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Memory Allocation in Segmentation

Page 38: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Address Mapping in SegmentationThe user specifies each address by two

quantities: segment name/number and offset.   <segment-number, offset> Mapping from logical address to physical

address is done with the help of a segment table.Segment table – maps two-dimensional physical

addresses; each table entry has:base – contains the starting physical address where the

segments reside in memory. limit – specifies the length of the segment.

Page 39: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Address Mapping in Segmentation

Page 40: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Segmentation with Paging

Page 41: Memory Management. Process must be loaded into memory before being executed. Memory needs to be allocated to ensure a reasonable supply of ready processes

Comparison of Paging and Segmentation