Top Banner
Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall
66

Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Mar 29, 2015

Download

Documents

Jordon Baisden
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Chapter 7Memory Management

Operating Systems:Internals and Design Principles, 6/E

William Stallings

Patricia RoyManatee Community College, Venice, FL

©2008, Prentice Hall

Page 2: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Given Credit Where It is Due

• Some of the lecture notes are borrowed from Dr. 柯皓仁 at National Chiao Tung

University, Taiwan • One slide is borrowed from Dr. Hugh C.

Lauer at Worcester Polytechnic Institute• I have modified them and added new

slides

Page 3: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management

• Subdividing memory to accommodate multiple processes

• Memory needs to be allocated to ensure a reasonable supply of ready processes to consume available processor time

Page 4: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Relocation• Protection• Sharing• Logical organization• Physical organization

Page 5: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Relocation– Programmer does not know where the

program will be placed in memory when it is executed

– While the program is executing, it may be swapped to disk and returned to main memory at a different location (relocated)

– Memory references must be translated in the code to actual physical memory address

Page 6: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Addressing Requirement

Page 7: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Background

• Program must be brought into memory and placed within a process for it to be executed

• Input (job) queue – collection of processes on the disk that are waiting to be brought into memory for execution

• User programs go through several steps before being executed

7

Page 8: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Steps for Loading a Process in Memory

• The linker combines object modules into a single executable binary file (load module)

• The loader places the load module in physical memory

8

System Library

System Library

static linking

dynamic linking

Linking: combine all the objectmodules of a program into a binary program image

Linking: combine all the objectmodules of a program into a binary program image

Page 9: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

9

The Linking Function

Module A

CALL B

Return

Length L

Module B

CALL C

Return

Length M

Module C

Return

Length N

0

L-1

Module A

JSR “L”

Return

Module B

JSR “L+M”

Return

Module C

Return

L

L+M-1

L+M

L+M+N-1

Object Modules

Load Module

0

L-1

0

M-1

0

N-1

Page 10: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Address Binding – Mapping from one address space to another

• Address representation– Source program: symbolic (such as count)– After compiling: re-locatable address

• 14 bytes from the beginning of this module– After linkage editor, loader or run-time referring: absolute address

• Physical memory address 2014

10

0

250

2000

2250

Re-locatable Address Absolute Address (Physical Memory)

Symbolic Address

int I;goto p1;

p1

Page 11: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Address Binding (Cont.)

• Address binding of instructions and data to physical memory addresses can happen at three different stages

– Compile time: If memory location known a priori, absolute code can be generated• Must recompile code if starting location changes• MS-DOS .COM-format program

11

Page 12: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

12

Binding at Compile Time

SymbolicAddresses

PROGRAM

JUMP i

LOAD j

DATA

i

j

Source Code

Absolute Addresses(Physical Memory Addresses)

1024

JUMP 1424

LOAD 2224

1424

2224

Absolute Load Module

Compile

The CPU generates the absolute addressesThe CPU generates the absolute addresses

Link

Page 13: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

13

Binding at Compile Time (Cont.)

Absolute Addresses(Physical Memory Addresses)

1024

JUMP 1424

LOAD 2224

1424

2224

Process Image (Part)

Load

Page 14: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Address Binding (Cont.)

• Address binding of instructions and data to physical memory addresses can happen at three different stages

– Load time: Must generate re-locatable code if memory location is not known at compile time• Physical memory address is fixed at load time• Must reload if starting location changes

14

Page 15: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

15

Binding at Loader Time

Relative (Relocatable)Addresses

0

JUMP 400

LOAD 1200

400

1200

Relative Load Module

SymbolicAddresses

PROGRAM

JUMP i

LOAD j

DATA

i

j

Source Code

Compile Link

Page 16: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

16

Binding at Loader Time (Cont.)

Absolute Addresses(Physical Memory Addresses)

1024

JUMP 1424

LOAD 2224

1424

2224

Process Image (Part)

Load

The CPU generates the absolute addressesThe CPU generates the absolute addresses

Page 17: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

17

Address Binding (Cont.)

– Execution time: Binding delayed until run time

