Professional Documents
Culture Documents
Memory Management
Topics to be covered:
4.1 Memory address, Swapping and Managing free
memory space
4.2 Resident Monitor
4.3 Multiprogramming with fixed partition
4.4 Multiprogramming with variable partition
4.5 Multiple Base Registers
4.6 Virtual Memory Management
4.6.1 Paging
4.6.2 Segmentation
4.6.3 Paged Segmentation
4.7 Demand Paging
4.8 Performance
4.9 Page Replacement Algorithms
4.10 Allocation of Frames
1
4.11 Thrashing -Anil Verma @ IOE, Pulchow
Memory Management Basics
Cost/bit Cache
increases
Memory
Storage
access
speed Main
increases
Memory
Storage
capacity
decrease
s Secondary
Memory
3
Monoprogramming Model
6
Address Binding
(Relocation)
• Our program is stored in RAM only
• A program has to go through these three phases:
Compilation, Loading and Execution
• The problem that arises is that where should an OS
store the results of programs after execution.
• A user specifies in his instruction where to store the
result.
i.e. x=(a+b)× (a-c)
• Such address given by the user, like x, are called
symbolic or logical address.
• These addresses need to be mapped to real physical
addresses in memory.
• The mechanism is called address binding. 7
Binding of Instructions and Data to Memory
• Compile time
– known memory location
– absolute code can be generated
– must recompile code if starting location
changes.
• Load time
– generate relocatable code if memory location
is not known at compile time.
• Execution time
– process can be moved during its execution
from one memory segment to another.
– need hardware support for address mapping.
8
Logical versus Physical Address
Space
• A logical address space that is bound to a
separate physical address space.
– Logical address: generated by the CPU; also
referred to as virtual address.
– Physical address : address generated by the
memory management 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.
9
Program Relocation
• Refers to the ability to load and execute a given
program into an arbitrary place in the memory.
• Relocation is a mechanism to convert the logical
address into a physical address.
• To do this, there is a special register in CPU called
relocation register.
• Every address used in the program is relocated as:
effective physical address= Logical address + Contents of
Relocation register
• MMU is a special hardware which performs address
binding, uses relocation scheme.
10
Memory Management Unit(MMU)
MMU maps virtual addresses to physical addresses and puts them on memory 11
bus
• Two basic types of
relocation:
– Static Relocation:
Formed during the
loading of the program
into memory by a
loader.
– Dynamic Relocation:
mapping from the
virtual address space to
physical address is
performed at
execution-time.
12
Protection
• Providing security from
unauthorized usage of
memory. Hardware Protection Mechanism
• OS can protect the memory
with the help of base and
limit registers.
• Base register consist of
the starting address of the
next process, the limit
register specifies the
boundary of that job.
• That is why the limit
register is also called a
fencing register.
13
Memory Allocation
Techniques
• Two types:
– Contiguous Storage Allocation
• Fixed Partition Allocation
• Variable Partition Allocation
– Non-Contiguous
• Paging
• Segmentation
14
Contiguous Storage
Allocation
• A memory resident program occupies
a single contiguous block of physical
memory. The memory is partitioned
into block of different sizes to
accommodate the programs. The
partitioning may be:
– Fixed Partition allocation
– Variable Partition allocation
15
Fixed Partition
allocation/Multiprogramming with fixed
partition
• In multiprogramming environment, several
programs reside in primary memory at a time
and the CPU passes its control rapidly between
these programs.
• One way to support multiprogramming is to
divide the main memory into several partitions
each of which is allocated to a single process.
• Depending on how and when the partitions are
created, there may be two types of partitioning:
– Static partitioning
– Dynamic partitioning
16
• Static Partitioning: implies that the
division of memory into number of
partitions and its size is made in the
beginning prior to the execution of
user programs and remains fixed
thereafter.
• Dynamic Partitioning: the size and
the number of partitions are decided
during the execution time by the OS.
17
• Operation Principle:
– We divide the memory into several fixed
size partitions where each partition will
accommodate only one program for
execution. The number of programs (i.e.
degree of multiprogramming) residing in
the memory will be bound by the
number of partitions. When a program
terminates, that partition is freed for
another program waiting in a queue.
18
Multiprogramming with Fixed
Partitions
19
Modeling Multiprogramming
(Probabilistic viewpoint of CPU usage)
Degree of multiprogramming
Example
Let, total memory = 1M =1000K
Memory space occupied by OS = 200K
Memory space taken by an user program =
200K
NOW
Number of processes n= (1000-200)/200 =4
[n= total user space/size of an user program]
CPU utilization = 1- (0.8)4 =60%
22
Modeling Multiprogramming
• Add another 1M memory, then
n= (2000-200)/200 =9
CPU utilization = 1- (0.8)9 =87%
Improvement = (87-60)/60 = 45%
• Again add another 1M, then
n= (3000-200)/200 =14
CPU utilization = 1- (0.8)14 =96%
Improvement = (96-87)/87 = 10%
improvement
Conclusion: addition of last 1M is not logical
23
Advantages of fixed
partition
• Implementation of this allocation
scheme is simple.
• The overhead of processing is also
slow.
• It supports multiprogramming.
• It requires no special costly
hardware.
• It makes efficient utilization of
processor and I/O devices.
24
Disadvantages of fixed
partitioning
• No single program (process) may exceed the
size of the largest partition in a given system.
• It does not support a system having dynamic
data structure such as stack, queue, heap etc.
• It limits the degree of multiprogramming which
in turn may reduce the effectiveness of short-
term scheduling.
• The main problem is the wastage of memory by
programs that are smaller than their partitions.
This is known as Internal Fragmentation.
25
How to run more programs then fit in main
memory at once
26
Variable-partition allocation/multiprogramming
with Dynamic partition
D-IN
A
Job-A Job-D
A-OUT
D
OS OS OS
28
Multiprogramming with
Variable Partitions
.
29
Multiprogramming with
Variable Partitions
• A process can grow if there is an adjacent hole.
• When a process finishes and leaves hole, the hole may not
be large enough to place new job.
OS OS
20K Process-
hole A
Process- Process-
.
A . . B
10K
hole FREE
Process- 20+10+
B 10=40K
FREE
10K
34
Fixed vs. Variable
Partitioning
Fixed
• It the Partitioning
the OS that Variable Partitioning
decides the partition • OS has to decide about
size only once at the partition size, every time a
new process is chosen by
system boot time. long-term scheduler.
• Here, the degree of • Here, the degree of
multiprogramming is multiprogramming will vary
fixed. depending on program
size.
• It leads to internal
• It leads to external
fragmentation. fragmentation.
• IBM-360 DOS and • IBM OS/MFT used this
OS/MFT OS used this approach also.
approach.
35
Drawbacks of Compaction
• Reallocation info must be maintained.
• System must stop everything during
compaction.
• Memory compaction requires lots of CPU
time.
For example:
On a 256MB machine that can copy 4
bytes in 40nses, it takes 2.7sec to
compact all of memory.
36
Swapping
• If there is not enough main memory
to hold all the currently active
processes, the excess processes
must be kept on the disk and brought
in to run dynamically.
• Swapping consists of moving
processes from main memory and
disk.
• Relocation may be required during
swapping. 37
Swapping, a picture
39
Bitmaps-the picture
(a)Picture of memory
(b)Each bit in bitmap corresponds to a unit of storage (e.g. bytes) in memory
(c) Linked list P: process H: hole
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory management With Bitmap
42
Memory management with
Linked Lists
44
Storage placement
Strategies
1. First fit:
The memory manager allocates the first hole
that is big enough. It stops the searching as
soon as it finds a free hole that is large enough.
Advantages: It is a fast algorithm because it
searches as little as possible.
Disadvantages: Not good in terms of storage
utilization.
//Next fit
It works the same way as first fit, except that it keeps track of
where it is whenever it finds a suitable hole.
Storage placement
Strategies
3. Best fit:
– Allocate the smallest hole that is big enough.
– Best fit searches the entire list and takes the
smallest hole that is big enough to hold the new
process.
– Best fit try to find a hole that is close to the actual
size needed.
Advantages: more storage utilization than
first fit.
Disadvantages: slower than first fit because
it requires searching whole list at time.
Storage placement
Strategies
4. Worst fit:
– Allocate the largest hole.
– It search the entire list, and takes the
largest hole, rather than creating a
tinny hole, it produces the largest
leftover hole, which may be more useful.
Advantages: some time it has more
storage utilization than first fit and best
fit.
Disadvantages: not good for both
performance and utilization.
Problem:
In this example Best-fit turns out to be the best,
since it allocates memory for all processes
Paging
• Partition memory into small equal
fixed-size chunks and divide each
process into the same size chunks
• The chunks of a process are called
pages
• The chunks of memory are called
frames
Paging
• Operating system maintains a page
table for each process
– Contains the frame location for each
page in the process
– Memory address consist of a page
number and offset within the page
Processes and Frames
1. System with a number of frames
allocated
2. Process A, stored on disk, consists
A.0 of four pages. When it comes time
to load this process, the operating
A.1 system finds four free frames and
A.2 loads the four pages of process A
into the four frames.
A.3 3. Process B, consisting of three
D.0
B.0 pages, and process C, consisting of
four pages, are subsequently
D.1
B.1 loaded.
B.2
D.2 4. Then process B is suspended and
is swapped out of main memory.
C.0 5. Later, all of the processes in main
C.1 memory are blocked, and the
C.2 operating system needs to bring in
a new process, process D, which
C.3 consists of five pages. The
Operating System loads the pages
D.3
into the available frames and
D.4 updates the page table
Page Table
Pages and Page Frames
• Solutions
– Good page replacement algorithms
– Reduce number of processes running
– Fit more memory
Translation Lookaside Buffer
• Every virtual memory reference
causes two physical memory access
– Fetch page table entry
– Fetch data
• Use special cache for page table
– TLB
TLB and Cache Operation
Segmentation
76
Segmentation
Paging vs Segmentation