memory management. memory management is the managing of the pool of available memory in a computer...

88
Memory Management Memory Management

Upload: joanna-hamilton

Post on 27-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Memory ManagementMemory Management

Page 2: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Memory Management Memory Management is the managing of is the managing of the pool of available memory in a the pool of available memory in a computer system, allocating space to computer system, allocating space to application programs and making sure that application programs and making sure that they do not interfere with each otherthey do not interfere with each other

The problem of memory management is The problem of memory management is composed of three sub-problems:composed of three sub-problems:

Page 3: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

AllocationAllocation

This is the problem of assigning suitable This is the problem of assigning suitable storage space to processes as required.storage space to processes as required.

An efficient memory allocation mechanism An efficient memory allocation mechanism may be expected to respond quickly to:may be expected to respond quickly to: requests for assorted amounts of memoryrequests for assorted amounts of memory to assign memory only when neededto assign memory only when needed To reclaim memory promptly when its use is To reclaim memory promptly when its use is

completecomplete

Page 4: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

RelocationRelocation This is the problem of matching programs and data to This is the problem of matching programs and data to

the memory locations that have been allocated to them. the memory locations that have been allocated to them. A program on disk is a binary executable file, it must be A program on disk is a binary executable file, it must be

loaded into memory in a process to be executed. loaded into memory in a process to be executed. In general, a user process can reside anywhere in free In general, a user process can reside anywhere in free

memory. Possibly even being moved once execution memory. Possibly even being moved once execution has begun. Modifications must be made to addresses in has begun. Modifications must be made to addresses in machine instructions and data to reflect the addresses machine instructions and data to reflect the addresses actually used.actually used.

Addresses represented in source programs are symbolic, such Addresses represented in source programs are symbolic, such as the name of a variable. Sumas the name of a variable. Sum

The compiler will “bind” these symbolic addresses to relocatable The compiler will “bind” these symbolic addresses to relocatable addresses, such as 100 bytes from the beginning of the module.addresses, such as 100 bytes from the beginning of the module.

The linkage editor/loader will bind these relocatable addresses The linkage editor/loader will bind these relocatable addresses to absolute address, such as byte 74321to absolute address, such as byte 74321

Page 5: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

ProtectionProtection

This is the problem of restricting a This is the problem of restricting a process’s access to memory that has process’s access to memory that has actually been allocated to it.actually been allocated to it.

Page 6: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

When we think of the structure of an OS, When we think of the structure of an OS, an OS is generally a collection of routinesan OS is generally a collection of routines

Some that are essential such as those that Some that are essential such as those that control physical I/O are control physical I/O are residentresident (or always (or always loaded into memory)loaded into memory)

Others are Others are transienttransient, which means they are , which means they are stored on disk, and read into memory only stored on disk, and read into memory only when needed. These are usually utility when needed. These are usually utility routines that are not essential to the control routines that are not essential to the control and operation of the system.and operation of the system.

Page 7: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Normally the OS occupies low memory beginning with address 0, although the resident portion of the OS can be in high or low memory, the location of the interrupt vector is the primary factor in governing the kernel’s location.

Control information comes first, followed by resident routines and the remaining memory forms the “transient area” where application programs and transient routines are loaded

System Control Information

Resident Operating System

Transient Area

0

Page 8: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Memory allocationMemory allocation refers to the action of selecting and reserving refers to the action of selecting and reserving particular parts of memory for use by particular processes.particular parts of memory for use by particular processes.

Early operating systems allocated space in physical memory with Early operating systems allocated space in physical memory with little or no hardware assistance, or protection.little or no hardware assistance, or protection.

The need for memory management grew as OS capabilities The need for memory management grew as OS capabilities changed.changed.

Usually memory is allocated to a process in discrete blocks or Usually memory is allocated to a process in discrete blocks or regions that must be contiguous (they must occupy consecutive regions that must be contiguous (they must occupy consecutive addresses in the addresses in the logicallogical or or virtualvirtual address space of the process.) address space of the process.)

These logical addresses are converted to physical addresses when These logical addresses are converted to physical addresses when data and instructions are fetched during the instruction execution data and instructions are fetched during the instruction execution cycle.cycle.

Page 9: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

In early systems…In early systems…

The evolution of memory management The evolution of memory management techniques has been shaped largely by the techniques has been shaped largely by the resource’s early characteristics.resource’s early characteristics. Memory was expensive and slowMemory was expensive and slow Only very limited amounts were availableOnly very limited amounts were available Multiprogramming evolved partly because of the need Multiprogramming evolved partly because of the need

to share such an expensive resource among several to share such an expensive resource among several concurrent processes, increasing the amount of work concurrent processes, increasing the amount of work performed.performed.

The memory allocation strategies have evolved as The memory allocation strategies have evolved as OS’s have become more complex.OS’s have become more complex.

Page 10: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

No AllocationNo Allocation Simplest strategy is none at all.Simplest strategy is none at all.

Possible only on systems that run Possible only on systems that run a single program at a timea single program at a time

Each program has total access to Each program has total access to ALL portions of memory and can ALL portions of memory and can manage and use it in any manner.manage and use it in any manner.

Simple approach with costs Simple approach with costs nothing to implement (it entails no nothing to implement (it entails no OS services)OS services)

