Segmentation in Operating system (OS) is a memory management technique and it follows the non-contiguous memory allocation method.
Segmentation meaning and Definition:
Segment is defined as “it is a logical grouping of instruction such as subroutine, array or data area which are not necessarily all of the same sizes are called segments.”
Every program (job/process) is a collection of these segments and each segment is not an equal size (not fixed partition).
The process is divided into modules (segments) for better visualization.
Segmentation is a technique for managing these segments.
Why Segmentation is required?
Segmentation 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. So segmentation is used to improve efficiency of the system.
Segment Table:
The OS search the entire main memory for free space to load a segment. This mapping done by segment table.
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.
Each entry of the segment table includes ‘Segment Number’, ‘Segment Base’ and ‘Segment Limit’
Segment Base: Contains the starting physical address where the segment resides in memory.
Segment Limit: Specifies the limit of the segment.
Click here for Segmentation Structure
Address generated by CPU (Logical Address) is divided into two parts:
I] Segment Number (S):
It is used as an index into the segment table. Number of bits required to represent the segment.
II] Segment Offset (D):
Number of bits required to represent the size of the segment.
Consider following illustration to understand above:
Segment Number | Limit | Base |
0 | 800 | 2000 |
1 | 400 | 2500 |
2 | 1200 | 1500 |
3 | 1500 | 5000 |
Here,
Segment Number – Indicates Segment
Limit – Indicates length or size of segment
Base – Indicates starting address of the segment in main memory
So loading size range in the main memory from base to limit is considered as segment loaded range.
Storage of segment in main memory is indicates as:
For segment ‘0’ = Limit + Base = 2800
For segment ‘1’ = Limit + Base = 2900
For segment ‘2’ = Limit + Base = 3700
For segment ‘3’ = Limit + Base = 6500
Main Memory representation is as:
Segment – 0 |
Segment – 1 |
Segment – 3 |
Segment – 2 |
Advantages of Segmentation:
1] Allows dynamically growing
2] It allows to divide the program into modules which provides better visualization
3] Segment table consumes less space as compared to Page Table in paging
4] It solves the problem of internal fragmentation
5] It support virtual memory
Disadvantages of Segmentation:
1] There is an overhead of maintaining a segment table for each process.
2] The time taken to fetch the instruction is large
3] Segments of unequal size are not suited for swapping