Segmentation in OS
Dividing the physical memory space into multiple blocks of same or different sizes is called Segmentation in OS. Each block has some length which is it’s segment. Starting address of the segment is the base address and The length of the segment tells the memory size of that segment.
Segment table contains mainly two values about 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 OS rather than the User. As in the concept of paging a process is divided into equal sized pages. It may divide the same function into different pages and those pages may or may not be loaded at the same time into the main memory. Let explain with example
Suppose, there is an Add () function which is divided in two different pages and these two pages are loaded in frame 3 and 4.Let suppose as CPU execute F3 and page fault occur and F4 replaced with any other frame then ADD() function is not executed completely . In this way efficiency of system is reduced.
So, this problem is overcome through segmentation. In segmentation, Each segment contains same type of functions such as main(), 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 required segmented memory then the actual address of data is calculated by adding the base address of the segment with offset value.
The distance between the base address and actual position of data in segment is known as displacement or offset value.
The Segment number from logical address is mapped to the segment table in main memory. The offset of the respective segment is compared with Limit. If the offset is less than limit then it is a valid address otherwise it return an invalid address.
If the address is valid then Physical address can be calculated by the addition of base address and offset of respective segment from ?main memory.
In diagram given below, we will use the short forms as, S is Segment No, d is offset and f is Frame No.
We can fetch 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 from 250 to 300 memory locations are fetched. And if displacement value is 80 then 250 to 330 values are fetched explanation is given in the diagram below,
Let suppose we have two process P1 and P2 at secondary memory with 5 and 4 segments respectively
Values of each segment will be like that as show in the below diagram
Advantages of Segmentation
- Segment table size is less than page table size.
- No internal fragmentation
- Average Size of Segment is larger than the actual size of page.
- It can have external fragmentation.
- Costly memory management algorithms.