Segmentation and Paging & Page Replacement
Segmentation and Paging & Page Replacement
SEGMENTATION
In Operating Systems, Segmentation is a memory management technique in which, the memory is divided into the
variable size parts. Each part is known as segment which can be allocated to a process.
The details about each segment are stored in a table called as segment table. Segment table is stored in one (or many)
of the segments.
Segment table contains mainly two information about segment:
1. Base: It is the base address of the segment
2. Limit: It is the length of the segment.
Till now, we were using Paging as our main memory management technique. Paging is more close to Operating system
rather than the User. It divides all the process into the form of pages regardless of the fact that a process can have
some relative parts of functions which needs to be loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide the same function into different pages
and those pages may or may not be loaded at the same time into the memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each segment contain same type of
functions such as main function can be included in one segment and the library functions can be included in the other
segment,
If segment offset is found to be greater than or equal to the limit, it throws an error as the address is invalid.
If segment offset is found to be smaller than the limit, then request is treated as a valid request.
The segment offset must always lie in the range [0, limit-1],
Then, segment offset is added with the base address of the segment.
The result obtained after addition is the address of the memory location storing the required word.
Advantages of Segmentation
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
9 Page table is used to maintain the page Segment Table maintains the segment information
information.
10 Page table entry has the frame number and some Segment table entry has the base address of the
flag bits to represent details about pages. segment and some protection bits for the segments.
PAGING
Paging is a non-contiguous memory allocation technique. The logical address generated by the CPU is translated into
the physical address using the page table.
Each process is divided into parts where size of each part is same as page size.
The size of the last part may be less than the page size.
The pages of process are stored in the frames of main memory depending upon their availability.
Page Table
Page Table Base Register (PTBR) provides the base address of the page table.
The base address of the page table is added with the page number referenced by the CPU.
It gives the entry of the page table containing the frame number where the referenced page is stored.
1. Frame Number-
Frame number specifies the frame where the page is stored in the main memory.
The number of bits in frame number depends on the number of frames in the main memory.
If the required page is not present in the main memory, then it is called as Page Fault.
A page fault requires page initialization.
The required page has to be initialized (fetched) from the secondary memory and brought into the main
memory.
3. Protection Bit-
4. Reference Bit-
Reference bit specifies whether that page has been referenced in the last clock cycle or not.
If the page has been referenced recently, then this bit is set to 1 otherwise set to 0.
NOTE
6. Dirty Bit-
Before replacing the modified page with some other page, it has to be written back in the secondary memory
to avoid losing the data.
Dirty bit helps to avoid unnecessary writes.
This is because if the page is not modified, then it can be directly replaced by another page without any need
of writing it back to the disk.
Example-
Depending upon the availability, these pages may be stored in the main memory frames in a non-contiguous
fashion as shown-
Following steps are followed to translate logical address into physical address-
Step-01:
1. Page Number
2. Page Offset
Step-02:
Page Table provides the corresponding frame number (base address of the frame) where that page is stored
in the main memory.
Step-03:
The frame number combined with the page offset forms the required physical address.
Frame number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
Diagram-
The following diagram illustrates the above steps of translating logical address into physical address-
Advantages-
The advantages of paging are-
Disadvantages-
The page replacement algorithm decides which memory page is to be replaced. The process of replacement is
sometimes called swap out or write to disk. Page replacement is done when the requested page is not found in the main
memory (page fault).
There are two main aspects of virtual memory, Frame allocation and Page Replacement. It is very important to have
the optimal frame allocation and page replacement algorithm. Frame allocation is all about how many frames are to be
allocated to the process while the page replacement is all about determining the page number which needs to be
replaced in order to make space for the requested page.
1. if the number of frames which are allocated to a process is not sufficient or accurate then there can be a problem of
thrashing. Due to the lack of frames, most of the pages will be residing in the main memory and therefore more page
faults will occur.
However, if OS allocates more frames to the process then there can be internal fragmentation.
2. If the page replacement algorithm is not optimal then there will also be the problem of thrashing. If the number of
pages that are replaced by the requested pages will be referred in the near future then there will be more number of
swap-in and swap-out and therefore the OS has to perform more replacements then usual which causes performance
deficiency.
Therefore, the task of an optimal page replacement algorithm is to choose the page which can limit the thrashing.
There are various page replacement algorithms. Each algorithm has a different method by which the pages can be
replaced.
1. Optimal Page Replacement algorithm → this algorithms replaces the page which will not be referred for so
long in future. Although it can not be practically implementable but it can be used as a benchmark. Other
algorithms are compared to this in terms of optimality.
2. Least recent used (LRU) page replacement algorithm → this algorithm replaces the page which has not
been referred for a long time. This algorithm is just opposite to the optimal page replacement algorithm. In this,
we look at the past instead of staring at future.
3. FIFO → in this algorithm, a queue is maintained. The page which is assigned the frame first will be replaced
first. In other words, the page which resides at the rare end of the queue will be replaced on the every page
fault.
Q. Consider a main memory with five page frames and the following sequence of page references: 3, 8, 2, 3,
9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. which one of the following is true with respect to page replacement policies First-
In-First-out (FIFO) and Least Recently Used (LRU)?
Solution:
Number of frames = 5
FIFO
According to FIFO, the page which first comes in the memory will first goes out.
LRU
According to LRU, the page which has not been requested for a long time will get replaced with the new
one.
Number of Page Faults = 9
Number of Hits = 6
The Number of page faults in both the cases is equal therefore the Answer is option (A).