Memory Management: Protection
Memory Management: Protection
It is the task carried out by the OS and hardware to accommodate multiple processes in main
memory. If only a few processes can be kept in main memory, then much of the time all
processes will be waiting for I/O and the CPU will be idle. Hence, memory needs to be allocated
efficiently in order to pack as many processes into memory as possible. In most schemes, the
kernel occupies some fixed portion of main memory and the rest is shared by multiple processes.
Fixed Partitioning
Partition main memory into a set of non-overlapping regions called partitions
Partitions can be of equal or unequal sizes
any process whose size is less than or equal to a partition size can be loaded into the
partition
if all partitions are occupied, the operating system can swap a process out of a partition
a program may be too large to fit in a partition. The programmer must then design the
program with overlays: when the module needed is not present the user program must load
that module into the program’s partition, overlaying whatever program or data are there
Main memory use is inefficient. Any program, no matter how small, occupies an entire
partition. This is called internal fragmentation.
Unequal-size partitions lessens these problems but they still remain...
Equal-size partitions was used in early IBM’s OS/MFT (Multiprogramming with a Fixed
number of Tasks)
Dynamic Partitioning
Partitions are of variable length and number
Each process is allocated exactly as much memory as it requires
Eventually holes are formed in main memory. This is called external fragmentation
Must use compaction to shift processes so they are contiguous and all free memory is in
one block
Used in IBM’s OS/MVT (Multiprogramming with a Variable number of Tasks)
Placement Algorithm
Used to decide which free block to allocate to a process
Goal: to reduce usage of compaction (time consuming)
Possible algorithms:
o Best-fit: choose smallest hole
o First-fit: choose first hole from beginning
o Next-fit: choose first hole from last placement
Replacement Algorithm
When all processes in main memory are blocked, the OS must choose which process to
replace
o A process must be swapped out (to a Blocked-Suspend state) and be replaced by a
new process or a process from the Ready-Suspend queue
o We will discuss later such algorithms for memory management schemes using
virtual memory
Buddy System
A reasonable compromise to overcome disadvantages of both fixed and variable
partitioning schemes
A modified form is used in Unix SVR4 for kernel memory allocation
Memory blocks are available in size of 2K where L <= K <= U and where
o 2L = smallest size of block allocatable
o 2U = largest size of block allocatable (generally, the entire memory available)
We start with the entire block of size 2U
When a request of size S is made:
o If 2U-1 < S <= 2U then allocate the entire block of size 2U
o Else, split this block into two buddies, each of size 2U-1
o If 2U-2 < S <= 2U-1 then allocate one of the 2 buddies
o Otherwise one of the 2 buddies is split again
This process is repeated until the smallest block greater or equal to S is generated
Two buddies are coalesced whenever both of them become unallocated
The OS maintains several lists of holes
o the i-list is the list of holes of size 2i
o whenever a pair of buddies in the i-list occur, they are removed from that list and
coalesced into a single hole in the (i+1)-list
Presented with a request for an allocation of size k such that 2i-1 < k <= 2i:
o the i-list is first examined
o if the i-list is empty, the (i+1)-list is then examined...
Relocation
Because of swapping and compaction, a process may occupy different main memory
locations during its lifetime
Hence physical memory references by a process cannot be fixed
This problem is solved by distinguishing between logical address and physical address
Address Types
A physical address (absolute address) is a physical location in main memory
A logical address is a reference to a memory location independent of the physical
structure/organization of memory
Compilers produce code in which all memory references are logical addresses
A relative address is an example of logical address in which the address is expressed as a
location relative to some known point in the program (ex: the beginning)
Address Translation
Relative address is the most frequent type of logical address used in pgm modules (ie:
executable files)
Such modules are loaded in main memory with all memory references in relative form
Physical addresses are calculated "on the fly" as the instructions are executed
For adequate performance, the translation from relative to physical address must by done
by hardware
Simple Paging
Main memory is partition into equal fixed-sized chunks (of relatively small size)
Trick: each process is also divided into chunks of the same size called pages
The process pages can thus be assigned to the available chunks in main memory called
frames (or page frames)
Consequence: a process does not need to occupy a contiguous portion of memory
Page Tables
The OS now needs to maintain (in main memory) a page table for each process
Each entry of a page table consist of the frame number where the corresponding page is
physically located
The page table is indexed by the page number to obtain the frame number
A free frame list, available for pages, is maintained