Вы находитесь на странице: 1из 35

Operating System

Concepts
Ku-Yaw Chang
canseco@mail.dyu.edu.tw
Assistant Professor, Department of
Computer Science and Information Engineering
Da-Yeh University

Chapter 9 Memory Management


Keep several processes in memory to increase
CPU utilization
Memory management algorithms

Require hardware support


Common strategies
Paging
Segmentation

Ku-Yaw Chang

Chapter 9 Memory Management

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

9.1 Background
Program must be brought (loaded) into memory and
placed within a process for it to be run.

Address binding
A mapping from one address space to another
A typical instruction-execution cycle

Fetch an instruction from memory


Decode the instruction
May cause operands to be fetched from memory

Execute the instruction


Store results back into memory

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.1 Address Binding


Input queue

A collection of processes on the disk that are waiting


to be brought into memory to run the program.

A user program will go through several steps


before being executed

Addresses in source program are symbolic


A compiler binds these symbolic addresses to
relocatable addresses
A loader binds these relocatable addresses to
absolute addresses

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.1 Address Binding


Compile time

Absolute code can


be generated
Know at compile
time where the
process will reside
in memory

MS-DOS .COMformat programs


are absolute code

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.1 Address Binding


Load time

Relocatable code
can be generated
Not known at
compile time where
the process will
reside in memory

Final binding is
delayed until load
time

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.1 Address Binding


Execution time

The process can be


moved from one
memory segment
to another
Binding must be
delayed until run
time

Special hardware
must be available

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.2 Logical- Versus PhysicalAddress Space


Logical address

An address generated by the CPU


Compile-time and load-time
Also called virtual address
Logical-address space
The set of all logical addresses

Physical address

An address seen by the memory unit


The one loaded into the memory-address unit

Execution time
Logical and physical address spaces differ

Physical-address space

Ku-Yaw Chang

Chapter 9 Memory Management

9.1.2 Logical- Versus PhysicalAddress Space


Memory-Management Unit (MMU)

A hardware device
Run-time mapping from virtual to physical addresses

Different methods to accomplish such a mapping

Logical addresses

0 to max

Physical addresses

R + 0 to R + max

Ku-Yaw Chang

Chapter 9 Memory Management

10

Dynamic Relocation
Using a Relocation Register

Ku-Yaw Chang

Chapter 9 Memory Management

11

9.1.3 Dynamic Loading


The entire program and data must be in memory
for its execution

The size of a process is limited to the size pf physical


memory.

Dynamic Loading

All routines are kept on disk in a relocatable load


format
The main program is loaded into memory and is executed

A routine is not loaded until it is called

Advantage

An unused routine is never loaded

Ku-Yaw Chang

Chapter 9 Memory Management

12

9.1.4 Dynamic Linking and


Shared Libraries
Dynamic Linking

Linking is postponed until execution time


Small piece of code, called stub, used to locate the
appropriate memory-resident library routine

OS checks if routine is in processes memory address


If yes, load the routine into memory
Stub replaces itself with the address of the routine, and
executes the routine.

Dynamic linking is particularly useful for libraries.

Ku-Yaw Chang

Chapter 9 Memory Management

13

9.1.5 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
Features

Implemented by user
No special support needed from operating system
Programming design is complex

Ku-Yaw Chang

Chapter 9 Memory Management

14

9.1.5 Overlays

Ku-Yaw Chang

Chapter 9 Memory Management

15

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

16

Swapping
A process can be

Swapped temporarily out of memory to a backing


store
Commonly a fast disk

Brought back into memory for continued execution

A process swapped back into

The same memory space


Binding is done at assembly or load time

A different memory space


Execution-time binding

Ku-Yaw Chang

Chapter 9 Memory Management

17

Swapping of two processes

Ku-Yaw Chang

Chapter 9 Memory Management

18

Swapping
Context-switch time is fairly high

User process size: 1MB


Transfer rate: 5MB per second
Actual transfer:
1000KB / 5000 KB per second = 200 milliseconds

An average latency: 8 ms
Total swap time
208 + 208 = 416 ms

Time quantum should be substantially larger than


0.416 seconds.

Ku-Yaw Chang

Chapter 9 Memory Management

19

Swapping
Major part of the swap time is transfer time

Directly proportional to the amount of memory


swapped

Factors

How much memory is actually used


To reduce swap time

Be sure the process is completely idle


Pending I/O

Ku-Yaw Chang

Chapter 9 Memory Management

20

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

21

Contiguous Memory Allocation


Memory

One for the resident operating system


In either low or high memory

Location of the interrupt vector

One for the user processes

Contiguous memory allocation

Each process is contained in a single contiguous


section of memory

Ku-Yaw Chang

Chapter 9 Memory Management

22

Memory Protection
A relocation register with a limit register

Ku-Yaw Chang

Chapter 9 Memory Management

23

Memory Allocation
Fixed-sized partitions

Simplest
Each partition contain exactly one process
Degree of multiprogramming is bounded

Strategies
First fit
Best fit
Worst fit

Problem

External fragmentation
Internal fragmentation

50-percent rule

Given N allocated blocks


Another 0.5 N blocks will be lost

Ku-Yaw Chang

Chapter 9 Memory Management

24

Memory Allocation
Possible solutions to the external fragmentation

Compaction
Permit the logical address space of a process to be
noncontiguous
OS

OS

OS

OS

process 5

process 5

process 5

process 5

process 9

process 9

process 8
process 2

Ku-Yaw Chang

process 10
process 2

process 2

Chapter 9 Memory Management

process 2

25

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

26

Paging
A memory-management scheme that permits
the physical-address space of a process to be
noncontiguous

Frames (physical memory)


Fixed-sized blocks

Pages (logical memory)


Blocks of the same size

Every address is divided into

A page number (p)


An index to a page table

A page offset (d)

Ku-Yaw Chang

page number

page offset

m-n

Chapter 9 Memory Management

27

Paging hardware

Ku-Yaw Chang

Chapter 9 Memory Management

28

Paging Model

Ku-Yaw Chang

Chapter 9 Memory Management

29

Paging Example

Ku-Yaw Chang

Chapter 9 Memory Management

30

Free Frames

Before Allocation
Ku-Yaw Chang

After Allocation

Chapter 9 Memory Management

31

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

32

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

33

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

34

Chapter 9 Memory Management


1. Background
2. Swapping
3. Contiguous Memory
Allocation
4. Paging

Ku-Yaw Chang

5. Segmentation
6. Segmentation with
Paging
7. Summary
8. Exercises

Chapter 9 Memory Management

35

Вам также может понравиться