24
CY2003 Computer Systems Lecture 09 Memory Management

CY2003 Computer Systems Lecture 09 Memory Management

Embed Size (px)

Citation preview

Page 1: CY2003 Computer Systems Lecture 09 Memory Management

CY2003Computer Systems

Lecture 09

Memory Management

Page 2: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 2

Overview

• Memory management concepts

– memory types

• cache, primary and secondary

– contiguous v. non-contiguous memory allocation

– single user memory allocation

• Fixed partition multiprogramming

– concepts

• Variable partition multiprogramming• Allocation strategies

Page 3: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 3

Background

• The process manager determines– when a process will run

• The memory manager determines– where a process will run

• The ‘first law’ of computing– however much memory a computer has,

a program will be written that needs more!

• The functions of the memory manager include– allocating memory to processes– protecting memory areas from interference– overcoming the ‘first law’ by simulating extra memory

Page 4: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 4

Memory Types• There are three main types of computer memory

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

– primary• the main memory of the computer

– Random Access Memory (RAM) or Read Only Memory (ROM)

– secondary• hard disks or other offline storage devices

cache

primary

secondary

Page 5: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 5

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

Page 6: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 6

Overlays• Even with this very simple model 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

Page 7: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 7

Contiguous and Non-contiguous

• The simple system presented so far allocates memory only in a single block– this is called contiguous storage allocation– the memory is in a single, continuous block, with no

‘holes’ or ‘gaps’

• More complex memory management models have the capability to allocate memory in multiple blocks, or segments, which may be placed anywhere in primary memory– the blocks are not necessarily next to each other– this is called non-contiguous storage allocation

Page 8: CY2003 Computer Systems Lecture 09 Memory Management

Fixed Partition Multiprogramming

Page 9: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 9

Concepts

• The first stage in extending the simple one-process model is to allow multiprogramming– this can improve processor utilisation

• if the average process only uses the processor 50% of the time and is performing I/O (waiting) for the other 50% then we can fully utilise the processor with two jobs

• Multiple jobs can be run if there is space in main memory, for example– the computer has 1Mb main memory

• the operating system occupies 200Kb• each process fits into 400Kb

– the computer has enough memory to run both jobs

Page 10: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 10

Fixed Partitions

• The simplest multiprogramming scheme is the fixed partition multiprogramming model

• Memory is divided up into N partitions of fixed size– the partitions do not have to be the same size

– the size of each may be determined by operators• e.g. the IBM OS/360: partitions determined each morning

– the maximum size of each process must be known

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

Page 11: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 11

Multiple Partition Queues

0

100K

200K

400K

700K

process Eprocess Fprocess G

process D

process C

process Aprocess B process A

process C

process D

process Eprocess Fprocess G

process B

process F

process Fprocess B

process G process G

operating system

Page 12: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 12

Single Partition Queue

0

100K

200K

400K

700K

process A

process B

process C

process D

process Bprocess C

process Dprocess Eprocess Fprocess G

process A

operating system

process E

process F

process G

Page 13: CY2003 Computer Systems Lecture 09 Memory Management

Variable Partition Multiprogramming

Page 14: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 14

What is Swapping?

• In a batch oriented operating system, fixed partition multiprogramming is simple and effective– as long as enough jobs can be kept in memory to keep

the processor busy there is no real reason to use a more complicated memory allocation scheme

• In timesharing systems the situation is different– there are usually more users than there is main

memory to hold all their processes– excess processes must be kept on hard disk

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

Page 15: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 15

Fixed Partition Swapping

• In principle, a swapping system could be based on the fixed partition allocation scheme– whenever a process blocked it could be moved out to

hard disk and the partition freed for use by another process to be swapped in

• However, this is likely to waste a lot of memory– if a large process is swapped out of a large partition,

there may only be small processes ready to run

• If we are going to the trouble of implementing the ability to swap, then it is better to allow the partition sizes to change size (dynamically)

Page 16: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 16

The Problem of Fixed Partitions

process A

process B

process C

process D

process E

process D(blocked)

process E

process A

process Dalthough process D is now ready to run again, there is no partition free that is big enough to hold it

there is now a lot of wasted memory

0

100K

200K

400K

700K

operating system

Page 17: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 17

Variable Partitions

• When using variable partitions, the number, location and size of each partition is governed by the processes actually being run– when a process is to be swapped in (by being newly

created or recently unblocked)• a new partition big enough to hold the process is created

– when a process is to be swapped out• the partition is freed

• This flexibility improves memory usage buts also complicates allocation and deallocation– as well as keeping track of the memory used

Page 18: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 18

How Variable Partitions Work

process A

process B

process C

process D

process E

operating system0

1 Mb

Page 19: CY2003 Computer Systems Lecture 09 Memory Management

Allocation Strategies

Page 20: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 20

Allocation with Linked Lists

• A more sophisticated allocation data structure is required to deal with a variable number of free and used partitions– there are various data structures and associated

algorithms available• A linked list is one such possible data structure

– a linked list consists of a number of entries (‘links’!)– each link contains data items– each link also contains a pointer to the next in the chain

start free? nextstart size free? nextstart size free? endstart size

Page 21: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 21

First Fit

• The linked list is initialised to a single link of the entire memory size, flagged as ‘free’

• Whenever a block of memory is requested the linked list is scanned in order until a link is found which represents free space of sufficient size– if requested space is exactly the size of the free space

• all the space is allocated

– else, the free link is split into two• the first entry is set to the size requested and marked ‘used’• the second entry is set to remaining size and marked ‘free’

• When a block is freed the link is marked ‘free’

Page 22: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 22

Next Fit

• The first fit algorithm starts scanning at the start of the linked list whenever a block is requested

• As a minor variation, the next fit algorithm maintains a record of where it got to, in scanning through the list, each time an allocation is made– the next time a block is requested the algorithms

restarts its scan from where ever it left off last time– the idea is to give an even chance to all of memory to

getting allocated, rather than concentrating at the start

• However, simulations have shown that next fit actually gives worse performance than first fit!

Page 23: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 23

Best Fit

• First fit just looks for the first available hole– it doesn’t take into account that there may be a hole

later in the list that exactly fits the requested size– first fit may break up a big hole when the right size

hole exists later on

• The best fit algorithm always searches the entire linked list to find the smallest hole big enough to satisfy the memory request– however, it is slower than first fit because of searching– surprisingly, it also results in more wasted memory!

• because it tends to fill up memory with tiny, useless holes

Page 24: CY2003 Computer Systems Lecture 09 Memory Management

© LJMU, 2004 CY2003- Week 09 24

Summary

• Memory management concepts

– memory types

• cache, primary and secondary

– contiguous v. non-contiguous memory allocation

– single user memory allocation

• Fixed partition multiprogramming

– concepts

• Variable partition multiprogramming• Allocation strategies