Memory Management Motivation

harpywarrenSoftware and s/w Development

Dec 14, 2013 (3 years and 10 months ago)

97 views

Memory Management
Carsten Griwodz
University of Oslo
(includes slides from A. Tanenbaum and M. van Steen)
Motivation
! In project assignments so far
! Program code is linked to kernel
! Physical addresses are well-known
! Not realistic
! In the real world
! Programs are loaded dynamically
! Physical addresses it will get are not known to
program
! Program size at run-time is not known to kernel
1Memory Management for Monoprogramming
! Only one user program loaded
! Program is entirely in memory
! No swapping or paging
! Three simple ways of organizing memory
C64, ZX80, …
Some PDAs, embedded systems
User program OS in ROM
0x0 0xfff….
Old mainframes and minicomputers
OS in RAM User program
0x0 0xfff….
MS-DOS, …
OS in RAM User program Devs in ROM
0x0 0xfff….
Multiprogramming
! Processes have to wait for I/O
! Goal
! Do other work while a process waits
! Give CPU to another process
! Processes may be concurrently ready
! So
! If I/O waiting probability for all processes is p
! Probable CPU utilization can be estimated as
n
CPU utilization = 1 - p
2Multiprogramming
! Arrival and work requirements of 4 jobs
! CPU utilization for 1-4 jobs with 80% I/O wait
# processors
Arrival CPU use IO wait
Remaining CPU time
Job# % 1 2 3 4
time time time
1 2. 1. 0.0 1 3
CPU idle 80 64 51 41
1 10:00 4 16
2 3. 2. 1. 1. 0.0 1 2 0 1
2 10:10 3 12
CPU busy 20 36 49 59
3 2. 1. 0.0 1 9
3 10:15 2 8
CPU per
4 2. 1. 0. 0.0 7 7 8
4 10:20 2 8 20 18 16 15
process
2.0
0.9 0.8 0.3
1
0.9 0.8 0.3 0.9 0.1
2
0.8 0.3 0.9
3
0.3 0.9 0.1 0.7
4 time
0 10 15 20 22 27.6
31.7
28.2
! Sequence of events as jobs arrive and finish
! Note numbers show amount of CPU time jobs get each interval
Multiprogramming
Degree of multiprogramming
! CPU utilization as a function of number of processes
in memory
3Multiprogramming
! Several programs
! Concurrently loaded into memory
! OS must arrange memory sharing
! Memory partitioning
! Memory
! Needed for different tasks within a process
! Shared among processes
! Process memory demand may change over time
! Use of secondary storage
! Move (parts of) blocking processes from memory
! Higher degree of multiprogramming possible
! Makes sense if processes block for long times
Memory Management for Multiprogramming
! Process may not be entirely
into memory
! Reasons
! Other processes use memory
! Their turn Registers
! Higher priority
! Process is waiting for I/O
Cache(s) 2x
! Too big
! For its share
! For entire available memory
DRAM 100x
! Approaches
Paging
! Swapping Swapping
Overlays
9
! Paging
10 x
Disk
! Overlays
4Memory Management for Multiprogramming
! Swapping
! Remove a process from memory
! With all of its state and data
! Store it on a secondary medium
! Disk, Flash RAM, other slow RAM, historically also Tape
! Paging
How to use these
! Remove part of a process from memory with
! Store it on a secondary medium Virtual Memory
! Sizes of such parts are fixed
! Page size
! Overlays
! Manually replace parts of code and data
! Programmers rather than OSs work
! Only for very old and memory-scarce systems
Memory Management Techniques
! Before details about moving processes out
! Assign memory to processes
! Memory partitioning
! Fixed partitioning
! Dynamic partitioning
! Simple paging
! Simple segmentation
! Virtual memory paging
! Virtual memory segmentation
5Fixed Partitioning
! Divide memory
! Into static partitions
! At system initialization time (boot or earlier)
! Advantages
! Very easy to implement
! Can support swapping process in and out
Fixed Partitioning
0x0
Operating system
! Two fixed partitioning
8MB
schemes
8MB
! Equal-size partitions
! Unequal-size partitions
8MB
8MB
! Equal-size partitions
! Big programs can not be
8MB
executed
! Unless program parts are 8MB
loaded from disk
8MB
! Small programs use entire
partition
8MB
! A problem called internal
0x…fff
fragmentation
6Fixed Partitioning
Operating system Operating system
! Two fixed partitioning
8MB 8MB
schemes 2MB
8MB 4MB
! Equal-size partitions
6MB
! Unequal-size partitions
8MB
8MB
8MB
! Unequal-size partitions
8MB
! Bigger programs can be
8MB
loaded at once
12MB
8MB
! Smaller programs can lead to
less internal fragmentation
8MB
! Advantages require
16MB
assignment of jobs to
8MB
partitions
Fixed Partitioning
Operating system
! Approach
8MB
2MB
! Has been used in
4MB
mainframes
6MB
! Uses the term job for a
8MB
running program
! Jobs run as batch jobs
8MB
! Jobs are taken from a
queue of pending jobs
12MB
! Problem with unequal
partitions
16MB
! Choosing a job for a
partition
7Fixed Partitioning
Operating system
! One queue per partition
8MB
2MB
! Internal fragmentation is
4MB
minimal
6MB
! Jobs wait although
8MB
sufficiently large
partitions are available 8MB
12MB
16MB
Fixed Partitioning
Operating system
! Single queue
8MB
2MB
! Jobs are put into next
4MB
sufficiently large
6MB
partition
8MB
! Waiting time is reduced
! Internal fragmentation is
8MB
bigger
12MB
! A swapping mechanism
can reduce internal
fragmentation
16MB
! Move a job to another
partition
8Dynamic Partitioning
Operating system
! Divide memory
8MB
! Partitions are created
Swapped in
Process 5
dynamically for jobs
Process 2
Process 1
External 14MB
20MB free
14MB
! Removed after jobs are
20MB
fragmentation
finished
6M 6M B fB ree
Process 4
Process 2
8MB
14MB free
56MB free
! External fragmentation
14MB
6MB free
! Problem increases with
36MB free
system running time
Process 3
! Occurs with swapping as
18MB
22MB free
well
4MB free
! Addresses of process 2
Solutions to address
changed
change with
Address Translation
Dynamic Partitioning
Operating system
! Reduce external
8MB
fragmentation
Swapped in
! Compaction Process 2
14MB
Process 4
6MB
! Compaction
8MB
Process 4
! Takes time
6MB free
8MB
Process 3
! Consumes processing
6MB free
18MB
resources
Process 3
6MB free
18MB
! Reduce compaction
16MB free
6MB free
need
4MB free
! Placement algorithms
9Dynamic Partitioning: Placement Algorithms
First Next Best
4MB
8MB
! Use most suitable
16 8MB MB 16MB 16MB
6MB
4MB 4MB 4MB
partition for process
8MB 8MB 8MB
4MB
6MB 6MB 6MB 6MB
16MB 16MB 16MB
! Typical algorithms
8MB 8MB 8MB
! First fit 4MB
8MB 8MB
128MB 128MB 128MB
8MB
! Next fit
6MB
16MB 16MB
8MB
! Best fit
16MB
32MB 32MB
32MB
Dynamic Partitioning: Placement Algorithms
First Best
4MB
8MB
! Use most suitable
8MB
12MB
6MB
4MB 4MB
partition for process
4MB
6MB
16MB 16MB
! Typical algorithms
! First fit
8MB 8MB
128MB 128MB
12MB 12MB
! Next fit
16MB 16MB
! Best fit
32MB 32MB
10MB
12MB
10MB
10Dynamic Partitioning: Placement Algorithms
! Comparison of First fit, Next fit and Best fit
! Example is naturally artificial
! First fit
! Simplest, fastest of the three
! Typically the best of the three
! Next fit
! Typically slightly worse than first fit
! Problems with large segments
! Best fit
! Slowest
! Creates lots of small free blocks
! Therefore typically worst
Buddy System
Process 32kB
Process
Pr 128kB ocess
! Mix of fixed and dynamic Process
256kB
128kB
128kB
partitioning
256kB
Proc32k essB 32kB
64kB
k
32kB
! Partitions have sizes 2 ,
128kB
64kB
L ≤ k ≤ U
512kB
Process
256kB
! Maintain a list of holes with
256kB
sizes
1MB
! Assign a process
Process
256kB
! Find smallest k so that
256kB
k
process fits into 2
k
512kB
! Find a hole of size 2
! If not available, split smallest
256kB
k
hole larger than 2
! Split recursively into halves
k
until two holes have size 2
11Memory use within a process
PCB
! Memory needs of
program
known size
Program
Initialized global
! Program code
variables (data)
! Global variables
Uninitialized global vars
Uninitialized global
! Memory needs of
data
variables
Process
unknown size
! Dynamically allocated
memory
! Stack
stack
! Several in multithreaded
programs
Possibly stacks for more threads
Memory Addressing
! Addressing in memory
program
! Addressing needs are
Initialized global
determined during
variables
programming
Uninitialized global vars
! Must work independently
of position in memory
! Actual physical address
are not known
12Memory Addressing
PCB
! Addressing in memory
program
! Addressing needs are
determined during
programming
! Must work independently
data
of position in memory
! Actual physical address
are not known
stack
Memory Management
PCB
program
! Addressing
! Covered address
data
translation and virtual
memory
program
! Important now data
! Translation is necessary
data
! Therefore possible to
have several parts
! Pages
stack
! Segments
13Paging
! Paging
Process 4
Process 1
P P Pr r roce oce ocess 1 ss 3 ss 5
Process 2
! Equal lengths
! Determined by processor
! One page moved into one
memory frames
! Process is loaded into
several frames
! Not necessarily consecutive
! No external fragmentation
! Little internal fragmentation
! Depends on frame size
Segmentation
! Segmentation
! Different lengths
! Determined by programmer
! Memory frames
! Programmer (or compiler toolchain) organizes program in
parts
! Move control
! Needs awareness of possible segment size limits
! Pros and Cons
! Principle as in dynamic partitioning
! No internal fragmentation
! Less external fragmentation because on average smaller segments
14Paging and Segmentation
! Typical for paging and ! Simple paging and segmentation
swapping ! Without virtual memory and
protection
! Address translation
! Can be implemented
! At execution time
! by address rewriting at load time
! With processor support
! by jump tables setup at load time
(“part 2”,
Code part 1
offset in part 2)
Lookup
table
+
Simplified
Address translation
Code part 2
Other needs
PCB
program
! Protection of process
program
from itself
data
! (stack grows into heap)
! Protection of processes
stack
from each other
data
! (write to other process) program
data
Solutions to protection
stack
with
stack
Address Translation
15Summary of Memory Management Algorithms
! Algorithms
! Paging and segmentation
! To be extended in address translation and virtual memory lectures
! Placement algorithms for partitioning strategies
! Mostly obsolete for system memory management
! since hardware address translation is available
! But still necessary for managing
! kernel memory
! memory within a process
! memory of specialized systems (esp. databases)
! Address translation solves
! Solves addressing in a loaded program
! Hardware address translation
! Supports protection from data access
! Supports new physical memory position after swapping in
! Virtual memory provides
! Provide larger logical than physical memory
! Selects process, page or segment for removal from physical memory
16