The program can use all memory The program can use all memory previously occupied by the OS. previously occupied by the OS. It’s the program’s responsibility to It’s the program’s responsibility to reload the OS when it completes.reload the OS when it completes.A bare machine with no

memory allocation and no OS in memory

Page 11: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Single-user Operating SystemsSingle-user Operating Systems

The simplest type of OS serves only a The simplest type of OS serves only a single user and runs only one process at a single user and runs only one process at a time.time.

Memory needs only to be shared between Memory needs only to be shared between the the single processsingle process and the and the OSOS itself itself

A program may use whatever memory it A program may use whatever memory it needs as long as the required memory needs as long as the required memory exists and is not needed by the OS.exists and is not needed by the OS.

Page 12: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Single user OSSingle user OS

Page 13: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Memory in such systems is typically viewed as Memory in such systems is typically viewed as being divided into two regions: one for the OS being divided into two regions: one for the OS and one for the application programand one for the application program

The placement of the resident portion of the OS The placement of the resident portion of the OS in memory can be in either high or low memory in memory can be in either high or low memory addresses:addresses:

Some systems use low memory for the resident Some systems use low memory for the resident portion of the OS because this region contains portion of the OS because this region contains locations with special built-in properties such as locations with special built-in properties such as interrupt vectors.interrupt vectors.

Sometimes, even though it controls special Sometimes, even though it controls special hardware locations in low memory, the OS itself hardware locations in low memory, the OS itself may be located in high memory. may be located in high memory.

Page 14: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Protecting the NucleusProtecting the Nucleus RAM is easily changed. Providing allocation mechanisms is only RAM is easily changed. Providing allocation mechanisms is only

meaningful when it is accompanied by protection.meaningful when it is accompanied by protection.

With multiple programs in memory, it is easy for one program to With multiple programs in memory, it is easy for one program to destroy the contents of memory belonging to another program.destroy the contents of memory belonging to another program.

Some mechanism should exist to prevent a process’s access to Some mechanism should exist to prevent a process’s access to unallocated locations.unallocated locations.

