The concept of segmentation in os is similar to paging which is used for memory management. Segmentation divides the user program and the data associated with the program into the number of segments. Well, there is a difference between paging and segmentation in paging each page has an equivalent fixed size whereas segments are of variable size.
The segmentation maps to the user’s view onto the actual physical memory. In the section further, we will discuss segmentation in brief along with its workflow. Further, to understand segmentation easily we will discuss an example on segmentation and we will wind up the topic with advantage snd disadvantages.
Content: Segmentation in Operating System
- What is Segmentation?
- Implementation of Segmentation
- Example of Segmentation
- Advantages and Disadvantages
- Key Takeaways
What is Segmentation in OS?
Segmentation is used for memory management where user’s view is plotted onto the physical memory. In segmentation, the user program is divided into the number of segments where each segment is of variable size.
Segmentation may appear to you as a dynamic partitioning the where divided segments are of variable size. These segments need not be placed in contiguous memory. Thus segmentation does not create internal fragmentation but it may create external fragmentation.
From the user’s view, a program has segments such as the main method, with a set of objects, methods, arrays, procedures, global variables, templates etc. So these modules or data elements are the segments to which user refers it by its name without concerning at what address these modules or elements are stored in memory. Neither the user is worried about in which sequence the modules or data elements are stored in memory.
Each segment of the user program is of variable length and the length of the segment is decided by the purpose of the segment in the user program. Now, in logical addressing, the elements in a particular segment are identified by their offset value starting from the beginning of the segment. The logical address of a segment is always known by the two entities segment name and the offset. The segments created from a user program are numbered and for simplicity, each segment is referred to as by the segment number instead of the segment name. So, now the logical address has two tuples.
<segment-number, offset>
Implementation of Segmentation
Let us now see the implementation of segmentation. As we know in segmentation the user program is divided into the number of segments. And a segment is identified by two components i.e. a segment name and segment offset.
In this section, we will see how this two-dimensional address is mapped onto the one-dimensional physical memory. Like we have page table in paging similarly we have a segment table in segmentation.
Segmentation Table
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 and Disadvantages
Advantages of Segmentation
- Segmentation is more close to the programmer’s view of physical memory.
- Segmentation prevents internal fragmentation.
- Segmentation prevents the CPU overhead as the segment contain an entire module of at once.
Disadvantages of Segmentation
- The segmentation leads to external fragmentation.
Key Takeaways
- Segmentation used for memory management.
- The concept of segmentation is close to the user’s view of physical memory.
- Segmentation divides the user program into variable-length segments where the length of the segments depends upon its purpose in the program.
- Segmentation includes a segment table which is an array of the base address and limits where base address denotes the location of a particular segment in the physical memory and limit denote the segment length.
- Segmentation leads to external fragmentation.
So, this is all about segmentation and how it forms the segments of the user program. We have seen the implementation and working of segmentation along with an example and how it reduces the overhead of CPU.
Leave a Reply