• The process can be moved during its execution from one memory segment to another

• The CPU generates the relative (virtual) addresses

• Need hardware support for address maps (e.g., base and limit registers)

• Most general-purpose OS use this method

– Swapping, Paging, Segmentation

Relative (Relocatable)Addresses

0

JUMP 400

LOAD 1200

400

1200

MAX =2000

Page 18: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

18

Dynamic Relocation Using a Relocation Register

Generated By CPU

Seen ByMemory Unit

Map LA to PA

Binding at execution time (when reference is made)

Binding at execution time (when reference is made)

0 to MAX

14000 to 14000+MAX

Page 19: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Logical vs. Physical Address Space

• The concept of binding a logical address space to a physical address space is central to proper memory management– Logical address – generated by the CPU; also referred to as virtual

address– Physical address – address seen by the memory unit

• Logical and physical addresses are the same in compile-time and load-time address-binding schemes

• Logical and physical addresses differ in execution-time address-binding scheme

19

Page 20: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory-Management Unit (MMU)

• Hardware device that maps virtual to physical address• In MMU scheme, the value in the relocation register is

added to every address generated by a user process (CPU) at the time it is sent to memory

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

20

Page 21: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Static Linking and LoadingPrintf.c

Printf.o

StaticLibrary

gcc

ar

Linker

Memory

HelloWorld.c

gcc

HelloWorld.o

Loader

a.out(or name of

your command)

Page 22: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Linking

• The linking of some external modules is done after the creation of the load module (executable file)– Windows: external modules are .DLL files– Unix: external modules are .SO files (shared library)

• The load module contains references to external modules which are resolved either at:– Loading time (load-time dynamic linking)– Run time: when a call is made to a procedure defined in the external

module (run-time dynamic linking)

• OS finds the external module and links it to the load module– Check to see if the external module has been loaded into memory

22

Page 23: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Advantages of Dynamic Linking

• The external module is often an OS utility. Executable files can use another version of the external module without the need of being modified

• Code sharing: the same external module needs to be loaded in main memory only once. Each process is linked to the same external module– Saves memory and disk space

23

Page 24: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Techniques

• No OS Support: User-Managed Overlays• Fixed Partitioning• Dynamic Partitioning• Simple Paging• Simple Segmentation• Virtual Memory Paging• Virtual Memory Segmentation

Page 25: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Overlays• Keep in memory only those instructions

and data that are needed at any given time

• Needed when process is larger than amount of memory allocated to it

• Implemented by user, no special support needed from operating system, programming design of overlay structure is complex

25

Page 26: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

26

Overlay (Cont.)

Pass 1 70KPass 2 80KSym. Table 20KCommon Rou. 30K

Pass 1 70KPass 2 80KSym. Table 20KCommon Rou. 30K

Assembler

Total Memory Available = 150K

Page 27: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning

• Equal-size partitions– Any process whose size is less than or equal

to the partition size can be loaded into an available partition

– If all partitions are full, the operating system can swap a process out of a partition

– Equal-size partitions was used in early IBM’s OS/MFT (Multiprogramming with a Fixed number of Tasks)

Page 28: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning

• Equal-size partitions– A program may not fit in a partition. The

programmer must design the program with overlays

– Main memory use is inefficient. Any program, no matter how small, occupies an entire partition

• This is called internal fragmentation

Page 29: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning

Page 30: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Placement Algorithm

• Equal-size– Placement it trivial

• Unequal-size– Can assign each process to the smallest

partition within which it will fit– Queue for each partition– Processes are assigned in such a way as to

minimize wasted memory within a partition

Page 31: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning

Page 32: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning

Page 33: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Fixed Partitioning• Like equal-size partitions, unequal-size

partitions still suffer the following problems: – A program may not fit in a partition. The

programmer must design the program with overlays

– Main memory use is inefficient. Any program, no matter how small, occupies an entire partition

• This is called internal fragmentation

Page 34: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

• Partitions are of variable length and number

• Process is allocated exactly as much memory as required

Page 35: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

Page 36: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

Page 37: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

• Partitions are of variable length and number

