Vous êtes sur la page 1sur 38

Memory Management

Ashok Singh Sairam

Course Details
Memory Management and File Management
Dr. Ashok Singh Sairam

Process and I/O Management


Dr. Rajiv Misra

Grading Policy:
Quiz/Assignment: 12%; Midsem: 34 %; Endsem: 54%

Labs: will be on Nachos


Lab Assignments: 50%; Midsem: 20%; Endsem: 30%

CS 341: Operating Systems

Von Neumann Architecture

CS 341: Operating Systems

Memory Hierarchy

CS 341: Operating Systems

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 341: Operating Systems )

Memory Management Issues


Allocation: Process must be allocated space in main memory. Swapping: Move out an idle program temporarily to a persistent storage space Protection: Check for illegal access of data from another processs memory area Paging: Allows physical space of a program to be non-contiguous. Segmentation: Allows memory to be managed as a collection of segments.
CS 341: Operating Systems 6

Mapping Data & Instructions to Memory


Address binding of instructions and data to memory addresses can happen at three different stages Compile time: Memory location known a priori, absolute code can be generated; Load time: Must generate relocatable code if memory location is not known at compile time Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps
CS 341: Operating Systems 7

Why do we need relocatable code?


In case of absolute code, if that part of main memory is in use the program cannot run. For a variable x in a program P, an absolute code allocation of P will mean that the P can be run only when x is allocated the same memory again.

CS 341: Operating Systems

Who generates relocatable code?


Linking and Loading

static linking

CS 341: Operating Systems

Dynamic Linking
Linking postponed until execution time Small piece of code, stub, used to locate the appropriate memory-resident library routine Stub replaces itself with the address of the routine, and executes the routine Operating system needed to check if routine is in processes memory address Dynamic linking is particularly useful for libraries

CS 341: Operating Systems

10

Dynamic Loading
Routine is not loaded until it is called Better memory-space utilization; unused routine is never loaded Useful when large amounts of code are needed to handle infrequently occurring cases No special support from the operating system is required implemented through program design

CS 341: Operating Systems

11

How does CPU access memory?


The concept of a logical address space that is bound to a separate physical address space is central to proper memory management
Logical address generated by the CPU; also referred to as virtual address Physical address address seen by the memory unit

Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme

CS 341: Operating Systems

12

Dynamic Relocation using Relocation Registers

Is relocation register alone enough for multiprogramming?


CS 341: Operating Systems 13

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 cant use absolute locations in memory Several ways to guarantee this 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)
14

The OS must keep processes memory separate

CS 341: Operating Systems

Base/Relocation and limit registers


Special CPU registers: base & limit
0xFFFF 0x2000

Access to the registers limited to system mode Registers contain


Base: start of the processs memory partition Limit: length of the processs memory partition

Limit Process partition Base


0x9000

Address generation

Physical address: location in actual memory Logical address: location from the processs point of view Physical address = base + logical address Logical address larger than limit => error

OS
0

Logical address: 0x1204 Physical address: 0x1204+0x9000 = 0xa204


15

CS 341: Operating Systems )

Protection using base and limit registers

CS 341: Operating Systems

16

Basic memory management


Components include
Operating system (perhaps with device drivers) Single process Memory protection may not be an issue (only one program) Flexibility may still be useful (allow OS changes, etc.) 0xFFFF

Goal: lay these out in memory No swapping or paging


0xFFFF
User program (RAM) Operating system (RAM)

Operating system (ROM) User program (RAM)

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

0
17

CS 341: Operating Systems

Fixed partitions: multiple programs


Fixed memory partitions
Divide memory into fixed spaces Assign a process to a space when its free

Mechanisms
Separate input queues for each partition Single input queue: better ability to optimize CPU usage
900K Partition 4 Partition 3 Partition 2 Partition 1 OS
CS 341: Operating Systems

900K Partition 4 700K 600K 500K 100K 0


18

700K 600K 500K

Partition 3 Partition 2 Partition 1 OS

How many programs is enough?


100K 0

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!). However this is not relevant anymore as memory prices have become very cheap.

