27
Memory Management Chapter 4

Memory Management

  • Upload
    lawson

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

Memory Management. Chapter 4. Memory hierarchy. Programmers want a lot of fast, non-volatile memory But, here is what we have:. Memory manager. Tracks which parts of memory are in use Allocates memory to processes Long-term scheduling De-allocates memory from processes - PowerPoint PPT Presentation

Citation preview

Page 1: Memory Management

Memory Management

Chapter 4

Page 2: Memory Management

Memory hierarchy

• Programmers want a lot of fast, non-volatile memory

• But, here is what we have:

Page 3: Memory Management

Memory manager

• Tracks which parts of memory are in use

• Allocates memory to processes– Long-term scheduling

• De-allocates memory from processes

• Swaps between main memory and disk when main memory is too small to hold all of the processes – Intermediate scheduling

Page 4: Memory Management

Simple memory management

An operating system with one user process

Page 5: Memory Management

Multiprogramming

• Use fixed-sized partitions (MFT)

– Each partition contained only one process

– Very simple

• More flexibility is achieved with MVT– OS knows which parts of memory are available – When a process is to be loaded, it needs a large

enough block of contiguous memory

Page 6: Memory Management

Multiprogramming with fixed size partitions (MFT)

single input queue for all memory

separate input queuefor each partition

Page 7: Memory Management

MVT Examplejob queue at time 0

operatingsystem

0

400K

2160K

2560K

How to schedule the

job queue in a FCFS fashion?

process memory time in system

P1 600K 10P2 1000K 5P3 300K 20P4 700K 8P5 500K 15

Page 8: Memory Management

MVT Example – at time 5

job queue

process memory time in system

P4 700K 8P5 500K 15

operatingsystem

0

400K

260K2560K

Now P2 is done, so replace with

P4…

P1

P2

P32000K

2300K

1000K

Page 9: Memory Management

MVT Example – at time 8

job queue

process memory time in system

P5 500K 15

operatingsystem

0

400K

260K2560K

Now P4 is done, so replace with

P5…

P1

P4

P32000K

2300K

1000K

1700K 300K

Page 10: Memory Management

MVT Example – at time 8+

Empty job queue

operatingsystem

0

400K

260K2560K

This worked well with our

configuration of jobs, but what can

go wrong?

P1

P5

P32000K

2300K

1000K1500K 500K

Page 11: Memory Management

CPU utilization as a function of number of processes in memory

Degree of multiprogramming

Page 12: Memory Management

Relocatability

• Processes are loaded into main memory– They may be loaded into any unoccupied user

space. – Successive executions of the same process may be

loaded into different main memory locations. • This is the concept of relocatability

– Any memory address references within a process (i.e., variables, instructions) are relative

Page 13: Memory Management

Address mapping

14000+CPU memory

logicaladdress 346

physicaladdress 14346

MMU

relocation

register

Page 14: Memory Management

Protecting processes from each other

• Use base and limit values – Each location within a process is added to base value

to map it to a physical address– Any address locations larger than the limit value are

flagged as errors

Page 15: Memory Management

Base-limit registers

One base-limit pair and two base-limit pairs

Page 16: Memory Management

Swapping

• What to do if there is not enough memory to store all active processes?– Swap certain processes out and then back in

• An executing process must be in main memory, but can be temporarily swapped out & then back into memory– Consider a preemptive CPU scheduling

algorithm such as RR

Page 17: Memory Management

About swapping...

• When the CPU is ready for the next process, it selects it from the ready queue. – If it has been swapped out, the memory manager

brings it back in.– If there is no room for it, another process is swapped

out first. – Context-switch time is high.

• If a process is to be swapped out of main memory, it must be completely idle– If it is waiting for I/O, then another candidate is

found, if possible

Page 18: Memory Management

Swapping entire processes

Swapping can create holes or fragments in memory

Page 19: Memory Management

Fragmentation• As processes are loaded and removed from

memory, available memory space is broken into pieces

• When there is enough memory space to satisfy a request, but it is not contiguous, we say there is external fragmentation

• When there is wasted space within a process’ address space, we have internal fragmentation (later on this)

Page 20: Memory Management

Compaction

• A solution to external fragmentation.

• Partitions are rearranged to collect all the fragments into one large block.– Requires all internal addresses to be remapped

to new physical addresses– All partitions are moved to one end of memory

and all base and limit registers are altered.

• Very costly

Page 21: Memory Management

Swapping with room for growth

Space for growing data segment

Space for growing data & stack segments

Page 22: Memory Management

Implementation

• How is dynamic memory allocation actually implemented?

• Two approaches– Using bitmaps– Using linked lists

• Strategies for assignment of processes to memory spaces

Page 23: Memory Management

Using bit maps

• Part of memory with 5 processes, 3 holes– tick marks show allocation units – may be a few

words up to several KB

– shaded regions are free

– Searching bitmaps can be slow

Page 24: Memory Management

Using linked lists

• Part of memory with 5 processes, 3 holes• This example uses a singly linked list – a doubly linked

list would make it easier to merge available slots

• See next slide

• We have several strategies for assigning memory

Page 25: Memory Management

Memory management with linked lists

Four neighbor combinations for the terminating process X

Page 26: Memory Management

Allocation strategies

• First-fit: Starting at the head of the list, allocate the first hole that is found to be big enough. – Next fit: pick up where it left the list last time

• Best-fit: Search entire list to find the optimal fit. – this allocates the smallest hole that is big enough.

• Worst-fit: Search entire list to find the largest available hole.

• Quick fit: Uses separate lists for more common process sizes

Page 27: Memory Management

Assignment

• In-class:– p. 264 - #5

• HW:– Given memory partitions of 100K, 500K,

200K, 300K, 600K (in order), how would each of first-fit, best-fit, worst-fit, and next-fit algorithms place processes of 212K, 417K, 112K, 426K (in this order)?

– Read Sections 4.3 & 4.4