51
Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

  • View
    301

  • Download
    17

Embed Size (px)

Citation preview

Page 1: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 1Copyright ©2005

Memory Management

Chapter 5

Page 2: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 2Copyright ©2005

Memory binding

• Each entity has a set of attributes, e.g. a variable has type, size, dimensionality.

• Binding is the action of specifying values of attributes of an entity.

• Two types of binding are used in practice:– Early binding: Restrictive, but leads to efficient execution– Late binding: Flexible, but may lead to less efficient execution

Page 3: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 3Copyright ©2005

Memory binding

• Memory allocation is a binding of the `memory address’ attribute of a data entity

• Static and dynamic binding are examples of early and late binding, respectively

Page 4: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 4Copyright ©2005

Features of static and dynamic memory allocation

Page 5: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 5Copyright ©2005

Memory allocation preliminaries

• Stack – LIFO allocation

– A `contiguous’ data structure

– Used for data allocated `automatically’ on entering a block

• Heap – Non-contiguous data structure

– Pointer-based access to allocated data

– Used for `program controlled data’ (PCD data) that is explicitly allocated and de-allocated in a program, e.g. malloc/calloc

Page 6: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 6Copyright ©2005

A heap before and after freeing an allocation area

Page 7: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 7Copyright ©2005

Memory allocation model for a process

Page 8: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 8Copyright ©2005

Linking, loading and execution of programs

Page 9: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 9Copyright ©2005

Memory binding in programs

• The origin of a program is the address of its first instruction or data byte

• A compiler is given an origin specification

• It binds instructions and data of a program in accordance with its origin specification

Page 10: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 10Copyright ©2005

Assembly program P and its generated code

Page 11: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 11Copyright ©2005

Linking and relocation of programs

• Linking– A program may wish to use library functions and other programs

– These library functions and other programs should become a part of the program

– Linking is the function that performs this action

• Relocation– Many program may have the same origin specification, so the address

binding of some of them has to be changed

– A program may have to be executed from a memory area that is different from the area for which it is compiled

Page 12: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 12Copyright ©2005

Linking and relocation of programs

• Linking and relocation could be performed– Statically

– Dynamically

• What are the advantages of dynamic linking or relocation?

• How is dynamic linking or relocation performed?

Page 13: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 13Copyright ©2005

Program relocation using the relocation register: (a) program, (b) its view during execution

Page 14: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 14Copyright ©2005

Memory protection using bound registers

Page 15: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 15Copyright ©2005

Memory protection using memory protection leys

Page 16: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 16Copyright ©2005

Kernel actions for memory protection

Page 17: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 17Copyright ©2005

Free area management in a heap:(a) singly linked free list, (b) doubly linked free list

Page 18: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 18Copyright ©2005

Heap management: (a) free list, (b)-(d) allocation using first, best and next fit

Page 19: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 19Copyright ©2005

Memory fragmentation

• Fragmentation is the development of un-usably small areas in memory

• It has several consequences– Memory is wasted

– The OS may run out of space to be allocated

• Two forms of memory fragmentation– Internal fragmentation

– External fragmentation

Page 20: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 20Copyright ©2005

Forms of memory fragmentation

Page 21: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 21Copyright ©2005

How to counter external fragmentation?

• Do not allow free memory areas to become too small– Best fit leads to successively smaller memory areas!

• Combine adjoining free areas into a single larger memory area

• Perform compaction

Page 22: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 22Copyright ©2005

Boundary tags

Page 23: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 23Copyright ©2005

Merging using boundary tags (a) Free list, (b) -(d) freeing of areas X, Y or Z

Page 24: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 24Copyright ©2005

Memory compaction

Page 25: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 25Copyright ©2005

Buddy system

• When memory is to be allocated– Allocate the entire free area to satisfy a request, or

– Split a free area into two free areas of equal size* These areas are called buddies

* One of the buddies is considered for satisfying a memory request (either completely, or through successive splitting)

• When memory is to be freed– It is merged with its buddy, if the buddy is also free

• Status of blocks is saved in a status map

Page 26: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 26Copyright ©2005

A buddy system

Page 27: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 27Copyright ©2005

Buddy system operation when a block is released

Page 28: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 28Copyright ©2005

Powers of two allocators

• Allocation is in terms of blocks whose sizes are different powers of 2

• Blocks are not split or merged

• Free lists are maintained for different block sizes

• The header element contains information about block status and size. It is stored in the block itself

• Header element occupies memory, hence memory efficiency is low for requests that are powers of two in size

Page 29: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 29Copyright ©2005

Header element in the powers-of-two allocator

Page 30: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 30Copyright ©2005

Approaches to memory allocation