CS 341: Operating Systems

19

Modeling multiprogramming
More I/O wait means less processor utilization
CPU Utilization 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 1 2 3 4 5 6 7 8 9 10 Degree of Multiprogramming 80% I/O Wait 50% I/O Wait 20% I/O Wait

At 20% I/O wait, 34 processes fully utilize CPU At 80% I/O wait, even 10 processes arent enough

This means that the OS should have more processes if theyre I/O bound More processes => memory management & protection more important!
CS 341: Operating Systems

20

Swapping

CS 341: Operating Systems

21

Swapping
C B A OS C B OS C B D OS C D OS C A D OS

A OS

B A OS

Memory allocation changes as


Processes come into memory Processes leave memory
Swapped to disk Complete execution

Gray regions are unused memory


CS 341: Operating Systems 22

Swapping: leaving room to grow


Need to allow for programs to grow
Allocate more memory for data Larger stack Process B Stack Room for B to grow Data Code Stack Process A Room for A to grow Data Code OS
23

Handled by allocating more space than is necessary at the start


Inefficient: wastes memory thats not currently in use What if the process requests too much memory?

CS 341: Operating Systems )

Context Switch time


User process size: 100 MB Backing store is a standard HDD with a transfer rate of 50MB/sec Latency is 8 msecs What is the total swap time?

CS 341: Operating Systems

24

Limitations of swapping
Problems with swapping
Process must fit into physical memory (impossible to run larger processes) Memory becomes fragmented
External fragmentation: lots of small free areas Compaction needed to reassemble larger free areas

Transfer time is very high

Overlays solved the first problem


Bring in pieces of the process over time (typically data) Still doesnt solve the problem of fragmentation or transfer time

CS 1550, cs.pitt.edu (originaly modified by Ethan

Chapter 4

25

Fixed Size Partitions & Swapping


Memory divided into Fixed size partitions Any process size <= partition size can be loaded into an available partition If all partitions are full, O/S can swap out a process

CS 341: Operating Systems

26

Fixed Size Partitions (contd)


Partitions may be of equal or unequal size.

How to load process into Memory ?

CS 341: Operating Systems

27

CS 341: Operating Systems

28

Disadvantages of Fixed Size Partition


A program may not fit the partition Max no. of active processes ? Internal fragmentation can also occur (wasted space inside a partition) No longer used today.

How to overcome these problems?


CS 341: Operating Systems 29

Dynamic Partition Scheme


Initially all memory is available for user process. Process is allocated as much memory as required (if available) When a process terminates it releases memory. Eventually get holes in the memory called external fragmentation Must use compaction to shift processes so that they are contiguous and free memory is available as one block.

CS 341: Operating Systems

30

Dynamic Partitioning Example

CS 341: Operating Systems

31

Dynamic Storage Allocation Problem

At any given time we will have a list of available block sizes and an input queue of processes. How to satisfy a request of size n from a list of free holes?

CS 341: Operating Systems

32

Example
Where to allocate memory space for process P2?
Free1

Free2 Free3

First Fit Best Fit Worst Fit


CS 341: Operating Systems

Main Memory

33

First Fit
Allocate the first hole that is big enough Searching may start from the beginning or from the position where the last first-fit search ended. Usually the fastest May have many processes loaded at the front end which must be searched and skipped when trying to find a free block.

CS 341: Operating Systems

34

Best Fit and Worst Fit


Chose the block that is closest in size to the request The entire list of holes must be searched Produces the smallest left overall hole Worst Fit Allocate the largest hole. The entire list of holes must again be searched Produces the largest left-over hole.

CS 341: Operating Systems

35

Request Size: 16 MB Where would first fit, best fit and worst fit pick?

How to avoid external fragmentation?


CS 341: Operating Systems 36

External Vs. Internal Fragmentation

CS 341: Operating Systems

37

Fragmentation
External Fragmentation total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Reduce external fragmentation by compaction
Shuffle memory contents to place all free memory together in one large block Compaction is possible only if relocation is dynamic, and is done at execution time
CS 341: Operating Systems 38

Vous aimerez peut-être aussi