89
Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems Introduction Computer System Structures Operating System Structures Processes Process Synchronization Deadlocks CPU Scheduling Memory Management Virtual Memory File Management Security Networking Distributed Systems Case Studies Conclusions

Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems Introduction Computer System Structures Operating System

  • View
    220

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 1 © 2000 Franz Kurfess

Course OverviewPrinciples of Operating Systems

Course OverviewPrinciples of Operating Systems

Introduction Computer System

Structures Operating System

Structures Processes Process Synchronization Deadlocks CPU Scheduling

Memory Management Virtual Memory File Management Security Networking Distributed Systems Case Studies Conclusions

Page 2: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 2 © 2000 Franz Kurfess

Chapter Overview Memory Management

Chapter Overview Memory Management

Motivation Objectives Background Address Spaces

logical physical

Partitioning fixed dynamic

Swapping

Contiguous Allocation Paging Segmentation Segmentation with Paging Important Concepts and

Terms Chapter Summary

Page 3: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 3 © 2000 Franz Kurfess

MotivationMotivation

after CPU time, memory is the second most important resource in a computer system

even with relatively large amounts of memory, the amount of available memory is often not satisfactory

getting information from hard disk instead of main memory takes orders of magnitudes longer 60 ns access time for main memory 10 ms (= 10,000,000 ns) average access time for hard

disks

several processes must coexist in memory

Page 4: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 4 © 2000 Franz Kurfess

ObjectivesObjectives

understand the purpose and usage of main memory in computer systems

understand the addressing scheme used by the CPU to access information in main memory

distinguish between logical and physical addresses understand various methods to allocate available

memory sections to processes

Page 5: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 5 © 2000 Franz Kurfess

Background and PrerequisitesBackground and Prerequisites

main memory and computer system architecture linking and loading program execution and processes instruction execution

Page 6: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 6 © 2000 Franz Kurfess

MemoryMemory

main memory is used to store information required for the execution of programs code, data, auxiliary information

the CPU can only access items that are in main memory

memory is a large array of addressable words or bytes

a process needs memory space to execute sufficient memory can increase the utilization of

resources and improve response time

Page 7: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 7 © 2000 Franz Kurfess

Hardware ArchitectureHardware Architecture

[David Jones]

CPUMain Memory

I/O Devices

System Bus

Control Unit

Reg

iste

rs

Arithmetic LogicUnit

(ALU)

Controllers

Page 8: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 8 © 2000 Franz Kurfess

CPU

Main Memory

Bus

Control Unit

Reg

iste

rs

Arithmetic LogicUnit

(ALU)

Memory and CPUMemory and CPU

MM

U

Page 9: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 9 © 2000 Franz Kurfess

Memory OrganizationMemory Organization memory size =

memory “height” * memory width usually measured in MByte

memory “height” number of memory locations = physical address space

memory width number of bits per memory location either one Byte or one word

memory location each location has its own unique (physical)

address larger items may be assigned to consecutive

locations

0

7FFFFMain Memory

Page 10: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 10 © 2000 Franz Kurfess

Hierarchy of Storage DevicesHierarchy of Storage DevicesRegisters

Cache

Main Memory

Electronic Disk

Magnetic Disk

Optical Disk

Magnetic Tape

Page 11: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 11 © 2000 Franz Kurfess

Storage Device CharacteristicsStorage Device CharacteristicsStorageDevice

AccessSpeed

Capacity Cost Volatility

Registers nanoseconds Kbytes veryhigh

high

Cache tens ofnanoseconds

Mbytes $100 perMbyte

high

MainMemory

50-70nanoseconds

tens orhundreds of

Mbytes

$10 perMbyte

high

ElectronicDisk

100-200nanoseconds

hundreds ofMbytes

$1 perMbyte

medium(battery)

MagneticDisk

5-15milliseconds

tens of Gbytes $100 perGbyte

low

OpticalDisk

100milliseconds

hundreds ofGbytes

$10 perGbyte

low

TapeDrive

milliseconds toseconds

hundreds ofGbytes to

TBytes

$1 perGbyte

low

Page 12: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 12 © 2000 Franz Kurfess

From Program to ProcessFrom Program to ProcessProcess Image

Process Control Block

User Stack

Shared Address Space

Data

Program

Load Module

Data

Program

Loader

Data

Program

Page 13: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 13 © 2000 Franz Kurfess

