Unit 5. Memory Management
Unit 5. Memory Management
Memory Management
Two important terms considered while loading programs into the partition are
internal fragmentation and external fragmentation.
Fixed Partitioning: - Main memory is divided into multiple partitions of fixed size
at the time of system generation. A process may be loaded into a partition of equal
size or greater size.
1
Variable partitioning:-
3
Free-space Management
1) Bitmap
2) Linked list
Bitmaps:
When memory is assigned dynamically, the operating system must manage
it.
With a bitmap, memory is divided up into allocation units, starting from
few words to several kilobytes.
Corresponding to each allocation unit is a bit in the bitmap, which is 0 if
the unit is free and 1if it is occupied (or vice versa).
Figure below shows part of memory and the corresponding bitmap:
ss
Fig:
(a) A part of memory with five processes and three holes. The tick marks show the
memory allocation units. The shaded regions (0 in the bitmap) are free.
Linked Lists
Another way of keeping track of memory is to maintain a linked list of
allocated and free memory segments, where a segment is either a process or
a hole between two processes.
Each entry in the list specifies a hole (H) or process (P), the address at
which it starts, the length, and a pointer to the next entry.
In this example, the segment list is kept sorted by address. Sorting this way
has the advantage that when a process terminates or is swapped out,
updating the list is straightforward.
A terminating process normally has two neighbors (except when it is at the
very top or very bottom of memory). These may be either processes or
holes, leading to the four combinations shown in fig below.
When the processes and holes are kept on a list sorted by address, several
algorithms can be used to allocate memory for a newly created process (or
an existing process being swapped in from disk). First fit, Best Fit, Worst
fit etc.
Virtual memory
User written error handling routines are used only when an error occurred in
the data or computation.
Certain options and features of a program may be used rarely.
Many tables are assigned a fixed amount of address space even though only
a small amount of the table is actually used.
The ability to execute a program that is only partially in memory would
counter many benefits.
Less number of I/O would be needed to load or swap each user program into
memory.
A program would no longer be constrained by the amount of physical
memory that is available.
Each user program could take less physical memory; more programs could
be run the same time, with a corresponding increase in CPU utilization and
throughput.
6
Paging
7
Address generated by CPU is divided into
Page number (p) -- page number is used as an index into a page table
which contains base address of each page in physical memory.
Page offset (d) -- page offset is combined with base address to define the
physical memory address.
Demand Paging
A demand paging system is quite similar to a paging system with swapping.
When we want to execute a process, we swap it into memory. Rather than
swapping the entire process into memory, however, we use a lazy swapper
called pager.
When a process is to be swapped in, the pager guesses which pages will be
used before the process is swapped out again. Instead of swapping in a
whole process, the pager brings only those necessary pages into memory.
Thus, it avoids reading into memory pages that will not be used in anyway,
decreasing the swap time and the amount of physical memory needed.
Hardware support is required to distinguish between those pages that are in
memory and those pages that are on the disk using the valid-invalid bit
scheme.
Where valid and invalid pages can be checked by checking the bit. Marking
a page will have no effect if the process never attempts to access the page.
While the process executes and accesses pages that are memory resident,
execution proceeds normally.
8
Access to a page marked invalid causes a page-fault trap. This trap is the result of
the operating system's failure to bring the desired page into memory. But page fault
can be handled as following
Swapping
9
Segmentation
10
CPU generates a logical address which contains two parts:
1. Segment Number
2. Offset
The Segment number is mapped to the segment table. The limit of the respective
segment is compared with the offset. If the offset is less than the limit then the
address is valid otherwise it throws an error as the address is invalid.
In the case of valid address, the base address of the segment is added to the offset
to get the physical address of actual word in the main memory.
Advantages
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
11
Difference between paging and segmentation
12
First in First out (FIFO) algorithm
Oldest page in main memory is the one which will be selected for
replacement.
Easy to implement, keep a list, replace pages from the tail and add new
pages at the head.
Replace the page that will not be used for the longest period of time. Use the
time when a page is to be used.
13
Least Recently Used (LRU) algorithm
Page which has not been used for the longest time in main memory is the
one which will be selected for replacement.
Easy to implement, keep a list, replace pages by looking back into time.
14