8.1 Memory Management
8.1 Memory Management
References:
1.Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating system
concepts with Java. Wiley Publishing, 2009.
2.Stallings, William. Operating Systems 5th Edition. Pearson Education India,
2006.
Background
• Every instruction has to be fetched from memory before it can be executed, and most
instructions involve retrieving data from memory or storing data in memory or both.
• Multi-tasking OSes compounds the complexity of memory management, because as
processes are swapped in and out of the CPU, all at high speeds and without
interfering with any other processes.
• Shared memory, virtual memory, the classification of memory as read-only versus
read-write, and concepts like copy-on-write forking all further complicate the issue.
• The CPU can only access its registers and main memory. It cannot, for example,
make direct access to the hard drive, so any data stored there must first be transferred
into the main memory chips before the CPU can work with it.
• Register access in one CPU clock (or less).
• Main memory can take many cycles, causing a stall.
• Cache sits between main memory and CPU registers.
• Protection of memory required to ensure correct operation
Base and Limit Registers
• User processes must be restricted and
access only the memory locations that
"belong" to that particular process.
• A pair of base and limit registers define
the logical address space for each process.
• Every memory access made by a user
process is checked against these two
registers, and if a memory access is
attempted outside the valid range, then a
fatal error is generated.
• Changing the contents of the base and
limit registers is a privileged activity,
allowed only to the OS kernel.
Hardware Address Protection
Address Binding
• Programs on disk, ready to be brought into memory to execute form an input
queue
– Without support, must be loaded into address 0000
• Inconvenient to have first user process physical address always at 0000
– How can it not be?
• Further, addresses represented in different ways at different stages of a program’s
life
– Source code addresses usually symbolic
– Compiled code addresses bind to relocatable addresses
• i.e. “14 bytes from beginning of this module”
– Linker or loader will bind relocatable addresses to absolute addresses
• i.e. 74014
– Each binding maps one address space to another
Binding of Instructions and Data to
Memory
• Address binding of instructions and data to memory addresses can happen at three
different stages
– Compile Time - If it is known at compile time where a program will reside in
physical memory, then absolute code can be generated by the compiler,
containing actual physical addresses.
However if the load address changes at some later time,
then the program will have to be recompiled. DOS .COM programs use
compile time binding.
– Load Time - If the location at which a program will be loaded is not known at
compile time, then the compiler must generate relocatable code, which
references addresses relative to the start of the program. If that starting address
changes, then the program must be reloaded but not recompiled.
– Execution Time - If a program can be moved around in memory during the
course of its execution, then binding must be delayed until execution time. This
requires special hardware, and is the method implemented by most modern
OSes.
Multistep Processing of a User Program
Addresses
Logical
Relative
Physical or Absolute
• Does the swapped out process need to swap back in to same physical addresses?
Swapping…
• Depends on address binding method
– Plus consider pending I/O to / from process memory space
• Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and
Windows)
– Swapping normally disabled
– Started if more than threshold amount of memory allocated
– Disabled again once memory demand reduced below threshold
Memory Management Techniques
Fixed Partitioning
• Equal-size partitions: any process whose size is less than
or equal to the partition size can be loaded into an available
partition
• The operating system can swap out a process if all partitions
are full and no process is in the Ready or Running state
• A program may be too big to fit in a partition, program
needs to be designed with the use of overlays.
• Disadvantage:
– Main memory utilization is inefficient any program,
regardless of size, occupies an entire partition
– The number of partitions specified at system generation
time limits the number of active processes in the system
– internal fragmentation :wasted space due to the block
of data loaded being smaller than the partition
Unequal Size Partitions
• Using unequal size partitions helps lessen the
problems
– programs up to 16M can be accommodated
without overlays
– partitions smaller than 8M allow smaller
programs to be accommodated with less internal
fragmentation
Memory assignment for fixed partitioning
Dynamic Partitioning
• Partitions are of variable length and
number
• Process is allocated exactly as much
memory as it requires
• This technique was used by IBM’s
mainframe operating system, OS/MVT
Disadvantage:
• External Fragmentation: memory
becomes more and more fragmented,
memory utilization declines
• Compaction: technique for
overcoming external fragmentation
• OS shifts processes so that they are
contiguous free memory is together in
one block.
• time consuming and wastes CPU time
Placement Algorithms
• begins to
• begins to scan memory • Allocate the
scan memory from the
• chooses the largest
from the location of
block; must
block that is beginning the last
also search
closest in and chooses placement
entire list
size to the the first and chooses • Produces the
request available the next
largest
block that is available
leftover hole
large enough block that is
large enough
Question?
• Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb, 600Kb (in order),
how would the first-fit, best-fit, and worst-fit algorithms place processes of 212 Kb,
417 Kb, 112 Kb, and 426 Kb (in order)? Which algorithm makes the most efficient
use of memory?
• First-fit:
– 212K is put in 500K partition
– 417K is put in 600K partition
– 112K is put in 288K partition (new partition 288K = 500K - 212K)
– 426K must wait
• Best-fit:
– 212K is put in 300K partition
– 417K is put in 500K partition
– 112K is put in 200K partition
– 426K is put in 600K partition
• Worst-fit:
– 212K is put in 600K partition
– 417K is put in 500K partition
– 112K is put in 388K partition
– 426K must wait
Question?
• Exercise: Consider the requests from processes in given order 300K, 25K, 125K
and 50K. Let there be two blocks of memory available of size 150K followed by a
block size 350K.
Which of the following partition allocation schemes can satisfy above requests?
A) Best fit but not first fit.
B) First fit but not best fit.
C) Both First fit & Best fit.
D) neither first fit nor best fit.
https://www.geeksforgeeks.org/partition-allocation-methods-in-memory-management/?ref=rp
Buddy System
• Comprised of fixed and dynamic partitioning schemes
• Space available for allocation is treated as a single block
• Memory blocks are available of size 2K words, L ≤ K ≤ U, where
• 2L = smallest size block that is allocated
• 2U = largest size block that is allocated; generally 2 U is the
size of the entire memory available for allocation
Example of buddy system
Tree representation of buddy system
Paging
• Physical address space of a process can be noncontiguous; process is allocated
physical memory whenever the latter is available
– Avoids external fragmentation
– Avoids problem of varying sized memory chunks
• Partition memory into equal fixed-size blocks that are relatively small
• Process is also divided into small fixed-size blocks of the same size
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames and load program
• Still have Internal fragmentation
Pages Frames
• Available blocks of
• Blocks of a process
memory
Page Table
• Maintained by operating system for each process
• Contains the frame location for each page in the process(translate logical to
physical addresses)
• Processor must know how to access the page table for the current process
• Used by processor to produce a physical address
Question?
• (Gate 2015) Consider a system with byte-addressable memory, 32−bit logical
addresses, 4 kilobyte page size and page table entries of 4 bytes each. The size of
the page table in the system in megabytes is
Gateoverflow.in
Internal Fragmentation
• Calculating internal fragmentation
– Page size = 2,048 bytes
– Process size = 72,766 bytes
– 35 pages + 1,086 bytes
– Internal fragmentation of 2,048 - 1,086 = 962 bytes
– Worst case fragmentation = 1 frame – 1 byte
– On average fragmentation = 1 / 2 frame size
– So small frame sizes desirable?
• The processor used in the computer has a 1 MB 16 way set associative virtually
indexed physically tagged cache. The cache block size is 64 bytes. What is the size
of a page in KB in this computer?
gateoverflow.in
gateoverflow.in
Hashed Page Tables
• Common in address spaces > 32 bits
• The virtual page number is hashed into a page table
– This page table contains a chain of elements hashing to the same location
• Each element contains (1) the virtual page number (2) the value of the mapped
page frame (3) a pointer to the next element
• Virtual page numbers are compared in this chain searching for a match
– If a match is found, the corresponding physical frame is extracted
• Variation for 64-bit addresses is clustered page tables
– Similar to hashed but each entry refers to several pages (such as 16) rather than
1
– Especially useful for sparse address spaces (where memory references are
non-contiguous and scattered)
Hashed Page Table
Inverted Page Table
• Rather than each process having a page table and keeping track of all possible
logical pages, track all physical pages
• One entry for each real page of memory
• Entry consists of the virtual address of the page stored in that real memory location,
with information about the process that owns that page
• Decreases memory needed to store each page table, but increases time needed to
search the table when a page reference occurs
• Use hash table to limit the search to one — or at most a few — page-table entries
– TLB can accelerate access
• But how to implement shared memory?
– One mapping of a virtual address to the shared physical address
Inverted Page Table Architecture