Linking and LoadingLinking and Loading

LinkerLibrary

Module 1

Module 2

Module 3Loader

Main Memory

ProcessImage

Load Module

Page 14: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 14 © 2000 Franz Kurfess

LinkingLinking

several components are combined into one load module modules, objects libraries

references across modules must be resolved the load module is passed to the loader

Page 15: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 15 © 2000 Franz Kurfess

Static vs. Dynamic LinkingStatic vs. Dynamic Linking

static linking done at compile or link time does not facilitate sharing of libraries

dynamic linking linking is deferred until load or runtime to allow integration

of libraries

Page 16: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 16 © 2000 Franz Kurfess

Dynamic Runtime LinkingDynamic Runtime Linking

linking is postponed until runtime unresolved references initiate the loading of the

respective module and its linking to the calling module

easy sharing of modules only the modules that are really needed are loaded

and linked rather complex

Page 17: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 17 © 2000 Franz Kurfess

LoadingLoading

creation of an active process from a program process image

process image is loaded into main memory execution can start now

symbolic addresses must be resolved into relative or absolute addresses

absolute loading relocatable loading dynamic run-time loading

Page 18: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 18 © 2000 Franz Kurfess

Absolute LoadingAbsolute Loading

a given load module must always be given the same location in main memory

all address references must be absolute refer to the starting address of physical memory can be done by the programmer or compiler

severe disadvantages inflexible: modifications require recoding or recompilation

of the program requires knowledge about memory allocation impractical for multiprogramming, multitasking, etc.

Page 19: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 19 © 2000 Franz Kurfess

Relocatable LoadingRelocatable Loading

load module can be located anywhere in main memory

addresses are relative to the start of the program the location where the image is loaded is added to

the relative address at load time or during execution

Page 20: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 20 © 2000 Franz Kurfess

Dynamic Runtime LoadingDynamic Runtime Loading

memory references are still relative in the process image when it is loaded into main memory

the absolute address is only calculated when the instruction is executed requires hardware support (MMU)

allows swapping out and back in of processes the new location of a process may be different after a

swap

Page 21: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 21 © 2000 Franz Kurfess

Processes and AddressesProcesses and AddressesProcess

Process Control Block

User Stack

Shared Address Space

Data

Program

Process ControlInformation

Program EntryPoint

Top of Stack

ProgramExecution

Data Access

process image determines logical address

space must be placed in physical

memory process execution

relative addresses: relevant information is addressed within the process image

must be converted to absolute (physical) addresses

Page 22: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 22 © 2000 Franz Kurfess

Process in Main MemoryProcess in Main MemoryMain Memory

Process Control Block

User Stack

Shared Address Space

Data

Program

Process ControlInformation

Program EntryPoint

Top of Stack

ProgramExecution

Data Access

mapping from logical address space (process image) to physical address space (main memory) memory size address conversions

contiguous allocation the whole process

image is allocated in one piece

Page 23: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 23 © 2000 Franz Kurfess

Processes and Address SpacesProcesses and Address SpacesProcess 1 Process 2 Process n

Process Identification

Process StateInformation

Process Control Information

System Stack

User Stack

UserAddress Space

Shared Address Space

Process Identification

Process StateInformation

Process Control Information

System Stack

User Stack

UserAddress Space

Shared Address Space

Process Identification

Process StateInformation

Process Control Information

System Stack

User Stack

UserAddress Space

Process ControlBlock

[adapted from Stallings 98]

Page 24: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 24 © 2000 Franz Kurfess

Processes in MemoryProcesses in Memory several processes need to

be accommodated OS has its own memory

section simplified view

larger number of processes processes do not occupy one

single section in memory, but several smaller ones (non-contiguous allocation)

not the whole process image is always present in memory (virtual memory)

Process 1

Process 2

Process n

OperatingSystem

Main Memory

Page 25: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 25 © 2000 Franz Kurfess

Instruction Execution CycleInstruction Execution Cycle

the execution of one instruction consists of several steps fetch an instruction from memory decode the instruction fetch operands from memory execute instruction store result in memory

the execution of one instruction may require several memory accesses even worse with indirect addressing (pointers)

Page 26: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 26 © 2000 Franz Kurfess

TerminologyTerminology contiguous allocation

information is stored in consecutive memory addresses a process occupies a single section of memory

non-contiguous allocation a process is distributed over several, disjoint sections of main

