Segmentation in OS
Dividing the physical memory space into multiple blocks of the same or different sizes is called Segmentation in OS. Each block has some length, which is its segment. The starting address of the segment is the base address, and The length of the segment tells the memory size of that segment.
The segment table contains mainly two values about the segment:
- Base Value: It tells about the base address of the segment
- Limit Value: It tells about the size of the segment.
Need of Segmentation
Paging is more favorable to the OS rather than the User. As in the concept of paging, a process is divided into pages of equal size. It may divide the same function into different pages, and those pages may or may not be loaded into the main memory at the same time. Let’s explain with an example
Suppose there is an Add () function that is divided into two different pages, and these two pages are loaded in frames 3 and 4. Let’s suppose as the CPU executes F3 the page fault occurs, and F4 is replaced with any other frame, then the ADD() function is not executed completely. In this way, the efficiency of the system is reduced.
So, this problem is overcome through segmentation. In segmentation, each segment contains the same type of functions, such as main() and ADD() functions.
Logical address to physical address Translation
CPU generates the logical address, which contains two parts.
i. Segment Number: When there is a need to obtain data from the required segmented memory, the actual address of data is calculated by adding the base address of the segment with the offset value.
The distance between the base address and the actual position of data in a segment is known as displacement or offset value.
The Segment number from the logical address is mapped to the segment table in the main memory. The offset of the respective segment is compared with the Limit. If the offset is less than the limit, then it is a valid address; otherwise, it returns an invalid address.
If the address is valid, then the Physical address can be calculated by the addition of the base address and offset of the respective segment from ?main memory.
In the diagram given below, we will use the short forms, S is Segment No, d is offset, and f is Frame No.
We can fetch the whole segment or some part of the segment. If CPU wants to fetch segment 1 then it look at segment table where base address is 250. If its displacement size (d) is 50, then memory locations are fetched from 250 to 300. And if the displacement value is 80, then 250 to 330 values are fetched. An explanation is given in the diagram below,
Let’s suppose we have two processes, P1 and P2, at secondary memory with 5 and 4 segments respectively
Values of each segment will be like that as shown in the below diagram
Advantages of Segmentation
- Segment table size is less than page table size.
- No internal fragmentation
- The average size of the Segment is larger than the actual size of the page.
- It can have external fragmentation.
- Costly memory management algorithms.