What is Segmentation in Operating System
What is Segmentation Advantage and Disadvantage
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, they are given below
- Base: It is the base address of the segment
- Limit: It is the length of the segment.
The segment table has two entries for each segment i.e. segment base address and segment limit. The segment base address denotes the starting address where the segment is stored in the memory while the segment limit denotes the length of the segment. Let us understand the workflow of the segment table with the help of the figure given below.
As in the figure above you can see that the CPU calls outs for a segment and logically provides a two-dimensional address for that segment where the first entity s is the segment number (segment name) and its offset d.
Now the segment number would help to find two more details regarding the segment number in segment table i.e. segments base address and its limit. On the other hand, the offset provides by CPU is verified whether it is between 0 and the limit of called segment if yes then the offset is added to the base address of called segment to retrieve the physical address of the segment. If the offset doesn't verify the condition the operating system is trapped with addressing error i.e. the logical address tries to attempt beyond the segment limit.
Example of Segmentation in OS
Consider that a user program has been divided into five segments and they are numbered from segment 0 to segment 4, as you can see them in the logical address space. You also have a segment table which has entries for these segment with their base address in physical memory and their limit.
Now suppose the CPU calls for segment number 2 which is 400 bytes long and it resides at 4300 memory location. The CPU wants to refer the 53rd byte of segment 2. So, here the input we get from CPU is segment number 2 and 53 as an offset.
Now the offset is in between 0 and limit of the segment 2 i.e. 400. So, the condition got verified and the offset is being added to the base address of the segment 2 to reach the 53rd byte of the segment 2 in physical memory. In case you try to access the 453rd byte of segment 2 then it would result in a trap to the operating system as offset value 453 is greater than the limit of segment 2.
Advantages of Segmentation
- No internal fragmentation
- Average Segment Size is larger than the actual page size.
- Less overhead
- It is easier to relocate segments than entire address space.
- The segment table is of lesser size as compare to the page table in paging.
Disadvantages of Segmentation
- It can have external fragmentation.
- it is difficult to allocate contiguous memory to variable sized partition.
- Costly memory management algorithms.