memory information is not necessarily stored in consecutive addresses

real memory memory directly available in the form of RAM chips

virtual memory extension of real memory through the use hard disk space for less

frequently used information

Page 27: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 27 © 2000 Franz Kurfess

Terminology (cont.)Terminology (cont.) logical address

address generated by the CPU

physical addresses address applied to memory chips

block data are transferred in units of fixed size used for hard disks and similar devices typical block sizes are 512 Bytes to 16 KBytes

locality of reference there is a good chance that the next access to instructions or data will be

close to the current one

fragmentation memory or disk space is allocated in small parts, leading to inefficient

utilization

Page 28: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 28 © 2000 Franz Kurfess

Memory Management Requirements

Memory Management Requirements

relocation protection sharing logical organization physical organization

Page 29: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 29 © 2000 Franz Kurfess

RelocationRelocation

the location of a process image in main memory may be different for different runs of the program availability of memory areas at the start of the execution process may be temporarily swapped out

as a consequence, the logical address is different from the physical address

the conversion is performed by the memory management

common schemes require hardware support special registers, or memory management unit (MMU)

Page 30: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 30 © 2000 Franz Kurfess

Static RelocationStatic Relocation

relocation done at linking or at loading time requires knowledge about available memory sections

a statically relocated program cannot be moved once it has its memory section assigned

Page 31: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 31 © 2000 Franz Kurfess

Dynamic RelocationDynamic Relocation

relocation done at run time requires hardware support

relocation registers, MMU

the process image, or a part of it, can be moved around at any time the value of the relocation registers must be changed

accordingly

Page 32: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 32 © 2000 Franz Kurfess

ProtectionProtection processes may only access their own memory sections

in addition, shared memory sections may be accessible to selected processes

access to other memory areas must be restricted sections of other processes sections of the operating system

memory references must be checked at run time the location and size of the memory section of a process may

change during execution

requires hardware assistance frequently relocation and limit register

Page 33: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 33 © 2000 Franz Kurfess

SharingSharing

code processes executing the same program should share its

code

data data structures used by several programs

examples for sharing code libraries common programs (editors, mail, etc.) producer-consumer scenarios with shared memory

Page 34: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 34 © 2000 Franz Kurfess

Shared LibrariesShared Libraries

prewritten code for commonly used functions is stored in libraries

statically linked libraries library code is linked at link time into an executable

program results in large executables with no sharing

dynamically linked libraries if libraries are linked at run time they can be shared

between processes

Page 35: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 35 © 2000 Franz Kurfess

Logical OrganizationLogical Organization

abstract view of memory programs are usually composed of separate modules,

procedures, objects, etc. separate management of these components allows

various optimizations development, testing, compilation allocation sharing protection

it also is more complicated references across modules must be resolved at runtime

Page 36: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 36 © 2000 Franz Kurfess

Physical OrganizationPhysical Organization

view of memory as physical device dictated by the structure of hardware

size of the physical address space available memory size memory width arrangement of memory modules

interleaved memory, banks, etc.

usually handled by the MMU

Page 37: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 37 © 2000 Franz Kurfess

Address SpacesAddress Spaces

address binding logical address space physical address space

Page 38: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 38 © 2000 Franz Kurfess

Logical to Physical AddressLogical to Physical AddressProcess

Process Control Block

User Stack

Shared Address Space

Data

Program

Main Memory

CPU MMU

log

ical

ad

dre

ss

ph

ysic

al a

dd

ress

Page 39: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 39 © 2000 Franz Kurfess

Logical vs. Physical AddressLogical vs. Physical Address

+logicaladdress

physicaladdress

MAR

relocationregister

Memory

CPU

logical address is generated by the CPU

physical address is loaded into the memory address register (MAR) usually part of the MMU

Page 40: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 40 © 2000 Franz Kurfess

Memory AllocationMemory Allocation

assign sections of memory to processes section size: partitioning contiguous

one section per process

non-contiguous each process has several sections

Page 41: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 41 © 2000 Franz Kurfess

PartitioningPartitioning

fixed/dynamic paging/segmentation virtual memory

Page 42: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 42 © 2000 Franz Kurfess

Fixed PartitioningFixed Partitioning

memory is divided into a number of fixed partitions sizes of partitions may be different

chosen by the OS, developer, system administrator

maintain a queue for each partition internal fragmentation

space in a partition not used by a process is lost

