Tải bản đầy đủ (.pdf) (54 trang)

Lecture Operating systems: A concept-based approach (2/e): Chapter 5 - Dhananjay M. Dhamdhere

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (764.21 KB, 54 trang )

PROPRIETARY MATERIAL. ©  2007 The McGraw­Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide  may be displayed, reproduced or distributed 
in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw­Hill 
for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission. 

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 1
Copyright © 2008


Managing the memory hierarchy



The memory hierarchy is comprised of several memory
units with different speeds and cost
– Efficient operation of a process and the system depends on
effective use of the memory hierarchy
* Efficient operation of a process depends on hit ratios in faster
memories of the hierarchy, i.e., the cache and memory
* Efficient operation of the system requires many processes to be
present in memory

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—


A Concept­Based Approach, 2ed 

Slide No: 2
Copyright © 2008


Managing the memory hierarchy



How different levels in the hierarchy are managed
– L1 cache and L2 cache
* Allocation and use is managed by hardware to ensure high hit ratios

– Memory
* Use is managed by run time libraries of programming languages
* Allocation is managed by the kernel. It must
 Accommodate many processes in memory
 Ensure high hit ratios

– Disk
* Allocation and use is managed by the kernel
 Quick loading and storing of process address spaces is
important
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 


Slide No: 3
Copyright © 2008


Managing the memory hierarchy



Efficient use of memory
– Involves sharing of memory among processes and reuse of
memory previously allocated to other processes
– Requires speedy allocation and de-allocation within the memory
allocated to a process
– We discuss these aspects in Chapter 5 and this set of slides



The memory hierarchy consisting of memory and a disk
is called virtual memory
– We discuss it separately in Chapter 6

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 4
Copyright © 2008



Memory binding



Each entity has a set of attributes; e.g., a variable has
type, size and dimensionality
– Binding is the action of specifying values of attributes of an
entity, e.g.
* Declaration of type of a variable is the binding of its type attribute
* Memory allocation is the binding its memory address attribute

– Two types of binding are used in practice:
* Early binding (static binding ─ binding before operation begins)
 Restrictive, but leads to efficient operation of a process
* Late binding (dynamic binding ─ binding during operation)
 Flexible, but may lead to less efficient operation of a process

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 5
Copyright © 2008


Features of static and dynamic memory allocation




Static memory allocation
– Allocation is performed before a process starts operating
– Size of memory required should be known a priori; otherwise,
* Wastage may occur if size is overestimated
* A process may have to be terminated if it requires more memory
than allocated to it

– No allocation or de-allocation actions required during operation



Dynamic memory allocation
– Allocation is performed during operation of a process
– Allocation equals size; hence no wastage of memory
– Allocation / de-allocation overhead is incurred during operation

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 6
Copyright © 2008


Memory allocation preliminaries




Stack
– LIFO allocation
– A ‘contiguous’ data structure—data occupy adjoining locations
– Used for data allocated ‘automatically’ on entering a function, such as
parameters of a function call and local variables of a function



Heap
– Non-contiguous data structure—data may not occupy adjoining
locations
* 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
– ‘holes’, i.e., unused areas, may develop in memory during operation
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 7
Copyright © 2008


A hole develops in a heap due to de-allocation


(a) Three variables are allocated in the heap

(b) De­allocation of the memory of floatptr2 creates a hole in memory

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 8
Copyright © 2008


Memory allocation for a process



Memory requirements of the components of a process
– Sizes of code and static data of the program to be executed are
known a priori
* Hence code and static data can be allocated statically

– Sizes of stack and program controlled data (PCD) data vary
during operation
* Hence stack and PCD data require dynamic allocation

– The memory allocation model for a process incorporates both
these requirements


Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 9
Copyright © 2008


Memory allocation model for a process

•  The code and static data are given a fixed allocation
•  Stack and PCD data can grow in opposite directions
•  Their sizes can vary independently; problem arises only if they overlap
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 10
Copyright © 2008


Memory allocation for a process



A process is created when a program is to be executed.

Two issues concerning program loading and execution
are:
– If the start address of the program is different from the start
address of the allocated memory area
* Addresses in instructions should be changed so that they would
access the intended operands
* This requirement is called relocation of a program

– Programs should not interfere with each other’s execution
* This requirement is called memory protection

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 11
Copyright © 2008


Relocation register



