OS UNIT III Part-1 & 2 (chp-8 & 9)
OS UNIT III Part-1 & 2 (chp-8 & 9)
• Background(8.1)
• Swapping (8.2)
• Contiguous Memory Allocation(8.3)
• Paging(8.4)
• Structure of the Page Table(8.5)
• Segmentation(8.6)
Objectives
• Fixed-Sized Partition(Static)
• In the fixed-sized partition, the memory is divided into fixed-sized blocks and each
block contains exactly one process.
• But, the fixed-sized partition will limit the degree of multiprogramming as the number
of the partition will decide the number of processes.
• Variable-Size Partition(Dynamic)
• In the variable size partition method, the operating system maintains a table that
contains the information about all memory parts that are occupied by the processes
and all memory parts that are still available for the processes.
• Initially, the whole memory space is available for the user processes as a large block,
a hole. Eventually, when the processes arrive in the memory, executes, terminates and
leaves the memory you will see the set of holes of variable sizes.
• EX: when file A and file C release the memory allocated to them, creates the holes in
the memory of variable size.
Multiple-partition allocation
● Hole – block of available memory; holes of various size are scattered
throughout memory
● When a process arrives, it is allocated memory from a hole large enough to
accommodate it
● Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
Hardware Support for Relocation and Limit Registers
• In the variable size partition method, the operating system analyses the
memory requirement of the process and see whether it has a memory block
of the required size.
• If it finds the match, then it allocates that memory block to the process. If
not, then it searches the ready queue for the process that has a smaller
memory requirement.
• The operating system allocates the memory to the process until it cannot
satisfy the memory requirement of the next process in the ready queue.
• It stops allocating memory to the process if it does not have a memory block
(hole) that is large enough to hold that process.
• . If the memory block (hole) is too large for the process it gets spilt into two
parts. One part of the memory block is allocated to the arrived process and
the other part is returned to the set of holes.
• When a process terminates and releases the memory allocated to it, the
released memory is then placed back to the set of holes.
• The two holes that are adjacent to each other, in the set of holes, are
merged to form one large hole.
• Now, at this point, the operating system checks whether this newly formed
free large hole is able to satisfy the memory requirement of any process
waiting in the ready queue. And the process goes on.
• The operating system uses either the block allocation list or the bit map to
select the hole from the set of holes.
• Block Allocation List
• Block allocation list maintains two tables. One table contains the entries of
the blocks that are allocated to the various files. The other table contains the
entries of the holes that are free and can be allocated to the process in the
waiting queue.
• Now, as we have entries of free blocks which one must be chosen can be
decided using either of these strategies: first-fit, best-fit, worst-fit strategies.
.
Dynamic Storage Allocation problem
1. First-fit
•Here, the searching starts either at the beginning of the table or from where the
previous first-fit search has ended. While searching, the first hole that is found to
be large enough for a process to accommodate is selected.
2. Best-fit
•This method needs the list of free holes to be sorted according to their size.
Then the smallest hole that is large enough for the process to accommodate is
selected from the list of free holes. This strategy reduces the wastage of memory
as it does not allocate a hole of larger size which leaves some amount of
memory even after the process accommodates the space.
3. Worst-fit
•This method requires the entire list of free holes to be sorted. Here, again the
largest hole among the free holes is selected. This strategy leaves the largest
leftover hole which may be useful for the other process.
8.3.3 Fragmentation
• Fragmentation can either be external fragmentation or internal
fragmentation.
• External fragmentation is when the free memory blocks available
in memory are too small and even non-contiguous
• Internal fragmentation occurs when the process does not fully
utilize the memory allocated to it.
• The solution to the problem of external fragmentation is memory
compaction.
• Here, all the memory contents are shuffled to the one area of
memory thereby creating a large block of memory which can be
allocated to one or more new processes.
• In the below figure, the last process C, B, D are moved downwards
to make a large hole.
Fragmentation (Cont.)
Fragmentation
• The main idea behind the paging is to divide each process in the form
of pages. The main memory will also be divided in the form of
frames.
• One page of the process is to be stored in one of the frames of the
memory. The pages can be stored at the different locations of the
memory but the priority is always to find the contiguous frames or
holes.
• Pages of the process are brought into the main memory only when
they are required otherwise they reside in the secondary storage.
• Different operating system defines different frame sizes. The sizes of
each frame must be equal.
• Considering the fact that the pages are mapped to the frames in
Paging, page size needs to be as same as frame size.
• Address generated by CPU is divided into:
• Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
• Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
● Let us consider that, P2 and P4 are moved to waiting state after some time.
Now, 8 frames become empty and therefore other pages can be loaded in
that empty place. The process P5 of size 8 KB (8 pages) is waiting inside
the ready queue.
● Given the fact that, we have 8 non contiguous frames available in the
memory and paging provides the flexibility of storing the process at the
different places. Therefore, we can load the pages of process P5 in the place
of P2 and P4.
Paging (Cont.)
Free Frames
Before After
allocation allocation
8.4.2 Hardware Support
• Since the page table is paged, the page number is further divided into:
• a 12-bit page number
• a 10-bit page offset
• where p1 is an index into the outer page table, and p2 is the displacement within
the page of the inner page table
• Known as forward-mapped page table
Address-Translation Scheme
64-bit Logical Address Space
3. Bring the desired page into the (newly) free frame; update the
page and frame tables
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Graph of Page Faults Versus The Number of
Frames
First-In-First-Out (FIFO) Algorithm
● Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
● 3 frames (3 pages can be in memory at a time per process)
FIFO Page Replacement
Optimal Algorithm
● Replace page that will not be used for longest period of time
● 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
● Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
● Counter implementation
● Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
● When a page needs to be changed, look at the counters to
determine which are to change
LRU Page Replacement
Counting Algorithms
● MFU Algorithm: based on the argument that the page with the
smallest count was probably just brought in and has yet to be used
9.6 Thrashing