number of partitions (specified at system generation) limits number of active processes

Small jobs do not use partition space efficiently used by older IBM OS/360 (MFT)

Page 43: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 43 © 2000 Franz Kurfess

OS

50K

100K

200K

75K

Fixed Partitions - Multiple QueuesFixed Partitions - Multiple Queues

Processes

MainMemory

Page 44: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 44 © 2000 Franz Kurfess

OS

50K

100K

200K

75K

Fixed Partitions - Single QueueFixed Partitions - Single Queue

ProcessesMainMemory

Page 45: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 45 © 2000 Franz Kurfess

Variable PartitioningVariable Partitioning

each process is assigned a partition number, size, and location of the partition can vary overcomes some problems of fixed partitioning

but still inefficient use of memory

higher overhead

Page 46: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 46 © 2000 Franz Kurfess

SwappingSwapping

processes are temporarily taken out of main memory to make more space available

swap space secondary storage space provides a special area for these

processes

swap time very high compared with in-memory context switch example:

1 MByte process image, 10 MByte/sec transfer rate= 100 ms swap time

head seek time, latency not considered

Page 47: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 47 © 2000 Franz Kurfess

Dynamic Storage AllocationDynamic Storage Allocation

problem: find a suitable free section of memory to accommodate a process

analogy: packing boxes of different sizes ~ free memory sections items to be packed ~ processes

Page 48: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 48 © 2000 Franz Kurfess

Storage Allocation StrategiesStorage Allocation Strategies

first-fit:allocate the first hole that is big enough

best-fit: allocate the smallest hole that is big enough

worst-fit: allocate the largest hole

Page 49: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 49 © 2000 Franz Kurfess

First-FitFirst-Fit

low overhead no searching required

generates reasonably large holes on average

Page 50: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 50 © 2000 Franz Kurfess

Best-FitBest-Fit

slower than first-fit must search the entire list

tends to fill up memory with tiny useless holes can be made faster by sorting the hole list by size

Page 51: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 51 © 2000 Franz Kurfess

Worst-FitWorst-Fit

worst-fit leaves larger holes than best-fit

Page 52: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 52 © 2000 Franz Kurfess

ObservationsObservations

overhead to keep track of small holes may be substantially larger than the hole itself

in simulations first-fit and best-fit give better results, with first-fit being faster

separate lists for processes and holes to speed up these algorithms

Page 53: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 53 © 2000 Franz Kurfess

Keeping Track of Memory UsageKeeping Track of Memory Usage

bit maps linked lists

Page 54: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 54 © 2000 Franz Kurfess

Bit MapsBit Maps

memory is divided into allocation units each allocation unit is represented by a bit bit map shows which parts of the memory are in use

or free

Page 55: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 55 © 2000 Franz Kurfess

Memory

...

Bit Map

...0 1 1 1 0 1 0 0

Bit MapBit Map

Page 56: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 56 © 2000 Franz Kurfess

Bit Map PropertiesBit Map Properties

the smaller the allocation unit the bigger the bit map once the allocation unit is fixed, the size of the bit

map does not change for a fixed (physical) memory size

searching a bit map for a block of a certain size is a slow operation requires counting the number of successive ones or zeros

Page 57: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 57 © 2000 Franz Kurfess

Linked ListsLinked Lists

memory is represented by a linked list of allocated and free segments

the list is sorted by address facilitates updating

when a process terminates, its space can be combined with that of its neighbors

Page 58: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 58 © 2000 Franz Kurfess

Contiguous AllocationContiguous Allocation

each process occupies a single memory section requires adjustment of addresses for relocation may lead to inefficient memory utilization

processes must fit into free memory sections relatively large “holes” may be left

Page 59: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 59 © 2000 Franz Kurfess

Contiguous AllocationContiguous AllocationMain Memory

Process Control Block

User Stack

Shared Address Space

Data

Program

Process ControlInformation

Program EntryPoint

Top of Stack

ProgramExecution

Data Access

contiguous allocation the whole process

image is allocated in one piece

Page 60: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 60 © 2000 Franz Kurfess

Non-Contiguous AllocationNon-Contiguous Allocation

a process occupies several disjoint memory sections addresses need to be adjusted for relocation and

displacement relative distances between different sections of one process

better memory utilization small free sections can be utilized by parts of processes “holes” still exist, but are smaller

higher overhead requires a table to keep track of the parts of a process