A computer provides a relocation register in the CPU to
assist in relocation of a program
– When the current instruction wishes to access memory, the CPU
* takes the memory address from the instruction, say address bbb
* adds contents of the relocation register, say aaa, to it

* accesses the byte with the resulting address bbb + aaa

Q: How to execute a program with the start address 50,000 in the
memory area starting on address 70,000?
A: Load 20,000 in the relocation register (see next slide)

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 12
Copyright © 2008


Program relocation using the relocation register

(a) Program has been coded to have the start address 50,000
(b) Execution of the program in memory area with start address 70,000: 
       • Relocation register contains 20,000
       • Operand that had address 65,784 in program exists in memory 85,784
       ∙ 20,000 is added to the address in the Add instruction during execution
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 13

Copyright © 2008


Memory protection using bound registers

• CPU has two registers: Lower bound register (LBR), and Upper bound 
  register (UBR) containing start and end addresses of memory for a program
• An interrupt is raised if the program accesses an address outside the 
  range defined by contents of LBR and UBR
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 14
Copyright © 2008


Kernel actions for memory protection



The kernel performs the following actions:
– While allocating memory to a process
* Write the start and end addresses of the memory area allocated to
the program in the PCB of the process

– While dispatching a process
* Access the PCB and load the start and end addresses of the

memory area in the LBR and UBR registers

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 15
Copyright © 2008


Reuse of memory



The kernel keeps track of free memory areas in a heap
and reuses them while making fresh memory allocations
– It maintains a free list to store information about free memory areas
– A memory request is satisfied by using a part of a free memory area
* The remaining memory area is put back into the free list

– Three techniques for reuse of free memory
* First fit technique:
 Use the first free area in the list that can satisfy the request
* Best fit technique:
 Use the free area that leaves smallest remaining free area
* Next fit technique:
 Use the next free area in the list that satisfies the request


Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 16
Copyright © 2008


Free area management in a heap

(a) Singly linked free list
(b) Doubly linked free list
       ∙  Permits fast insertions and deletions
       ∙  Consider allocation of memory area y in best fit allocation
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 17
Copyright © 2008


Heap management

(a) Free list
       (b)–(d) Allocations using first, best and next fit allocation

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 18
Copyright © 2008


Memory fragmentation



Fragmentation is the existence of unusable free areas in
memory
– For example, fragmentation may occur because free memory
areas are too small to be used
– Fragmentation has several consequences
* Memory is wasted
* The kernel may run out of memory to be allocated

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 19
Copyright © 2008



Memory fragmentation



Two forms of fragmentation
– External fragmentation
* A memory area is too small to be allocated to any process

– Internal fragmentation
* Some part of an allocated memory area is not used; however, the
kernel cannot allocate it to another process
 It arises when the kernel allocates a larger memory area than
requested by a process

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 20
Copyright © 2008


How to counter external fragmentation?




Do not allow free memory areas to become too small
– Best fit leads to successively smaller memory areas!
– Perform merging by combining adjoining free areas into a single
larger memory area
* Boundary tags are used to assist in merging

– Perform compaction of allocated memory so that a single free
memory area exists

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 21
Copyright © 2008


Boundary tags

•  A tag is a descriptor of a memory area
•  Tags are stored at both boundaries of a memory area
    –  Hence tags of adjoining memory areas are in adjoining memory locations
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 


Slide No: 22
Copyright © 2008


Merging using boundary tags

(a) Singly linked free list
(b)–(d) Freeing of areas X, Y or Z
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 

Slide No: 23
Copyright © 2008


Memory compaction

* Free areas are hatched in the figure

•  All occupied free areas are moved towards one end of memory
•  All free areas move to the other end of memory
•  Compaction involves relocation of programs that are moved

Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—

A Concept­Based Approach, 2ed 

Slide No: 24
Copyright © 2008


Buddy system allocator



The allocator works as follows:
– Status of memory blocks is recorded in a status map
– When memory is to be allocated
* If an appropriate free block exists, allocate it.
* Otherwise, split a larger free block into two (or more) free areas
 These blocks are buddies of one another
 One of the buddies is used for satisfying a memory request
(either directly or through successive splitting)
 The other buddy block remains free

– When a memory block is to be freed
* If its buddy block is already free, the two are merged to form a
single free block. The resulting free block is similarly merged

– Binary buddy: A block is split into two blocks of same size
Chapter 5: 
Memory Management

Dhamdhere: Operating Systems—
A Concept­Based Approach, 2ed 


Slide No: 25
Copyright © 2008


×