Segmentation in Operating Systems
In this tutorial, we will be covering segmentation in the Operating System.
Segmentation is another way of dividing the addressable memory. It is another scheme of memory management and it generally supports the user view of memory. The Logical address space is basically the collection of segments. Each segment has a name and a length.
Basically, a process is divided into segments. Like paging, segmentation divides or segments the memory. But there is a difference and that is while the paging divides the memory into a fixed size and on the other hand, segmentation divides the memory into variable segments these are then loaded into logical memory space.
A Program is basically a collection of segments. And a segment is a logical unit such as:
local variable and global variables.
Types of Segmentation
Given below are the types of Segmentation:
Virtual Memory Segmentation
With this type of segmentation, each process is segmented into n divisions and the most important thing is they are not segmented all at once.
- Simple Segmentation
With the help of this type, each process is segmented into n divisions and they are all together segmented at once exactly but at the runtime and can be non-contiguous (that is they may be scattered in the memory).
Characteristics of Segmentation
Some characteristics of the segmentation technique are as follows:
The Segmentation partitioning scheme is variable-size.
Partitions of the secondary memory are commonly known as segments.
Partition size mainly depends upon the length of modules.
Thus with the help of this technique, secondary memory and main memory are divided into unequal-sized partitions.
Need of Segmentation
One of the important drawbacks of memory management in the Operating system is the separation of the user's view of memory and the actual physical memory. and Paging is a technique that provides the separation of these two memories.
User'view is basically mapped onto physical storage. And this mapping allows differentiation between Physical and logical memory.
It is possible that the operating system divides the same function into different pages and those pages may or may not be loaded into the memory at the same time also Operating system does not care about the User's view of the process. Due to this technique system's efficiency decreases.
Segmentation is a better technique because it divides the process into segments.
User's View of a Program
Given below figure shows the user's view of segmentation:
A computer system that is using segmentation has a logical address space that can be viewed as multiple segments. And the size of the segment is of the variable that is it may grow or shrink. As we had already told you that during the execution each segment has a name and length. And the address mainly specifies both thing name of the segment and the displacement within the segment.
Therefore the user specifies each address with the help of two quantities: segment name and offset.
For simplified Implementation segments are numbered; thus referred to as segment number rather than segment name.
Thus the logical address consists of two tuples:
Segment Number is used to represent the number of bits that are required to represent the segment.
Segment offset is used to represent the number of bits that are required to represent the size of the segment.
A Table that is used to store the information of all segments of the process is commonly known as Segment Table. Generally, there is no simple relationship between logical addresses and physical addresses in this scheme.
The mapping of a two-dimensional Logical address into a one-dimensional Physical address is done using the segment table.
This table is mainly stored as a separate segment in the main memory.
The table that stores the base address of the segment table is commonly known as the Segment table base register (STBR)
In the segment table each entry has :
Segment Base/base address:
The segment base mainly contains the starting physical address where the segments reside in the memory.
The segment limit is mainly used to specify the length of the segment.
Segment Table Base Register(STBR)
The STBR register is used to point the segment table's location in the memory.
Segment Table Length Register(STLR)
This register indicates the number of segments used by a program. The segment number s is legal if s<STLR
Given below figure shows the segmentation hardware :
The logical address generated by CPU consist of two parts:
Segment Number(s): It is used as an index into the segment table.
Offset(d): It must lie in between '0' and 'segment limit'.In this case, if the Offset exceeds the segment limit then the trap is generated.
Thus; correct offset+segment base= address in Physical memory
and segment table is basically an array of base-limit register pair.
Advantages of Segmentation
The Advantages of the Segmentation technique are as follows:
In the Segmentation technique, the segment table is mainly used to keep the record of segments. Also, the segment table occupies less space as compared to the paging table.
There is no Internal Fragmentation.
Segmentation generally allows us to divide the program into modules that provide better visualization.
- Segments are of variable size.
Disadvantages of Segmentation
Some disadvantages of this technique are as follows:
Maintaining a segment table for each process leads to overhead
This technique is expensive.
The time is taken in order to fetch the instruction increases since now two memory accesses are required.
Segments are of unequal size in segmentation and thus are not suitable for swapping.
- This technique leads to external fragmentation as the free space gets broken down into smaller pieces along with the processes being loaded and removed from the main memory then this will result in a lot of memory waste.
Example of Segmentation
Given below is the example of the segmentation, There are five segments numbered from 0 to 4. These segments will be stored in Physical memory as shown. There is a separate entry for each segment in the segment table which contains the beginning entry address of the segment in the physical memory( denoted as the base) and also contains the length of the segment(denoted as limit).
Segment 2 is 400 bytes long and begins at location 4300. Thus in this case a reference to byte 53 of segment 2 is mapped onto the location 4300 (4300+53=4353). A reference to segment 3, byte 85 is mapped to 3200(the base of segment 3)+852=4052.
A reference to byte 1222 of segment 0 would result in the trap to the OS, as the length of this segment is 1000 bytes.