discover yourself
inspire posterity
CHAPTER 4
discover yourself
inspire posterity
4.1 Basic Memory Management
4.2 Swapping
4.3 Virtual Memory
4.4 Page Replacement Algorithms
4.5 Modeling Page Replacement Algorithms
4.6 Design Issues for Paging Systems
discover yourself
inspire posterity
Ideally programmers want memory that is – large
– fast
– non volatile
Memory Hierarchy
– small amount of fast, expensive memory – some medium-speed, medium price
– gigabytes of slow, cheap storage
discover yourself
inspire posterity
Jobs of Memory Manager
1- Keep track of which part of the memory in use and which parts are not in use.
2- To allocate memory to processes when they need it and de-allocate it when they are done.
3- Manage swapping between main
discover yourself
inspire posterity
Memory Management
That move processes back and forth between main memory and disk during execution
That do not move processes back
discover yourself
inspire posterity
The simplest memory abstraction is no abstraction at all. Early mainframe computers (before 1960), early minicomputers (before 1970), and early personal computers (before 1980) had no memory abstraction. Every program simply saw the physical memory. When a program executed an instruction like MOV REGISTER1 , 1000
the computer just moved the contents of physical memory location 1000 to REGISTER 1 .
Thus the model of memory presented to the programmer was simply physical memory, a set of addresses from 0 to some maximum, each address corresponding to a cell containing some number of bits, commonly eight.
discover yourself
inspire posterity
Under these conditions, it was not possible to have two running programs in memory at the same time. If the first program wrote a new value to, say, location 2000, this would erase whatever value the second program was storing there. Nothing would work and both programs would crash almost immediately.
discover yourself
inspire posterity
Three simple ways of organizing memory
Only one process at a time can run.
NO MEMORY ABSTRACTION
The third model was used by early personal computers (e.g., running
discover yourself
inspire posterity
P1
Processes
OS copies the requested program from disk to memory and executes it
finishes
discover yourself
inspire posterity
-
Monoprogramming is hardly used except embedded
systems.
-
Modern systems allow multiprogramming.
-
Multiprogramming increases the CPU utilization.
discover yourself
inspire posterity
Multiprogramming with Fixed Partitions
• Fixed memory partitions
– separate input queues for each partition – single input queue
The easiest way to achieve multiprogramming is to divide memory into unequal
partitions. When a job arrives, it can be put into the input
queue for the smallest partition large enough to hold it.
Whenever a partition becomes free that job in front of the
discover yourself
inspire posterity
Disadvantages of separate input queues for each partition
1- Any space in the partition not used by the job is wasted.
2-Queue for a large partition is empty but the queue for a small partition is full.
discover yourself
inspire posterity
P1
P2
P3
P4 Processes
Swapping
“
Swapping
consists of bringing each process in thediscover yourself
inspire posterity
discover yourself
inspire posterity
Difference between fixed and variable partitions
1- The number, location and size of the partitions vary as processes come and go.
2- Allocating and de-allocating memory is complicated.
discover yourself
inspire posterity
Data Compaction
“When swapping creates multiple holes in memory, it is possible to combine then into one big one by moving all the processes downwards. This technique is called data compaction.”
C B D
Data
discover yourself
inspire posterity
How much memory should be allocated for a process when it is swapped in?
OS allocates exactly what is needed if process is of
fixed size.
If a hole is adjacent to the process, it can be allocated
discover yourself
inspire posterity
What if the process is adjacent to another process ?
1- The growing process will have to be moved to another available hole large enough for it.
2- One or more processes will have to be swapped out to create large enough hole.
discover yourself
inspire posterity
a) Allocating space for growing data segment
b) Allocating space for growing stack & data segment
Memory Management with Bit Maps
discover yourself
inspire posterity
There are two ways to keep track of memory usage.
1- Bitmaps
Memory Management with Bit Maps & Link List
10/07/2020 21
discover yourself
inspire posterity
• Part of memory with 5 processes, 3 holes – tick marks show allocation units – shaded regions are free
Memory Management with Linked Lists
discover yourself
inspire posterity
Dynamic Partitioning
Placement Algorithm
Operating system must decide which free
block to allocate to a process
discover yourself
Placement ALGORITHMS used to allocate memory for newly created process OR existing process swapped in from the disk.
1- First Fit (FF) 2- Next Fit (NF) 3- Best Fit (BF) 4- Worst Fit (WF)
discover yourself
ALGORITHMS
NOTE: Assume that the memory manager knows how much memory to allocate.
1- First Fit (FF)
“Scans memory form the beginning and chooses the first available block that is large enough.”
The memory manager scans along the list of segments until it finds a hole that is
big enough. The hole is then broken up into two pieces, one for the process and one for unused memory.
Next time it is called to find a hole, it starts searching the list from the beginning.
discover yourself
Example:
If a block of size 2 is needed, FF will allocate a hole at_______.
discover yourself
Bin Packing First Fit Algorithm
1 2 3 6 2 3 5 3 4
Each block will be fitted into the first bin that has room for it.
Each block will be fitted into the first bin that has room for it.
A B C D E F
Scan all bins and place item in first bin large enough to hold it
discover yourself
4
The first block fits into the first bin
The first block fits into the first bin
6
A B C D E F
discover yourself
4
The second block fits in the first bin
The second block fits in the first bin
1
2 3
6
2 3
5
3
A B C D E F
discover yourself
4 1
6 2
2
But there is room in the second bin
But there is room in the second bin
A B C D E F
discover yourself
The third bin is the first one the 5 will fit in
The third bin is the first one the 5 will fit in
4 1 2 6 3 5 3 2 3 5 5
A B C D E F
discover yourself
The second bin has room for the 3
The second bin has room for the 3
4 1
6 2
3
5 3
A B C D E F
discover yourself
The fourth bin is the first one with room for the next one
The fourth bin is the first one with room for the next one
4 1 2 6 3 3 2 3 5 2 2 2
A B C D E F
discover yourself
The next one also fits in the fourth bin
The next one also fits in the fourth bin
4 1
6 2
3
5
2
3 3 3
3
A B C D E F
discover yourself
No room until the fifth bin for the 6
No room until the fifth bin for the 6
4 1 6 3 2 3 5 2 3
6 6 6
6
A B C D E F
discover yourself
The 3 has to start a new bin
4 1
2 3
5
2 3
6
3 3 3 3
3
3
A B C D E F