Generally the OS keeps track of the space assigned to each program. Generally the OS keeps track of the space assigned to each program. In terms of the address of the first byte of memory assigned to the In terms of the address of the first byte of memory assigned to the process, and the range (or # of bytes assigned to the process)process, and the range (or # of bytes assigned to the process)

If a program attempts to modify the contents of memory locations that If a program attempts to modify the contents of memory locations that do not belong to it, the OS’s memory protection routine intervenes do not belong to it, the OS’s memory protection routine intervenes and usually terminates the program.and usually terminates the program.

For single user OS’s, the lack of protection is typically viewed as not For single user OS’s, the lack of protection is typically viewed as not serious since it is a single user machine, and disruption of OS serious since it is a single user machine, and disruption of OS services would affect a single user.services would affect a single user.

Page 15: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Allocation for multiprogrammingAllocation for multiprogramming Memory Management in a Memory Management in a

multiprogramming environment multiprogramming environment is more difficult than with a is more difficult than with a single user system.single user system.

Multiprogramming operating Multiprogramming operating systems require that memory be systems require that memory be shared among two or more shared among two or more processes.processes.

A portion of memory must be A portion of memory must be chosen for allocation to each chosen for allocation to each process, when it is createdprocess, when it is created

Memory allocated to a process Memory allocated to a process should be protected from should be protected from access by another processaccess by another process

As part of the process we will As part of the process we will need to keep track of the need to keep track of the starting address of the memory starting address of the memory assigned to the process, and assigned to the process, and the range of addresses it can the range of addresses it can access. The CPU must load access. The CPU must load this information when a process this information when a process is selected for execution.is selected for execution.

Resident Monitor

Application 2

Application 1

Page 16: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

When allocating memory, an OS When allocating memory, an OS can take either a static or dynamic can take either a static or dynamic

approachapproach

Page 17: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

With With static allocationstatic allocation a process is assigned a process is assigned all the memory it is expected to require when all the memory it is expected to require when it is loaded and started.it is loaded and started.

It can not be loaded until sufficient memory is It can not be loaded until sufficient memory is availableavailable

The process keeps all its memory throughout The process keeps all its memory throughout its lifetime, and cannot obtain moreits lifetime, and cannot obtain more

Simple to manage strategy, but does not Simple to manage strategy, but does not allow effective sharing of memory in a multi-allow effective sharing of memory in a multi-programmed system, may result in programmed system, may result in underunder or or overover allocation. allocation.

Page 18: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamic allocationDynamic allocation offers more efficient offers more efficient memory use.memory use.

After an initial allocation, each process is After an initial allocation, each process is permitted to request addition memory as needed permitted to request addition memory as needed during execution and is expected to release during execution and is expected to release memory it no longer needs.memory it no longer needs.

In this strategy, the OS is presented with an In this strategy, the OS is presented with an unpredictable sequence of unpredictable sequence of allocateallocate and and freefree requests to assign and release individual blocks requests to assign and release individual blocks of memoryof memory

Memory is viewed as consisting of a Memory is viewed as consisting of a variablevariable number of blocks which may vary in sizenumber of blocks which may vary in size

Each Each requestrequest must be filled by a single must be filled by a single contiguous region made available in the logical contiguous region made available in the logical address space of the requesting process.address space of the requesting process.

Page 19: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

The next issue is HOW to divide the The next issue is HOW to divide the available memory into portions that can available memory into portions that can

be allocated to processes!be allocated to processes!

There are 2 general strategies.There are 2 general strategies.

Page 20: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Static memory area definitionStatic memory area definition:: This is the simplest approach to managing This is the simplest approach to managing

memory.memory. This divides the available memory into a series This divides the available memory into a series

of “fixed-length” partitions, each of which will of “fixed-length” partitions, each of which will hold one program. (USED BY MFT)hold one program. (USED BY MFT)

The # and size of each partition is determined The # and size of each partition is determined when the OS is loaded, and once established when the OS is loaded, and once established does not change. does not change. # of processes is fixed!!!# of processes is fixed!!!

This means that in part the some of the memory This means that in part the some of the memory allocation decisions are made before the actual allocation decisions are made before the actual amount of space needed by a program is known.amount of space needed by a program is known.

Blocks defined and allocated via this strategy Blocks defined and allocated via this strategy are called fixed partitions (partitions).are called fixed partitions (partitions).

Page 21: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamic memory area definition:Dynamic memory area definition:

The transient area is viewed as an The transient area is viewed as an unstructured pool of free space. unstructured pool of free space.

When a program is loaded, the OS When a program is loaded, the OS allocates a allocates a regionregion of memory just sufficient of memory just sufficient to hold the program.to hold the program.

The number of concurrent programs The number of concurrent programs depends of the size and number of depends of the size and number of requests.requests.

Page 22: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Static allocation can be used Static allocation can be used with either static or with either static or

dynamically memory area dynamically memory area definitiondefinition

Page 23: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Assuming a multiprogramming operating system uses Assuming a multiprogramming operating system uses static allocation and either static or dynamic memory static allocation and either static or dynamic memory area definition, several scenarios exist for loading area definition, several scenarios exist for loading programs into memory:programs into memory:

1.1. Several processes can share memory, each being Several processes can share memory, each being allocated the allocated the same amount of spacesame amount of space using using statically-defined statically-defined fixed partitionsfixed partitions.. There is a fixed There is a fixed maximum number of processes. maximum number of processes.

2.2. Several processes can share memory, each being Several processes can share memory, each being allocated allocated different amounts of spacedifferent amounts of space using using statically-defined statically-defined fixed partitionsfixed partitions.. Again, there is a Again, there is a fixed maximum number of processes, based on the fixed maximum number of processes, based on the number of partitions. number of partitions.

3.3. Several processes can share memory, each being Several processes can share memory, each being allocated allocated different amounts of spacedifferent amounts of space using a using a variable number of variable number of variable-size regionsvariable-size regions.. Here the Here the number of processes may have no fixed maximum. number of processes may have no fixed maximum.

Page 24: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Using Statically defined partitionsUsing Statically defined partitions A fixed memory partition strategy divides A fixed memory partition strategy divides

all available space into a fixed number of all available space into a fixed number of partitions.partitions.

The partitions may or may not be all the The partitions may or may not be all the same size (# of bytes)same size (# of bytes)

Each has a fixed size established when Each has a fixed size established when the OS is first loaded and initialized.the OS is first loaded and initialized.

Common strategy in real-time systems Common strategy in real-time systems and early batch OS’s such as OS/360 and and early batch OS’s such as OS/360 and is no longer in use.is no longer in use.

Page 25: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and
Page 26: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

In this example memory is partitioned into a fixed In this example memory is partitioned into a fixed partition system. partition system.

Four partitions allow up to four processes to execute Four partitions allow up to four processes to execute concurrently. concurrently.

Simplifies memory management, because we only need Simplifies memory management, because we only need to identify if a partition is free or allocated. Usually to identify if a partition is free or allocated. Usually accomplished through the use of control blocks which accomplished through the use of control blocks which defines the size of an individual partition, and whether or defines the size of an individual partition, and whether or not it is free.not it is free.

Memory allocated to a process which is not needed by Memory allocated to a process which is not needed by that process is wastedthat process is wasted

If no waiting program can fit into a free partitions the If no waiting program can fit into a free partitions the partition remains empty, and processes must wait.partition remains empty, and processes must wait.

Page 27: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

DisadvantagesDisadvantages

# of process that can run is fixed# of process that can run is fixed

Unused memory at the end of each partition is Unused memory at the end of each partition is wasted if the process does not need the entire wasted if the process does not need the entire partition into which it is loaded.partition into which it is loaded.

Page 28: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamically Defined RegionsDynamically Defined Regions

Allows the dynamic definition of a variable Allows the dynamic definition of a variable number of variable size memory regions. number of variable size memory regions.

The size of each region is determined The size of each region is determined when the region is when the region is allocatedallocated by the OS by the OS

The number of concurrent processes The number of concurrent processes becomes variable, depending on the becomes variable, depending on the amount of memory available when the amount of memory available when the allocation request occurs.allocation request occurs.

Page 29: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Figure 7-5: A Variable Partition Environment

Page 30: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Control blocks are dynamically created to Control blocks are dynamically created to keep track of which regions have been keep track of which regions have been allocated to processes and which areas of allocated to processes and which areas of memory are free.memory are free.

This method has been used by several This method has been used by several OS’s including OS/360 (MVT), some OS’s including OS/360 (MVT), some versions of Windows and Macintosh OS.versions of Windows and Macintosh OS.

Page 31: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

This method is a cross between static and This method is a cross between static and dynamic methodsdynamic methods From the standpoint of the process: From the standpoint of the process:

allocation is static. (only one region will be allocated)allocation is static. (only one region will be allocated) The space assigned to each process is variable and The space assigned to each process is variable and

determined when the process starts, but doesn’t change.determined when the process starts, but doesn’t change.

From the OS’s standpoint:From the OS’s standpoint: Allocation is dynamicAllocation is dynamic It deals with many processes of different sizes that must start It deals with many processes of different sizes that must start

and stop at unpredictable times.and stop at unpredictable times. Memory can become Memory can become fragmentedfragmented as processes are as processes are

allocated regions, and then free them when the processes allocated regions, and then free them when the processes terminate.terminate.

Page 32: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Figure 7-6: Fragmentation in Variable Space Allocation

Page 33: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

A disadvantage is that this method STILL A disadvantage is that this method STILL does not solve the wasted space problem.does not solve the wasted space problem.

Fragmentation occurs when memory Fragmentation occurs when memory becomes divided into many small separate becomes divided into many small separate sections that are not large enough to meet sections that are not large enough to meet the needs of waiting processes.the needs of waiting processes.

As processes terminate, regions become As processes terminate, regions become available but another process can not use available but another process can not use that memory unless it will fit inside the that memory unless it will fit inside the existing region.existing region.

We need to implement methods to We need to implement methods to reorganize processes to reclaim free reorganize processes to reclaim free partitions such as swapping or compactionpartitions such as swapping or compaction

Page 34: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamic allocationDynamic allocation

Processes are allocated memory as requested.Processes are allocated memory as requested. Principal OS support operations are Principal OS support operations are allocate & freeallocate & free Operations are invoked via a system call at the program Operations are invoked via a system call at the program

interface.interface. Heap managementHeap management

Memory is viewed as a collection of Memory is viewed as a collection of blocksblocks of of randomrandom sizes sizes that will be that will be allocatedallocated and and freedfreed in unpredictable order. in unpredictable order.

Essentially, a collection of blocks/holes are scattered throughout Essentially, a collection of blocks/holes are scattered throughout memory. These blocks are of various sizes, and we have memory. These blocks are of various sizes, and we have processes waiting in the input queue.processes waiting in the input queue.

Memory is allocated to processes until finally the memory Memory is allocated to processes until finally the memory requested by the next process is not available .requested by the next process is not available .

The OS can wait until sufficient resources are available, or skip The OS can wait until sufficient resources are available, or skip down the input queue, handling requests that can be fulfilled.down the input queue, handling requests that can be fulfilled.

Page 35: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

AllocateAllocate An allocate call requests a specific amount An allocate call requests a specific amount

of memory from the OS. of memory from the OS. There is no requirement that the memory There is no requirement that the memory

be in a specific location within memorybe in a specific location within memory But it must be But it must be contiguouscontiguous The OS must allocate the memory and The OS must allocate the memory and

make it available in the address space of make it available in the address space of the process.the process.

If no suitable region is available the If no suitable region is available the process must waitprocess must wait

Page 36: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

FreeFree The free operation is used by a process to de-The free operation is used by a process to de-

allocate regions it no longer needs.allocate regions it no longer needs.

Each de-allocated block must be freed in its Each de-allocated block must be freed in its entirety.entirety.

The OS needs to reclaim freed blocks and make The OS needs to reclaim freed blocks and make them available to later allocation requeststhem available to later allocation requests

Any remaining un-freed space belonging to a Any remaining un-freed space belonging to a process is freed when the process terminates.process is freed when the process terminates.

Page 37: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Memory Control BlocksMemory Control Blocks

Blocks of memory whether allocated or free are Blocks of memory whether allocated or free are described by described by memory control blocks (MCBs).memory control blocks (MCBs).

Free memory control blocks Free memory control blocks represent “free” represent “free” blocks of memory (FMCBs)blocks of memory (FMCBs)

Allocated memory control blocksAllocated memory control blocks represent represent allocated blocks of memory. (AMCBs)allocated blocks of memory. (AMCBs)

Free blocks are maintained on a Free blocks are maintained on a linked listlinked list of of FMCB’s ordered by either decreasing size, FMCB’s ordered by either decreasing size, and/or increasing memory address.and/or increasing memory address.

Page 38: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Where are Memory Control blocks stored?Where are Memory Control blocks stored?

FMCB’s and AMCB’s may be stored in a FMCB’s and AMCB’s may be stored in a system control block area that is separate and system control block area that is separate and protected, accessible only by the OSprotected, accessible only by the OS

Alternately, they may be a part of the Alternately, they may be a part of the corresponding memory block they describe, corresponding memory block they describe, conveniently placed as a header at the conveniently placed as a header at the beginning of the blockbeginning of the block

Page 39: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Figure 7-7: Memory Control Blocks

Page 40: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Handling requests:Handling requests: When an When an allocationallocation request is received, the request is received, the

system searches the free list for a block that system searches the free list for a block that is large enough for the process. If the block is large enough for the process. If the block selected is “too big” it is split in half, part is selected is “too big” it is split in half, part is allocated to the process and the other allocated to the process and the other returned to the free list.returned to the free list.

When a process When a process terminatesterminates or or freesfrees allocated allocated memory, the released block is placed back memory, the released block is placed back into the list of free blocks. If the newly into the list of free blocks. If the newly returned block is physically adjacent to other returned block is physically adjacent to other free blocks, they are joined together to form a free blocks, they are joined together to form a new larger free block.new larger free block.

Page 41: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

How do we quickly determine How do we quickly determine which blocks are free or allocatedwhich blocks are free or allocated

Page 42: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Boundary Tag MethodBoundary Tag Method Devised by Knuth in 1973Devised by Knuth in 1973 Two MCB’s are placed in each regionTwo MCB’s are placed in each region At the end of each region is a simplified MCB containing At the end of each region is a simplified MCB containing

the size of the block and an indication of whether or not the size of the block and an indication of whether or not the block is allocatedthe block is allocated

At the beginning of each block is a complete MCB At the beginning of each block is a complete MCB containing the size of the block and all of the appropriate containing the size of the block and all of the appropriate pointers to the next and previous FMCBs or AMCBspointers to the next and previous FMCBs or AMCBs

This method simplifies the joining of adjacent free This method simplifies the joining of adjacent free blocks.blocks.

As blocks are freed, it is simple to check the boundary As blocks are freed, it is simple to check the boundary tag of adjacent blocks. IF they are free then the blocks tag of adjacent blocks. IF they are free then the blocks can be combined into a single larger block.can be combined into a single larger block.

Page 43: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

AMCB

LMCB

120K

AMCB

LMCB

120K

AMCB

LMCB

120K

AMCB

LMCB

120K

FMCB

LMCB

240K

Page 44: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Bit MapsBit Maps Useful when memory is partitioned into blocks of Useful when memory is partitioned into blocks of

equal fixed size.equal fixed size. Each block is represented by a bit, and the value Each block is represented by a bit, and the value

of the bit indicates whether the block is free or of the bit indicates whether the block is free or allocated (1 allocated, 0 free)allocated (1 allocated, 0 free)

Allocation is done in multiples of these fixed size Allocation is done in multiples of these fixed size blocksblocks

The address and size of the memory blocks The address and size of the memory blocks allocated must be stored in or linked to the PCBallocated must be stored in or linked to the PCB

When a process terminates, the appropriate bits When a process terminates, the appropriate bits in the bit map must be set back to zero to in the bit map must be set back to zero to indicate that the memory blocks are now free.indicate that the memory blocks are now free.

Page 45: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Figure 7-8: Memory Management Using Bit Maps

Page 46: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Allocation Strategies for Allocation Strategies for supporting Dynamic supporting Dynamic

Allocation of a Varying Allocation of a Varying number of variable sized number of variable sized

blocksblocks

Page 47: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

* 100KFMCB

* 110KFMCB

* 500KFMCB

* 200KFMCB

* 450KFMCB

* 356KFMCB

First Fit

Process A

Requests

215K

Page 48: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

First FitFirst Fit This method examines each FMCB starting from the This method examines each FMCB starting from the

beginning of the list of FMCBs and selects the beginning of the list of FMCBs and selects the firstfirst one one large enough to fulfill the request.large enough to fulfill the request.

The selected block is then divided into two partsThe selected block is then divided into two parts A portion large enough to fulfill the requestA portion large enough to fulfill the request Remainder is returned to the FMCB (a new smaller block is Remainder is returned to the FMCB (a new smaller block is

created)created)

Method is simple and fastMethod is simple and fast May lead to a rapid breakdown of large blocks, because May lead to a rapid breakdown of large blocks, because

a small request may be filled from a large block.a small request may be filled from a large block. Tendency for small blocks to collect at the front of the Tendency for small blocks to collect at the front of the

free list.free list.

Page 49: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Next FitNext Fit

Page 50: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

A roving pointer is maintained which points to A roving pointer is maintained which points to the location where the last search ended.the location where the last search ended.

New searches begin at this locationNew searches begin at this location The list is treated as a circular list, so that the The list is treated as a circular list, so that the

pointer in the “last” FMCB is linked to the pointer in the “last” FMCB is linked to the beginning of the listbeginning of the list

If the pointer returns to the place where it began, If the pointer returns to the place where it began, then request can not be fulfilledthen request can not be fulfilled

Eliminates some of the problems with the “first-Eliminates some of the problems with the “first-fit” strategy, because small blocks do not fit” strategy, because small blocks do not accumulate at the beginning of the list. accumulate at the beginning of the list.

Allocation is more evenly distributed throughout Allocation is more evenly distributed throughout the list and memory is used more evenly.the list and memory is used more evenly.

Page 51: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Best FitBest Fit

Page 52: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Neither of the previous strategies consider the Neither of the previous strategies consider the size of the block selected.size of the block selected.

The best fit strategy examines ALL free blocks The best fit strategy examines ALL free blocks and chooses the one whose size most closely and chooses the one whose size most closely matches the request.matches the request.

Requires a search for the one best fitting block Requires a search for the one best fitting block (more efficient if FMCBs are linked together in (more efficient if FMCBs are linked together in order of size!)order of size!)

Wastes the smallest amount of memory Wastes the smallest amount of memory But leftover portions are usually too small to be But leftover portions are usually too small to be

useful, so often the block is not divideduseful, so often the block is not divided

Page 53: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Worst-FitWorst-Fit

Page 54: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Selects the block that yields the largest Selects the block that yields the largest remainder after it is divided. remainder after it is divided.

It allocates from the largest block It allocates from the largest block available.available.

FMCBs should be linked in descending FMCBs should be linked in descending order by sizeorder by size

Yields leftovers that are likely to be useful Yields leftovers that are likely to be useful in future allocation requests.in future allocation requests.

However since largest blocks are always However since largest blocks are always consumed first, they are unlikely to be consumed first, they are unlikely to be available if a large request is submitted.available if a large request is submitted.

Page 55: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Choosing a strategyChoosing a strategy Each of the previous strategies have advantages and Each of the previous strategies have advantages and

disadvantagesdisadvantages One measure is the amount of time it takes to “block” – One measure is the amount of time it takes to “block” –

how long before the system is unable to process and how long before the system is unable to process and allocation request because of insufficient memory.allocation request because of insufficient memory.

Fragmentation (both internal and external) and eventual Fragmentation (both internal and external) and eventual blockage is inevitable with all of the algorithmsblockage is inevitable with all of the algorithms Internal fragmentation is when a requested block is not used in Internal fragmentation is when a requested block is not used in

its entirety by the requesting process.its entirety by the requesting process. External fragmentation is when several free blocks exist, but are External fragmentation is when several free blocks exist, but are

not contiguous so they can not be used to fulfill a request.not contiguous so they can not be used to fulfill a request. When blockage occurs it must be resolved via some When blockage occurs it must be resolved via some

technique such as swapping or compaction.technique such as swapping or compaction. Also we must consider HOW do divide the block when Also we must consider HOW do divide the block when

an allocation request if fulfilled. Do we allocate from the an allocation request if fulfilled. Do we allocate from the beginning or the end of the block?beginning or the end of the block?

Page 56: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Recovering BlocksRecovering Blocks The heap manager relies on the “free” The heap manager relies on the “free”

operations to return storage blocks when operations to return storage blocks when they are no longer needed.they are no longer needed.

These blocks must be returned to the free These blocks must be returned to the free list in a way that makes them useful to list in a way that makes them useful to future requests. future requests.

The smaller a block is the less likely it is to The smaller a block is the less likely it is to be useful in fulfilling future allocations.be useful in fulfilling future allocations.

Page 57: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Freeing memory should involve 2 stepsFreeing memory should involve 2 steps1.1. Moving the blocks MCB from the allocated Moving the blocks MCB from the allocated

list to the “free” list.list to the “free” list.

2.2. Checking to determine if adjacent blocks are Checking to determine if adjacent blocks are free, and if so combining them into a single free, and if so combining them into a single larger block represented by a single MCBlarger block represented by a single MCB

If the linked list of FMCBs are ordered by If the linked list of FMCBs are ordered by increasing memory address, then increasing memory address, then checking adjacent blocks is simple.checking adjacent blocks is simple.

Page 58: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

How do we divide blocks How do we divide blocks efficiently?efficiently?

As previously mentioned, free blocks are divided As previously mentioned, free blocks are divided when allocated to a process.when allocated to a process. A part big enough to fulfill the request is associated A part big enough to fulfill the request is associated

with an AMCB and provided to the process.with an AMCB and provided to the process. The remainder is returned to the system associated The remainder is returned to the system associated

with an FMCB.with an FMCB.

An important issues involves how to divide An important issues involves how to divide blocks effectively so that the remainder is useful blocks effectively so that the remainder is useful to the system in fulfilling future requests?to the system in fulfilling future requests?

Page 59: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

If the remainder block is If the remainder block is VERYVERY small then small then it will be “it will be “uselessuseless” as an independent free ” as an independent free block.block.

To solve this situation, the memory To solve this situation, the memory manager may choose NOT to divide manager may choose NOT to divide blocks if the remainder is less than some blocks if the remainder is less than some pre-selected minimum.pre-selected minimum.

When a “free” is performed the entire When a “free” is performed the entire block rejoins the free list intact!block rejoins the free list intact!

This strategy works well with a This strategy works well with a best-fit best-fit allocation methodallocation method

Page 60: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

The Buddy SystemThe Buddy System

1st FMCB, at address 1000

120K blockA

85K blockB

60K blockAremnants

85K blockB

1st FMCB, at address 1000

A request for 60 k would be filled from block A

1st AMCB, at address 1000

60K from original block A

An alternative strategy is the “buddy system”An alternative strategy is the “buddy system”

In this strategy a block must be used intact or divided In this strategy a block must be used intact or divided exactlyexactly in in halfhalfIf a block is divided the 2 halves are considered “buddies.If a block is divided the 2 halves are considered “buddies.A freed block can ONLY be combined with its buddy to form a larger A freed block can ONLY be combined with its buddy to form a larger block.block.

Page 61: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

The The boundary tagboundary tag method (explained method (explained earlier) also simplifies determining if earlier) also simplifies determining if adjacent blocks are free.adjacent blocks are free.

Also if FCMBs are maintained as a linked Also if FCMBs are maintained as a linked list it is also simple to “unlink” blocks, list it is also simple to “unlink” blocks, reform them into a single block, and then reform them into a single block, and then “relink” the new block back into the list!“relink” the new block back into the list!

Page 62: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Relocation: Relocation: Involves Preparing a program to run in the location into which it is Involves Preparing a program to run in the location into which it is loaded, there is a difference between a programs logical address loaded, there is a difference between a programs logical address

space and it’s physical address spacespace and it’s physical address space

Program1

Program 1

Program 1

3000

5000

Page 63: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

A computer’s memory is addressed by numbering the A computer’s memory is addressed by numbering the bytes sequentially, from 0 to the total number of bytes in bytes sequentially, from 0 to the total number of bytes in memory. Hardware uses these absolute – or physical memory. Hardware uses these absolute – or physical addresses to fetch and store the contents of individual addresses to fetch and store the contents of individual bytes.bytes.

Addresses in source programs are generally symbolic, Addresses in source programs are generally symbolic, logical, such as the name of a variable. logical, such as the name of a variable.

A compiler will typically A compiler will typically bindbind these symbolic addresses these symbolic addresses to relocatable addresses, such as 14 bytes from the to relocatable addresses, such as 14 bytes from the beginning of this module.beginning of this module.

The linkage editor or loader will in turn bind these The linkage editor or loader will in turn bind these relocatable addresses to absolute addresses within the relocatable addresses to absolute addresses within the memory space of the process once the program is memory space of the process once the program is loaded.loaded.

Page 64: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

The binding to absolute addresses can The binding to absolute addresses can occur at three times:occur at three times:

Compile timeCompile time Load timeLoad time ExecutionExecution

Page 65: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Compile time: Compile time: If it is know at compile time where the If it is know at compile time where the process will reside, then “process will reside, then “Absolute code”Absolute code” can be can be generated. Though the use of absolute addresses is generated. Though the use of absolute addresses is not convenient for software. Because the program not convenient for software. Because the program must be loaded into exactly the same place in memory must be loaded into exactly the same place in memory every time it runs. every time it runs.

Load time:Load time: If it is not know, at compile time, where If it is not know, at compile time, where the program will be loaded, then the compiler must the program will be loaded, then the compiler must generate relocatable code. Final binding is delayed generate relocatable code. Final binding is delayed until the program is loaded and the starting address of until the program is loaded and the starting address of the program is known. the program is known.

Execution time:Execution time: If the process can be moved from If the process can be moved from one memory segment to another, during its execution one memory segment to another, during its execution then binding must be delayed until run time. then binding must be delayed until run time.

Page 66: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

If relocation is performed when the program is If relocation is performed when the program is loaded or before it is called loaded or before it is called STATIC STATIC RELOCATIONRELOCATION

If relocation is performed after the program has If relocation is performed after the program has been loaded and execution has begun, when we been loaded and execution has begun, when we have have dynamic relocationdynamic relocation

Relocation can be performed either by Relocation can be performed either by hardwarehardware or or softwaresoftware means means

Page 67: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Static Relocation: via softwareStatic Relocation: via software

Figure 7-12: Program Relocation by a Relocating Loader

Page 68: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Relocation Via SoftwareRelocation Via Software Object file is converted to an executable file by Object file is converted to an executable file by

either a “either a “relocating linker” relocating linker” or processed when or processed when loaded by a “loaded by a “relocating loader”relocating loader”

Requires recording what memory references Requires recording what memory references must be changed and how with in the symbolic must be changed and how with in the symbolic code. code.

Object file may include a “Object file may include a “relocation relocation dictionary” dictionary” which identifies all instructions and which identifies all instructions and addresses that may need to be modified.addresses that may need to be modified.

Page 69: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

The relocating software uses the dictionary information The relocating software uses the dictionary information to decide which memory references to adjust and how to decide which memory references to adjust and how when the actual memory location is known. Normally the when the actual memory location is known. Normally the address are specified from some general location such address are specified from some general location such as 0.as 0.

A drawback is that it requires that additional information A drawback is that it requires that additional information be maintained in object files.be maintained in object files.

Relocation Information isn’t loaded into memory, so no Relocation Information isn’t loaded into memory, so no further relocation is possible. The program can not be further relocation is possible. The program can not be swapped.swapped.

This strategy provides only This strategy provides only staticstatic not not dynamicdynamic relocation! relocation!

Page 70: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamic Relocation: Dynamic Relocation: Based AddressingBased Addressing

4406LOAD

ADDI 25

JMP 4150

DISPLACEMENT

3000BASE REGISTER

4406 + 3000 = 7406

EFFECTIVE ADDRESS

Page 71: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Dynamic Relocation:Dynamic Relocation:“Based Addressing”“Based Addressing”

A program routine is written as though its first byte is at some base A program routine is written as though its first byte is at some base location, usually address 0, and every other location expressed as location, usually address 0, and every other location expressed as an offset or displacement from the starting point.an offset or displacement from the starting point.

A “base register” then contains the actual first (or base) address for A “base register” then contains the actual first (or base) address for the programthe program

Relocation is achieved by loading this base register with the actual Relocation is achieved by loading this base register with the actual starting address of the program.starting address of the program.

Address references in machine instructions are adjusted to create Address references in machine instructions are adjusted to create “effective” addresses by adding the contents of the specified base “effective” addresses by adding the contents of the specified base register to the value in the address field (displacement).register to the value in the address field (displacement).

A program can be relocated by changing this base register to point A program can be relocated by changing this base register to point to a new location, however to work instructions had to reference the to a new location, however to work instructions had to reference the correct base register.correct base register.

Page 72: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Relocation Registers!Relocation Registers!

An improvement on the base register concept An improvement on the base register concept is the introduction of a relocation register.is the introduction of a relocation register.

Page 73: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and
Page 74: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Involves a “relocation register” that is Involves a “relocation register” that is always added to every instruction address always added to every instruction address and data address and data address beforebefore the instruction or the instruction or data is fetched from memory.data is fetched from memory.

Used uniformly for all memory references Used uniformly for all memory references and isn’t specified with each instruction.and isn’t specified with each instruction.

Simplifies relocationSimplifies relocation The relocation register contents must be The relocation register contents must be

saved and restored via the PCB during a saved and restored via the PCB during a context switchcontext switch

Page 75: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

ProtectionProtection

The sub-problem of protection, involves The sub-problem of protection, involves allowing a process to accesses only that allowing a process to accesses only that memory which has been allocated to it.memory which has been allocated to it.

There are several possible strategies, but all There are several possible strategies, but all rely upon hardware assistance.rely upon hardware assistance.

Page 76: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Fences: simple single user OSFences: simple single user OS

Page 77: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

One of the simplest protection mechanism is the One of the simplest protection mechanism is the idea of a Fence address or Fence register. This idea of a Fence address or Fence register. This works in simple singer user computing works in simple singer user computing environments where memory is divided between environments where memory is divided between the OS and a single process.the OS and a single process.

The fence register specifies a fence address. A The fence register specifies a fence address. A memory reference above this fence address will memory reference above this fence address will result in a trap or memory error occurring.result in a trap or memory error occurring.

It is difficult to choose this value correctly, If it is It is difficult to choose this value correctly, If it is too small, programs will not have enough too small, programs will not have enough memory to run, if it is too large, then there may memory to run, if it is too large, then there may be unused space.be unused space.

Page 78: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Bounds RegistersBounds Registers

Page 79: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Bounds registers works for OS’s that have Bounds registers works for OS’s that have parts of the OS in both high and low parts of the OS in both high and low memory. memory.

It is a more general protection algorithm, It is a more general protection algorithm, and specifies an upper and lower bound and specifies an upper and lower bound on the memory accessible to an on the memory accessible to an application program.application program.

Page 80: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Base and Limit RegistersBase and Limit Registers

Page 81: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

This uses a limit register in conjunction with a This uses a limit register in conjunction with a base register to limit a process’s access.base register to limit a process’s access.

The base register contains a beginning address The base register contains a beginning address for the process in memory.for the process in memory.

The limit register specifies how many bytes of The limit register specifies how many bytes of memory the process has available to it. Added memory the process has available to it. Added to the contents of the base register creates an to the contents of the base register creates an upper limit.upper limit.

An address reference in the code, is calculated An address reference in the code, is calculated using base addressing. And is added to the using base addressing. And is added to the contents of the base register. If it exceeds the contents of the base register. If it exceeds the “limit” specified in the limit register, then the “limit” specified in the limit register, then the reference is invalid.reference is invalid.

Page 82: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Protection KeysProtection Keys

Page 83: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Associated with the IBM S/360 Architecture.Associated with the IBM S/360 Architecture. Each partition in memory is assigned a unique Each partition in memory is assigned a unique

protection key value between 0 and 15.protection key value between 0 and 15. As a process is loaded and assigned memory, it As a process is loaded and assigned memory, it

is assigned a protection key, that matches the is assigned a protection key, that matches the set of blocks assigned to it. set of blocks assigned to it.

The protection key of the currently executing The protection key of the currently executing process is loaded into a special register. process is loaded into a special register.

An access to a memory block, results in the An access to a memory block, results in the current protection key being compared to the current protection key being compared to the protection key of the block. If they match the protection key of the block. If they match the access is allowed, other wise it is an erroraccess is allowed, other wise it is an error

Limits memory to 15 protected areas.Limits memory to 15 protected areas.

Page 84: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

SwappingSwapping Swapping involves moving portions of Swapping involves moving portions of

programs and their data from main programs and their data from main memory to secondary storage before they memory to secondary storage before they complete execution.complete execution.

Processes which are swapped to disk are Processes which are swapped to disk are expected to be restored at some point so expected to be restored at some point so that they can finish executionthat they can finish execution

Page 85: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Program1

Program 1

3000

Swapping

Page 86: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

We can remove a process that has been We can remove a process that has been suspended by the medium term scheduler suspended by the medium term scheduler allowing another process to executeallowing another process to execute

It can be used to increase free memory if an It can be used to increase free memory if an allocation has been blocked.allocation has been blocked.

We can temporarily remove a process that has We can temporarily remove a process that has been blocked awaiting a long term event, such been blocked awaiting a long term event, such as waiting for resources.as waiting for resources.

We can use swapping to prepare a process for We can use swapping to prepare a process for relocation to a new location, such as during relocation to a new location, such as during compaction.compaction.

Page 87: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

CompactionCompaction

Page 88: Memory Management. Memory Management is the managing of the pool of available memory in a computer system, allocating space to application programs and

Additional material obtained from: Additional material obtained from: Operating Systems, a Systematic View by Davis & RajkumarOperating Systems, a Systematic View by Davis & Rajkumar Operating System Concepts, 6Operating System Concepts, 6thth edition, Silberschatz, Galvin, Gagne edition, Silberschatz, Galvin, Gagne