Docsity

Memory Management: Segmentation, Memory Hierarchy, and Paging, Study notes of Computer Science

Partial preview of the text

Download Memory Management: Segmentation, Memory Hierarchy, and Paging and more Study notes Computer Science in PDF only on Docsity!

Chapter 4: Memory Management

Part 1: Mechanisms for Managing Memory

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Memory management

 Basic memory management

 Swapping

 Virtual memory

 Page replacement algorithms

 Modeling page replacement algorithms

 Design issues for paging systems

 Implementation issues

 Segmentation

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Memory hierarchy

 What is the memory hierarchy?

 Different levels of memory  Some are small & fast  Others are large & slow

 What levels are usually included?

 Cache: small amount of fast, expensive memory  L1 (level 1) cache: usually on the CPU chip  L2 & L3 cache: off-chip, made of SRAM  Main memory: medium-speed, medium price memory (DRAM)  Disk: many gigabytes of slow, cheap, non-volatile storage

 Memory manager handles the memory hierarchy

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Basic memory management

 Components include

 Operating system (perhaps with device drivers)  Single process

 Goal: lay these out in memory

 Memory protection may not be an issue (only one program)  Flexibility may still be useful (allow OS changes, etc.)

 No swapping or paging

Operating system (RAM) User program (RAM) 0xFFFF 0xFFFF 0 0 User program (RAM) Operating system (ROM) Operating system (RAM) User program (RAM) Device drivers (ROM)

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

How many programs is enough?

 Several memory partitions (fixed or variable size)

 Lots of processes wanting to use the CPU

 Tradeoff

 More processes utilize the CPU better

 Fewer processes use less memory (cheaper!)

 How many processes do we need to keep the CPU

fully utilized?

 This will help determine how much memory we need

 Is this still relevant with memory costing $150/GB?

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Modeling multiprogramming

 More I/O wait means less

processor utilization

 At 20% I/O wait, 3– processes fully utilize CPU  At 80% I/O wait, even 10 processes aren’t enough

 This means that the OS

should have more

processes if they’re I/O

bound

 More processes =>

memory management &

protection more

important!

0

1 0 1 2 3 4 5 6 7 8 9 10 Degree of Multiprogramming CPU Utilization 80% I/O Wait 50% I/O Wait 20% I/O Wait

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Memory and multiprogramming

 Memory needs two things for multiprogramming

 Relocation

 Protection

 The OS cannot be certain where a program will be

loaded in memory

 Variables and procedures can’t use absolute locations in

memory

 Several ways to guarantee this

 The OS must keep processes’ memory separate

 Protect a process from other processes reading or

modifying its own memory

 Protect a process from modifying its own memory in

undesirable ways (such as writing to program code)

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Base and limit registers

 Special CPU registers: base & limit  Access to the registers limited to system mode  Registers contain  Base: start of the process’s memory partition  Limit: length of the process’s memory partition  Address generation  Physical address: location in actual memory  Logical address: location from the process’s point of view  Physical address = base + logical address  Logical address larger than limit => error Process partition OS 0 0xFFFF Limit Base 0x 0x Logical address: 0x Physical address: 0x1204+0x9000 = 0xa

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Swapping: leaving room to grow

 Need to allow for programs

to grow

 Allocate more memory for data  Larger stack

 Handled by allocating more

space than is necessary at

the start

 Inefficient: wastes memory that’s not currently in use  What if the process requests too much memory? OS Code Data Stack Code Data Stack Process B Process A Room for B to grow Room for A to grow

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Tracking memory usage: bitmaps

 Keep track of free / allocated memory regions with a bitmap  One bit in map corresponds to a fixed-size region of memory  Bitmap is a constant size for a given amount of memory regardless of how much is allocated at a particular time  Chunk size determines efficiency  At 1 bit per 4KB chunk, we need just 256 bits (32 bytes) per MB of memory  For smaller chunks, we need more memory for the bitmap  Can be difficult to find large contiguous free areas in bitmap

A B C D

8 16 24 32

Memory regions

Bitmap

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Allocating memory

 Search through region list to find a large enough space  Suppose there are several choices: which one to use?  First fit: the first suitable hole on the list  Next fit: the first suitable after the previously allocated hole  Best fit: the smallest hole that is larger than the desired region (wastes least space?)  Worst fit: the largest available hole (leaves largest fragment)  Option: maintain separate queues for different-size holes

  • 6 5 - 19 14 - 52 25 - 102 30 - 135 16
  • 202 10 - 302 20 - 350 30 - 411 19 - 510 3 Allocate 20 blocks first fit 5 Allocate 12 blocks next fit 18 Allocate 13 blocks best fit 1 Allocate 15 blocks worst fit 15

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Freeing memory

 Allocation structures must be updated when memory is freed

 Easy with bitmaps: just set the appropriate bits in the bitmap

 Linked lists: modify adjacent elements as needed

 Merge adjacent free regions into a single region  May involve merging two regions with the just-freed area A X B A X X B X

A B

A

B

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Virtual memory

 Basic idea: allow the OS to hand out more memory

than exists on the system

 Keep recently used stuff in physical memory

 Move less recently used stuff to disk

 Keep all of this hidden from processes

 Processes still see an address space from 0 – max address

 Movement of information to and from disk handled by the

OS without process help

 Virtual memory (VM) especially helpful in

multiprogrammed system

 CPU schedules process B while process A waits for its

memory to be retrieved from disk

CS 1550, cs.pitt.edu Chapter (^4 ) (originaly modified by Ethan

Virtual and physical addresses

 Program uses virtual

addresses

 Addresses local to the process  Hardware translates virtual address to physical address

 Translation done by the

M emory M anagement U nit

 Usually on the same chip as the CPU  Only physical addresses leave the CPU/MMU chip

 Physical memory indexed

by physical addresses

CPU chip

CPU

Memory

Disk

controller

MMU

Virtual addresses from CPU to MMU Physical addresses on bus, in memory