Segmentation is another way of dividing the addressable memory. It is another scheme of memory management and it generally supports the user's view of memory. The Logical address space is the collection of segments. Each segment has a name and a length.
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 various segments which are then loaded into logical memory space.
A Program is a collection of segments.
Segmentation divides processes into smaller subparts known as modules. The divided segments need not be placed in contiguous memory. Since there is no contiguous memory allocation, internal fragmentation does not take place. The length of the segments of the program and memory is decided by the purpose of the segment in the user program.
We can say that logical address space or the main memory is a collection of segments.
Need of Segmentation
Segmentation came into existence because of the problems in the paging technique. In the case of the paging technique, a function or piece of code is divided into pages without considering that the relative parts of code can also get divided. Hence, for the process in execution, the CPU must load more than one page into the frames so that the complete related code is there for execution. Paging took more pages for a process to be loaded into the main memory. Hence, segmentation was introduced in which the code is divided into modules so that related code can be combined in one single block.
Other memory management techniques have also an important drawback - the actual view of physical memory is separated from the user's view of physical memory. Segmentation in OS helps in overcoming the problem by dividing the user's program into segments according to the specific need.
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. of Segmentation
Common Segments of a Process
The current process (or the program in execution) has text, data and system data segments. Let us learn more about these segments.
Text Segment: The text segment contains the code of the program. Text Segment is also known as Instruction Segment, and it is a read-only code located in the address space segment of a program. Text Segments are initialized from the executable file of the program.
Data or User Data Segment: The data segment contains four areas of data namely - initialized data, non-initialized data, heap area, and stack area. a. The initialized section of data contains strings that are initialized from the program executable file. b. The non-initialized section of data contains global variables. c. The heap area is an area created by the process to store the global variables. The heap area is also known as Malloc Area. d. The stack area contains local variables, function parameters, and function calls. The stack area grows and shrinks as per function calls. If a function is called, the stack area is increased and when a function returns, the stack area is decreased.
System Data Segment: System Data Segment contains details of a segment like - the current directory of the process, user id (UID) of the process, group ids of the process (GIDs), size of text, data and stack, etc. One thing to note here is that the data segment part is maintained by the system and is accessible by the process only via system calls.
Advantages of Segmentation
No internal fragmentation is there in segmentation.
Segment Table is used to store the records of the segments. The segment table itself consumes small memory as compared to a page table in paging.
Segmentation provides better CPU utilization as an entire module is loaded at once.
Segmentation is near to the user's view of physical memory. Segmentation allows users to partition the user programs into modules. These modules are nothing but the independent codes of the current process.
The Segment size is specified by the user but in Paging, the hardware decides the page size.
Segmentation in OS can be used to separate the security procedures and data.
Disadvantages of Segmentation
During the swapping of processes the free memory space is broken into small pieces, which is a major problem in the segmentation technique.
Time is required to fetch instructions or segments.
The swapping of segments of unequal sizes is not easy.
There is an overhead of maintaining a segment table for each process as well.
When a process is completed, it is removed from the main memory. After the execution of the current process, the unevenly sized segments of the process are removed from the main memory. Since the segments are of uneven length it creates unevenly sized holes in the main memory. These holes in the main memory may remain unused due to their very small size.