• Process is allocated exactly as much memory as required

• Eventually get holes in the memory. This is called external fragmentation

Page 38: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

Must use compaction to shift processes so they are contiguous and all free memory is in one block

Page 39: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

• Operating system must decide which free block to allocate to a process

• Best-fit algorithm– Chooses the block that is closest in size to the

request– Worst performer overall– Since the smallest block is found for process,

the fragmentation of the smallest size is left– Memory compaction must be done more often

Page 40: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

• First-fit algorithm– Scans memory form the beginning and

chooses the first available block that is large enough

– Fastest– May have many processes loaded in the front

end of memory that must be searched over when trying to find a free block

Page 41: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Partitioning

• Next-fit– Scans memory from the location of the last

placement– More often allocate a block of memory at the

end of memory where the largest block is found

– The largest block of memory is broken up into smaller blocks

– Compaction is required to obtain a large block at the end of memory

Page 42: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Allocation

Page 43: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Buddy System

• Entire space available is treated as a single block of 2U

• If a request of size s such that 2U-1 < s <= 2U, entire block is allocated– Otherwise block is split into two equal buddies– Process continues until the smallest block

greater than or equal to s is generated

Page 44: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Example of Buddy System

Page 45: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Relocation

• When program loaded into memory the actual (absolute) memory locations are determined

• A process may occupy different partitions which means different absolute memory locations during execution (from swapping)

Page 46: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Relocation

• Compaction will also cause a program to occupy a different partition which means different absolute memory locations

Page 47: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Relocation

• Base register • Bounds register

These values are set when the process is loaded or when the process is swapped in

Page 48: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Paging

• Partition memory into small equal fixed-size chunks and divide each process into the same size chunks

• The chunks of a process are called pages and chunks of memory are called frames

Page 49: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Paging

• Operating system maintains a page table for each process– Contains the frame location for each page in

the process– Memory address consists of a page number

and offset within the page

Page 50: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Process and Frames

Page 51: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Process and Frames

Page 52: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Page Table

Page 53: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Logical Addresses

Page 54: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Paging

Page 55: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Comparison

• What are the advantages of paging mechanism vs. fixed partitioning and dynamic partitioning mechanisms?

Page 56: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Segmentation

• All segments of all programs do not have to be of the same length

• There is a maximum segment length• Addressing consist of two parts - a

segment number and an offset• Since segments are not equal,

segmentation is similar to dynamic partitioning

Page 57: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Logical Addresses

Page 58: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Segmentation

Page 59: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Comparison

• What are the advantages of the segmentation mechanism vs. the dynamic partitioning mechanism?

Page 60: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

In-Class Exercise

• Prob. 7.12 Consider a simple paging system with the following parameters: 232 bytes of physical memory; page size of 210 bytes; 216 pages of logical address space.– A. how many bits are in a logical address?– B. how many bytes in a frame? – C. how many bits in the physical address specify the

frame?– D. how many entries in the page table? – E. how many bits in each page table entry? Assume

each page table entry contains a valid/invalid bit.

Page 61: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Appendix

Page 62: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Protection– Processes should not be able to reference

memory locations in another process without permission

– Impossible to check absolute addresses at compile time, must be checked at run time

Page 63: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Sharing– Allow several processes to access the same

portion of memory– For instance, if a number of processes are

executing the same program, it is better to allow each process access to the same copy of the program rather than have their own separate copy

Page 64: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Logical Organization– Programs are written in modules– Modules can be written and compiled

independently– Different degrees of protection given to

modules (read-only, execute-only)– Share modules among processes

Page 65: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Memory Management Requirements

• Physical Organization– Memory available for a program plus its data

may be insufficient– System responsibility: control the information

move between main and secondary memory

Page 66: Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College, Venice, FL.

Dynamic Loading

• Routine is not loaded until it is called• Better memory-space utilization

– Unused routine is never loaded.

• Useful when large amounts of code are needed to handle infrequently occurring cases– Like error routines

• No special support from the operating system is required implemented through program design– Responsibility of users to design dynamic-loading programs– OS may help the programmer by providing library routines

66