usually in the form of paging, segmentation

Page 61: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 61 © 2000 Franz Kurfess

Non-Contiguous AllocationNon-Contiguous AllocationMain Memory

Process Control Block

Process ControlInformation

Program EntryPoint

Top of Stack

ProgramExecution

Data Access

parts of the process image are allocated to different sections of main memory

may break relative addresses

Program

Data

User Stack

Page 62: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 62 © 2000 Franz Kurfess

PagingPaging

a frequently used case of non-contiguous allocation the process image is divided into pieces of equal

size (pages) physical memory is divided into pieces of the same

size (page frames) any page can be allocated to any page frame memory allocation becomes very simple additional overhead to keep track of which page is in

which page frame kept in a page table

Page 63: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 63 © 2000 Franz Kurfess

Paging DiagramPaging DiagramProcess

Process Control Block

User Stack

Shared Address Space

Data

Program

Main Memory

Page Table

123

56789101112131415

3458

122

99385543

131102171

76123144

93

4 68

Page 64: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 64 © 2000 Franz Kurfess

Page SizePage Size

small page size less internal fragmentation large page table

large page size less overhead, both in

page table size page transfer time.

Page 65: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 65 © 2000 Franz Kurfess

Page Table ImplementationPage Table Implementation

set of dedicated registers memory associative registers

Page 66: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 66 © 2000 Franz Kurfess

Dedicated RegistersDedicated Registers

reasonable only for small page tables example:

16 bit logical address space

page size = 8K

page table size = 8

Page 67: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 67 © 2000 Franz Kurfess

Page Table in MemoryPage Table in Memory

use a page table base register (PTBR) to point to the page table of the current process

two memory accesses are needed for every data access

speed could be intolerable

Page 68: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 68 © 2000 Franz Kurfess

Address TranslationAddress TranslationMain Memory

3458

122

99385543

131102171

76123144

93

68

CPU

logical address physical address

Page Table

page # frame #offset offset

Page 69: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 69 © 2000 Franz Kurfess

Address TranslationAddress Translation the page table contains an entry for every page of the

process each process has its own page table

a logical address is divided into page number and offset

the physical address is calculated by looking up the frame in which the requested page resides adding the offset to the starting address of that frame

the page table itself is in main memory as a consequence an extra memory access is required for every

regular memory access this doubles the overall time for memory accesses

Page 70: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 70 © 2000 Franz Kurfess

Address TranslationAddress Translation

page = logical-address / page-size offset = logical-address mod page-size page-frame = page-table[page] physical address =

(page-frame * page-size) + offset

Page 71: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 71 © 2000 Franz Kurfess

Fragmentation in PagingFragmentation in Paging

no external fragmentation every page frame can be used

internal fragmentation the last page in the logical space of every process may not

be completely used (on average: half a page per process)

Page 72: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 72 © 2000 Franz Kurfess

Associative RegistersAssociative Registers

associative registers or translation look-aside buffers (TLBs)

special-purpose hardware to improve access times for paging

acts as cache for page table entries only few page table entries are kept in the associative

registers hit ratio is the percentage of times that a page number

is found in the associative registers simultaneous (“associative”) access to all stored values

Page 73: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 73 © 2000 Franz Kurfess

TLB SchemaTLB SchemaMain Memory

TLB

CPU

logical address

physical address

3458

122

99385543

131102171

76123144

93

68

Page Table

page # offset

frame # offset

page # frame #

TLB hit

TLBmiss

Page 74: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 74 © 2000 Franz Kurfess

Effective Access TimeEffective Access Time

the average access time for main memory depends on how often the requested page number is found in the TLB this is known as the hit ratio

the effective access time (EAT) is calculated by weighing the case when the page is found in the TLB vs. not found according to the probability given by the hit ratio

effective access time = hit ratio * entry found + (1-hit ratio) * entry not found

Page 75: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 75 © 2000 Franz Kurfess

Example EATExample EAT basic assumptions

memory access time: 60 ns; TLB access time: 6 ns EAT no paging: 60 ns EAT page table only: 60 ns + 60 ns = 120 ns EAT TLB/page table

hit ratio 90% EAT = 0.9 * (6 + 60) + 0.1 * (6 + 60 + 60)

= 59.4 + 12.6 = 72 20% performance loss with respect to “no paging”

