Paging and Segmentation in OS
What is Paging in OS?
Paging is a storage technique used for memory management. In paging, the (OS) Operating System retrieves the processes from the secondary memory into the main memory, and the memory is in the form of the pages. In this technique, we split the main memory into the small blocks of physical memory which are called frames. The size of the frames is fixed. In paging, for the maximum usage of the main memory and to prevent external fragmentation, the frame size must be the same as the page size. Paging is a logical concept, and it helps us to access the data faster.
Benefits of Paging
The benefits of Paging are:
- In Paging, there is no need of external fragmentation.
- In Paging, the swapping between the equal-size pages and page frames is easy.
- Paging is a simple technique that we use for memory management.
Drawbacks of Paging
The Drawbacks of Paging are:
- In Paging, there may be a chance of Internal Fragmentation.
- In Paging, the Page table consumes extra memory.
- Due to Multi-level Paging, there may be a chance of memory reference overhead.
What is Paging Protection?
Paging Protection means to protect the paging process by inserting an extra bit, known as valid/Invalid bit.
In paging, memory protection is done by adding the protection bits with each page. With each page table entry, these bits are linked. It also helps to specify the protection on the corresponding page.
Example of Paging
Suppose we have main memory, and the size of the main memory is 16 KB, and the size of the frame is 1 KB. In this, the main memory is split into 16 frames, and each frame is of 1 KB.
In the system, we have four distinct processes, and the processes are A1, A2, A3, and A4, and the size of each process is 4 KB. In this, we split or divide all the pages into the pages of size 1KB so that the OS can store 1page in 1 frame.
When the process started its execution, all the frames were vacant to store process pages in a contiguous manner.
The below figure shows the frames, pages, and the mapping between the frames and the pages.
We can see in the following example that after some time, the process A2 and the process A4 are moved into the waiting state. So, the eight frames will become vacant, and we need to load or put other pages in those vacant blocks. The process A5 is having a size of eight pages (8 KB), which are waiting in the ready queue.
We can see in the following example that we have eight non-contiguous frames that are existing in the memory, with the help of paging, we can store the processes at different places. Due to this, we can load the pages of the A5 process instead of process A2 and Process A4.
What is Segmentation?
Segmentation is a technique of memory management. It is just like the Paging technique except the fact that in segmentation, the segments are of variable length but, in Paging, the pages are of fixed size. In segmentation, the memory is split into variable-length parts. Each part is known as segments. The information which is related to the segment is stored in a table which is called a segment table.
There are two types of information stored in the segment table:
- Limit
- Base
Limit: - The limit is the length or size of the segment
Base: - The base is the base address of the segment.
A segment of the program comprises of the utility function, data structure, and the main function of the program for each process. The operating system preserves a segment map table for mapping. The table consists of segment number, list of the memory blocks which are free along with its size, and its memory location in the virtual memory or the main memory.
Types of Segmentation:
There are two types of Segmentation:
1. Simple Memory Segmentation
2. Virtual Memory Segmentation
Simple Memory Segmentation: - In simple memory segmentation, each process is split into different segments, and at the run time, all the processes are loaded. Also, not all the processes need to be loaded into a contiguous way.
Virtual Memory Segmentation: - As simple memory segmentation, in virtual memory segmentation, each process is split into different segments, but not all of them are residents at any point of time.
Benefits of Segmentation
The benefits of Segmentation are:
- Internal fragmentation is not present in the segmentation.
- Less overhead.
- In segmentation, the segment table size is less than the page table size.
- In segmentation, the relocation of the segment is easier than the whole address space.
Drawbacks of Segmentation
The drawbacks of segmentation are:
- In segmentation, there may be a chance of external fragmentation.
- The segmentation technique is expensive.
- In Segmentation, it is tough to allocate memory in a contiguous manner to a variable-sized partition.
Why Segmentation is needed?
Initially, we used the Paging technique for memory management. But instead of the user, it is closer to the operating system more probably. In paging, all the processes are split into the pages. Sometimes there may be a situation where a process having some relevant sections of the function requires being loaded on the same page.
The operating system does not mind the process from the user’s point of view. The operating system split the same function into the number of pages, and at the same time, the pages may be loaded or not loaded into the memory. Due to this reason, system efficiency could be decreased.
So, it is better to use the segmentation technique for memory management. In segmentation, the process is split into the segments. In this, every segment consists of a similar kind of function as a library function contained in one segment, and the main function contained in the other segment.
Translation of Logical Address into Physical Address by Segment Table
The CPU generates a logical address that comprises of two parts:
- Segment Number
- Segment Offset
Segment Number: - Segment Number is defined as the number of bits that are needed to represent the segment.
Segment Offset: - Segment Offset is defined as the number of bits which are needed to represent the size of the segment.
We map the segment number to the segment table, and after mapping, a comparison is done between the limit of the segment to the offset. If the offset bit is less than the limit, then we can say that the address is valid. Otherwise, the address is invalid.
If the address is valid then we add the base address of the segment to the offset so that we can get the physical address of the actual word in the main memory.
Difference between Paging and Segmentation
Paging | Segmentation |
Paging is a memory management technique. In paging, memory is partitioned into fixed-size pages. | Segmentation is also a memory management technique. In segmentation memory is partitioned into the variable size segments. |
In paging, with the help of hardware page size is determine. | In segmentation, the user gives the size of the segments. |
In paging the OS is accountable for paging. | There is a compiler which is accountable for the segmentation. |
Paging is faster than the segmentation. | Segmentation is slower than the paging. |
In paging, there may be a chance of internal fragmentation. | In segmentation, there may be a chance of external fragmentation. |
In this technique, the logical address is partitioned into the page number and the page offset. | In this technique, the logical address is partitioned into the section number and the section offset. |
In paging, to calculate the absolute address requires page number and the offset. | In segmentation, to calculate the value of absolute address require Segment number and the offset. |
The user cannot see the Paging. | The user can see the segmentation. |
In this technique, the list of free frames is maintained by the operating system. | In this technique, the list of holes is maintained by the operating system. |
The page table is created to maintain page data. | Section table is created to maintain the section data. |
In paging, a page table encloses the base address of each page. | In Segmentation, a segment table encompasses the segment number and the offset. |