• Contiguous memory allocation– Allocates a single contiguous memory area to a request– Suffers from fragmentation– Requires provision to counter fragmentation

• Noncontiguous memory allocation– Allocates a set of disjoint memory areas to a request– Overcomes certain forms of fragmentation (internal ? External?)– Requires address translation during execution of programs

Page 31: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 31Copyright ©2005

Contiguous memory allocation

• Fixed partitioned memory allocation– Partitions are fixed once and for all

– A process is allocated a memory partition that is larger in size than its maximum requirement

• Variable partitioned memory allocation– A process is allocated only as much memory as it needs

– Memory is reused through first-fit, best-fit, etc.

Page 32: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 32Copyright ©2005

Internal fragmentation in fixed partitioned allocation

Page 33: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 33Copyright ©2005

Memory compaction in variable partitioned allocation

Page 34: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 34Copyright ©2005

Noncontiguous memory allocation

• Several disjoint memory areas may be allocated in response to a request. However, hardware aids in the execution of programs.

– User or a process is not aware of noncontiguity– Hence two views of a process exist

* Logical view: View by the user or process itself. * Physical view: Actual situation regarding allocation

– The organization of components in the logical view is called logical organization, and addresses used in it are called logical addresses

– Addresses used in the physical view are called physical addresses

• Avoids external fragmentation as no memory area is too small to be allocated

Page 35: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 35Copyright ©2005

Noncontiguous memory allocation

• How is noncontiguous memory allocation performed?– A process is considered to consist of a set of components– Each component is allocated a contiguous area of memory that can

accommodate it– Each logical address in an instruction is considered to consist of a pair

(component #, byte #)

• During operation of the process, a hardware unit called the memory management unit (MMU) converts a (component #, byte #) pair into an absolute memory address

Page 36: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 36Copyright ©2005

Noncontiguous memory allocation

Page 37: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 37Copyright ©2005

Logical and physical views of a process in a noncontiguous memory allocation

Page 38: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 38Copyright ©2005

Schematic of address translation in non-contiguous memory allocation

Page 39: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 39Copyright ©2005

Approaches to Noncontiguous memory allocation

• Paging– A process consists of components of a fixed size, called pages

– The page size is a power of 2 and is fixed in the architecture

– Memory is divided into parts called page frames, whose size matches the size of a page

– The kernel allocates memory to all pages of a process

– The kernel builds a page table that stores information about addresses of page frames allocated to processes

– The MMU uses information in the page table to perform address translation

Page 40: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 40Copyright ©2005

Paging

• A logical address is split into a pair (page #, word #)

• This splitting is performed using bit-splitting since the page size is a power of 2

Page 41: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 41Copyright ©2005

Processes in paging

Page 42: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 42Copyright ©2005

Approaches to noncontiguous memory allocation

• Segmentation– Each component in a process is a logical unit called a segment,

e.g., a function, a module or an object

– Components have different sizes

– The kernel allocates memory to all components and builds a segment table

– Each logical address in a segment is a pair of the form (segment #, byte #)

– The MMU uses the segment table to perform address translation

Page 43: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 43Copyright ©2005

A process Q in segmentation

Page 44: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 44Copyright ©2005

Comparison of continuous and noncontinuous memory allocation

Page 45: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 45Copyright ©2005

Kernel memory allocation

• The kernel creates and destroys data structures used to store information about user processes and resources at a very high rate, e.g. PCBs, ECBs.

• Hence efficiency of kernel memory allocation directly influences its overhead

• Kernel uses special techniques to perform memory allocation for its data structures

• These techniques exploit the fact that the sizes of many of these data structures are known in advance, e.g. PCBs, ECBs.

Page 46: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 46Copyright ©2005

Slab allocator

• A slab is a fixed sized area of memory

• A slab contains data structures of the same kind

• A slab is preformatted to contain standard sized slots for these data structures

• A free list indicates which slots are free

• If all slabs containing data structures of a specific kind are full, new slabs are allocated by the kernel

• Allocation and de-allocation of memory is very fast

Page 47: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 47Copyright ©2005

Format of a slab

Page 48: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 48Copyright ©2005

Program forms employed in operating systems

Page 49: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 49Copyright ©2005

(a) a program, and (b) its equivalent overlay structured program in execution

Page 50: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 50Copyright ©2005

Sharing of a program (a) static sharing, (b) dynamic sharing

Page 51: Chapter 5: Memory Management Dhamdhere: Operating Systems— A Concept-Based Approach Slide No: 1 Copyright ©2005 Memory Management Chapter 5

Chapter 5: Memory Management

Dhamdhere: Operating Systems—A Concept-Based Approach

Slide No: 51Copyright ©2005

Reentrant program (a) structure of program C, (b)-(c) invocation by A and B.