Segmentation is one of the several memory management techniques adopted by operating systems to accomplish the concept of virtual memory. In our previous content, we have discussed other memory management techniques i.e., paging. Paging is close to the operating system’s view of memory. But the segmentation technique has the user’s view of memory.
With segmentation, OS mas the user’s view of memory onto the physical memory. This kind of mapping enables differentiation between local memory and physical memory.
Segmentation in Operating System
What is Segmentation?
The operating system uses many memory management techniques to ensure the smooth running of the system. Segmentation is one of the memory management techniques that allow the system to allocate noncontiguous memory locations to a process.
Segmentation provides the user’s view of the memory. User prefers to view the physical memory as the collection of variable-size segments and also there is no ordering among the segments.
The user-written program includes a set of methods, procedures, or functions. Data structures such as objects, arrays, stacks, variables, etc. are also associated with the programs.
Each module (segment) or data element of the program is referred to by some name. Like, while referring to a program we usually talk of stacks, libraries, and the main program instead of worrying about the memory addresses that these elements and segments occupy. Neither do we have to worry about the sequence in which these elements are stored in the memory.
Length of the all the segments of the program varies from each other and their length totally depends on their purpose in the program.
To identify particular elements within the data segment we use their offset from the beginning of the segment. All these segmentation techniques support the user’s view of the memory.
The logical address is totally the collection of segments of the program. We identify each segment by its name and length. The logical address that OS specifies while executing of the program includes both segment name and offset within the segment.
For convenience the segments are numbered so instead of providing the segment name, the segment number is provided. Thus the logical address generated by the operating system is:
Before execution, the user program is compiled and the compiler automatically generates segments of the program.
Example of Segmentation
For example, when a compiler compiles a normal user program would generate separate segments for the following modules or data elements of the program.
- Global variables
- Heap from which memory allocated
- Stack used by each thread
- Standard C library
Although the logical address that the operating system generates is two dimensional (segment_number, offset). Still, the actual physical memory available with the system is a one-dimensional sequence of bytes.
How Does Segmentation Works?
To map the two-dimensional logical address onto a one-dimensional physical the operating system makes use of the segment table. Each segment table has two entries segment base, and segment limit. The segment base indicates the starting address of the segment in physical memory and the segment limit indicates the length of the corresponding segment.
Now, how does this segment table help map the two-dimensional logical address onto a one-dimensional physical address?
Whenever the processor has to process any segment, it generates a logical address that has segment numbers ‘s’ and offset d.
The provided segment number is used as an index to read the segment table. The offset should always be between 0 and the segment limit. If the offset is between 0 and the segment then the offset is added to the segment base. Adding offset to the segment base provides the physical memory address of the desired byte.
Need of Segmentation
The paging technique is used to divide the process into equal size pages without considering that the relative parts of the process also get divided. Due to this, the operating system has to load more than one page over the frames of the main memory so that the complete code is there in the main memory for execution.
Thus, the paging technique requires loading more pages to execute a particular segment of the code. Because of this, segmentation was introduced where the program is divided into segments. The segment combines the relative code into one single block. Thus, the processor loads the entire segment into the main memory to complete the related code for execution.
Advantages and Disadvantages of Segmentation
- Segmentation avoids internal fragmentation.
- Segmentation is closer to the user’s view of physical memory.
- Compared to a page table, a segment table consumes less space.
- Loading segments into the main memory and removing them after execution is complete leaves holes in the main memory thereby causing external fragmentation.
- Processing the segment table for the execution of each segment causes processing overhead over the operating system.
- To access the process segment from main memory, the system requires two memory access, one memory access to the segment table other main memory increases memory access time.
- We require extra space to store the segment table.
In this way the segmentation allows the operating system to allocate noncontagious memory space to long processes for smooth execution of the process. Segmentation provides the user’s view of physical memory.