csci2413 lecture 5 operating systems memory management 1 phones off (please)

26
CSCI2413 Lecture 5 Operating Systems Memory Management 1 phones off (please)

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

CSCI2413 Lecture 5

Operating Systems

Memory Management 1

phones off (please)

© De Montfort University, 2004 CSCI2413 - L5 2

Lecture Outline

• Background

• Memory Types

• Memory Management..– Overlays– Swapping

• Allocation– First fit, Best fit, Worst fit

© De Montfort University, 2004 CSCI2413 - L5 3

Background

• The process scheduling determines– when a process will run

• The memory manager determines– where a process will run

© De Montfort University, 2004 CSCI2413 - L5 4

The problem !

• No matter how much memory we install, some programmer will always want to use more!!

• How do we get a quart into a pint pot ??

Hence, MEMORY Management …

© De Montfort University, 2004 CSCI2413 - L5 5

Memory Types– CPU registers

– cache• on board, or very close to, the microprocessor

– Primary or main memory – Read And Write Memory (RAM) or Read Only Memory (ROM)

– secondary• hard disks or other offline storage devices

© De Montfort University, 2004 CSCI2413 - L5 6

Memory Speeds

• The clock for a 500 MHz processor ticks at a rate of 500 million times per second– each tick takes 2 nanoseconds

• cache memory, built into the microprocessor, – access time

• typically a few nanoseconds (around 10 nsec.)

• Primary memory • around 50 - 100 nanoseconds

• Hard disk, about 10 milliseconds• or ten million nanoseconds!

© De Montfort University, 2004 CSCI2413 - L5 7

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 in the code must be translated to actual physical memory addresses

© De Montfort University, 2004 CSCI2413 - L5 8

© De Montfort University, 2004 CSCI2413 - L5 9

A Simple Model• In the simplest memory management model

– one process is in memory at a time• that process is allowed to use as much memory as available

userprogram 1

userprogram 2

operatingsystem 0x0000

0x1000

0xFFFF

© De Montfort University, 2004 CSCI2413 - L5 10

Overlays

• Split the program into small blocks

• Keep in main only those blocks that are needed at any given time.

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

© De Montfort University, 2004 CSCI2413 - L5 11

Overlays …• it is possible to increase the amount of memory

available through the use of overlays

main: initialisationinputprocessingoutput

initialisation

main: initialisationinputprocessingoutput

main: initialisationinputprocessingoutput

input

main: initialisationinputprocessingoutput

processing

main: initialisationinputprocessingoutput

output

operatingsystem 0x0000

0x1000

0xFFFF

© De Montfort University, 2004 CSCI2413 - L5 12

Swapping

• Moving processes to and fro between main memory and hard disk is called swapping

• Roll out, roll in – swapping variant used for priority-based scheduling algorithms;

• lower-priority process is swapped out so higher-priority process can be loaded and executed.

© De Montfort University, 2004 CSCI2413 - L5 13

Schematic View of Swapping

Main Memory Backing store

Operating Systems

Process1

User Space

Process2

Swap out

Swap in

© De Montfort University, 2004 CSCI2413 - L5 14

Fixed Partitioning

• Main memory is divided into equal- (or unequal-) sized partitions and these partitions are assigned to processes.

• Any program, no matter how small, occupies an entire partition. This is called internal fragmentation. Main memory use is inefficient.

• The operating system keeps a track of which partitions are being used and which are free– processes are allocated to partitions when free

© De Montfort University, 2004 CSCI2413 - L5 15

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

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

© De Montfort University, 2004 CSCI2413 - L5 16

Memory Allocation Methods

1. Single Process Systems

User Process

Operating System

Unused Space

© De Montfort University, 2004 CSCI2413 - L5 17

Memory Allocation Methods

2. Fixed Partition Memory

Operating System

Process A

180K

Process B

200K

Process C

300K

200K

300K

400K

0K

1000K

© De Montfort University, 2004 CSCI2413 - L5 18

Memory Allocation Methods

3. Variable Partition Memory

Operating System

Process A

180K

Process B

200K

Process C

300K

© De Montfort University, 2004 CSCI2413 - L5 19

Contiguous Allocation

• Main memory usually into two partitions:– Resident operating system, usually held in low

memory with interrupt vector.– User processes then held in high memory.

• contiguous storage – allocates memory only in a single block

• the memory is in a single, continuous block, with no ‘holes’ or ‘gaps’

© De Montfort University, 2004 CSCI2413 - L5 20

Contiguous Allocation …

• Hole – blocks of memory; holes of various size scattered throughout memory.– When a process arrives, it is allocated memory from a

hole large enough to accommodate it.– Operating system maintains information about:

a) allocated partitions b) free partitions (hole)

process 2

process 5

process 9

OS

process 2

process 5

OS

process 2

process 5

OS

process 2

process 5

process 9

OS

process 9

process 10

© De Montfort University, 2004 CSCI2413 - L5 21

Non-contiguous …

• Allocate memory in multiple blocks, or segments, which may be placed anywhere in memory– the blocks are not necessarily next to each other– this is called non-contiguous storage allocation

© De Montfort University, 2004 CSCI2413 - L5 22

Placement Algorithms

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

– First-fit: scan the list segment and choose the block that is closest to the request.

– Best-fit: search the entire list and allocate the smallest hole that is big enough;

– Worst-fit: allocate the largest hole; must also search entire list. Produces the largest leftover hole.

© De Montfort University, 2004 CSCI2413 - L5 23

Example:

A variable partition memory has the following hole sizes in memory order:

200K, 600K, 400K, 800K, 350K, 70K

A new process of size 300K enters the system. Determine where it will go according to the best-fit, first-fit and worst-fit algorithms and update the hole size status of the memory after the process has been added.

© De Montfort University, 2004 CSCI2413 - L5 24

Placement Policies

Another Example

Hole sizes are:

475K 425K 350K

2 processes of sizes 370K and 100K

Best–fit ? First-fit ?

© De Montfort University, 2004 CSCI2413 - L5 25

Dynamic relocation using a relocation register

© De Montfort University, 2004 CSCI2413 - L5 26

Summary

• A number of processes can coexist in memory using a variety of allocation techniques and placement policies

• Main problem is the creation of unusable holes as processes terminate making it sometimes difficult to accommodate new processes

• Compaction can be used to shuffle processes and create larger more usable space but the large overheads involved make it not always feasible to carry out in real time.