hit ratio 99% EAT = 0.99 * (6 + 60) + 0.01 * (6 + 60 + 60)

= 65.34 + 1.26 = 66.60 11% performance loss with respect to “no paging”

Page 76: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 76 © 2000 Franz Kurfess

ProtectionProtection

protection bits are associated with each page a page can have any combination of: read, write, or

execute permissions

Page 77: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 77 © 2000 Franz Kurfess

Very Large Page TablesVery Large Page Tables

page tables can be huge:

32-bit virtual address space (maximum size of processes)

4K page size

page table size = 232 / 212 = 220 entries

over 1 million entries!

Page 78: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 78 © 2000 Franz Kurfess

Multilevel PagingMultilevel Paging

instead of using huge page tables, the page table itself can be paged it is split up into several smaller level 2 page tables these tables are accessed via a level 1 page table the page number is divided into two parts

level 1 page number page offset for level 2 pages

modern computer architectures/OSs support multilevel paging Windows NT, OS/2: 32-bit virtual address, two-level paging, 4K

page size Unix: hardware dependent (three or four levels)

Page 79: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 79 © 2000 Franz Kurfess

Multilevel PagingMultilevel PagingMain Memory

CPU

logical address

physical address

Level 1Page Table

p1 p2 offset

frame # offset

page #

Level 2Page Tables

p2

Page 80: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 80 © 2000 Franz Kurfess

Inverted Page TablesInverted Page Tables

one page table in memory for all processes size of the table is determined by physical memory each process maintains an external page table on secondary

storage inverted page table maps page frames to the virtual

pages of various processes advantage

avoids keeping huge page tables in main memory disadvantage

cannot use page number as index into the page table must search for process id in the table

Page 81: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 81 © 2000 Franz Kurfess

Inverted Page TableInverted Page TableMain Memory

CPU

logical address

physical address

Hash Table

pid page # offset

frame # offset

Page Table

pid page # frame

Page 82: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 82 © 2000 Franz Kurfess

Shared PagesShared Pages

pages holding the code section of commonly used programs can be shared code must be re-entrant data sections must be different

each user’s page table maps the pages into the same physical frames

page boundaries do not necessarily coincide with the structure of the program

Page 83: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 83 © 2000 Franz Kurfess

code

data

editor process

5

2

3

4

P00

editor

editor

editor

data - P2

data - P0

data - P1

Shared Pages ExampleShared Pages Example

page tables 5

2

3

0

P1

5

2

3

7

P2

1

2

3

4

5

6

7

Page 84: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 84 © 2000 Franz Kurfess

SegmentationSegmentation

logical address space is divided into segments according to the internal structure of the process program: procedures, functions data: records, arrays

management is more complicated each segment has to be dynamically allocated

sharing of segments is easier boundaries are reflected

external fragmentation

Page 85: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 85 © 2000 Franz Kurfess

Segmentation Diagram

Segmentation Diagram

Process

Process Control Block

User Stack

Shared Address Space

Data

Program

Main Memory

Program

Segment Table

123

56789101112131415

234425592872

65398958025733662291169221447008612310449309

4

Program

6539

Page 86: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 86 © 2000 Franz Kurfess

Combined Paging/SegmentationCombined Paging/Segmentation

paging characteristics transparent to the programmer eliminates external fragmentation manipulating fixed size blocks implies development of

sophisticated memory management algorithms

segmentation characteristics visible to programmer ability to handle dynamic data structures modularity support for protection and sharing

Page 87: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 87 © 2000 Franz Kurfess

Segmentation with PagingSegmentation with Paging

paging is used to manage the segments combines advantages of both approaches

Page 88: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 88 © 2000 Franz Kurfess

Important Concepts and TermsImportant Concepts and Terms address binding best fit allocation bit map contiguous allocation dynamic linking/loading dynamic relocation effective access time external fragmentation first fit allocation fragmentation hardware internal fragmentation inverted page table library linker

loader logical address memory protection MMU multilevel paging non-contiguous allocation page page frame page table physical address relocation run-time dynamic linking segmentation static relocation worst fit allocation

Page 89: Memory Management 1 © 2000 Franz Kurfess Course Overview Principles of Operating Systems  Introduction  Computer System Structures  Operating System

Memory Management 89 © 2000 Franz Kurfess

Summary Memory ManagementSummary Memory Management

allocation of processes in main memory efficient utilization of memory various allocation strategies paging and segmentation