Table of contents
1.
Introduction
2.
Why is Segmentation in OS required?
3.
Types of Segmentation in Operating System
3.1.
1. Virtual Memory Segmentation
3.2.
2. Simple Segmentation
4.
How Does Segmentation Work?
5.
What is Segment Table?
6.
Translation of Logical address into physical address by segment table
7.
Characteristics of Segmentation in OS
8.
Advantages of Segmentation in OS
9.
Disadvantages of Segmentation in OS
10.
Example of Segmentation
11.
Architecture Segmentation in Operating System
12.
Frequently Asked Questions
12.1.
What is segmentation and paging?
12.2.
What is segmentation and fragmentation in OS?
12.3.
Where is segmentation used in OS?
12.4.
What is the difference between segmentation and partitioning in OS?
13.
Conclusion
Last Updated: Apr 1, 2024
Medium

Segmentation in OS (Operating System)

Author ANKIT KUMAR
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Segmentation in OS is a memory management technique in which the process is divided into logically related partitions or segments and then allocated to the memory.

Each segment represents a different portion of a program or process, such as code, data, or stack. Memory may be used more effectively when it is segmented and organized and allocated according to the requirements of individual processes.

Each segment in segmentation is given a specific number or identity along with its size. The operating system maintains a segment table that contains the base address and length of each segment in the memory. When a program is loaded into memory, it is separated into segments, and the segment table is updated accordingly.

segmentation in os

The segments are allocated in a non-contiguous manner in the physical memory. Unlike pages, the segments can be of variable size.

Why is Segmentation in OS required?

In paging, the process was divided into pages of equal size. In doing so, it may be possible that some part of the function or code in the process may be on one page and some on other. Consider a simple C program, where there is the main function, an add function to add two numbers, and a display function to run a loop and print the first ten natural numbers.

When the process is divided into pages, it may be possible that the add function may be divided into various pages. Also, the logic in the loop may be contained in more than one page. Because of this, the entire function will not be loaded at once. This is where segmentation comes into the picture. In segmentation, the entire process will be divided into logically related segments such that one segment will have the entire main function, the other will have the entire add function, and so on.

Segmentation gives greater control to the user.

Types of Segmentation in Operating System

There are two types of segmentation in the operating system:

1. Virtual Memory Segmentation

Virtual memory segmentation divides processes into n chunks. Not all of the segments are separated at the same time. The execution time of a program may or may not include virtual memory segmentation.

2. Simple Segmentation

Despite the fact that the segmentation is done all at once, Simple Segmentation breaks the operations into n segments. Simple segmentation occurs during the execution of a program. Because simple segmentation scatters the segments into memory (in a noncontinuous manner), one segment of the process may be located in a different location than another.

How Does Segmentation Work?

Segmentation in operating systems is a memory management technique that divides a process into segments of variable sizes, each representing a logical unit like a function or data structure. Unlike paging, segmentation doesn't require equal-sized blocks. When a process is loaded into memory, segments are allocated dynamically based on its requirements. This approach facilitates efficient memory utilization and enables sharing of memory segments among processes. Each segment is tracked in a segment table, storing its base address and length. Segmentation enhances memory access efficiency and supports multitasking environments by allowing flexible memory allocation.

What is Segment Table?

In order to know which segment is present at which address in the physical memory, every process has its segment table. The segment table is one of the most important parts of the segmentation. It helps in mapping the logical address generated by the CPU to the physical address of the memory.

Segment Number Base Limit
0 6000 600
1 1500 300
2 400 500
3 2837 100

The segment table consists of:

1. Segment Base- The base represents the starting address of the segment in the physical memory.

2. Segment Limit- Limit determines the size of the segment. Suppose the limit of a segment is 600 bytes, and the base address of the segment in the memory is 4000. We have assumed the memory to be byte-addressable. Therefore the last byte of the segment will be at the 4599th byte in the physical memory. 

Translation of Logical address into physical address by segment table

CPU (Central Processor Unit)  produces the logical address, but this is just a virtual address that is not present in our memory, so we need to translate the logical to a physical address. Translation of logical (or virtual) address into physical address is done by a unit called MMU (Memory Management Unit) which checks the range and permissions, and this process is programmed by OS.

Here is the diagram below to understand the process:

Translation logical to physical address

In the above diagram, The CPU produces the logical address, which then converts to a physical address using MMU (Memory Management Unit). Now this physical address can be used in memory for the assignment of processes.

Here is the diagram of the architecture of the segmentation that shows how logical address is used:

Architecture of Segmentation

CPU produces the logical address, which has 2 parameters that are segment number, shows the specific segment, and the segment offset shows the size of that segment. Then OS searches this segment number in the segment table, and MMU does computation to check the offset, where offset should be lesser than or equal to the limit of that segment because only then that segment can be accessed. If the condition gets true, the segment can be accessed. Here the physical address will be (base + D). 

For example, If we search for base 1500, which is a segment (S - 5), and D is 200. Then MMU does a computation where D should be lesser than or equal to the limit; here, D is 200, which is lesser than the limit, which is 300. So this segment can be accessed, and the physical address will be (base + D), which is 1500 + 200 = 1700. So the accessing will start from the memory index 1500 to 1700.

Characteristics of Segmentation in OS

The segmentation technique has the following characteristics:

  • The segmentation method employs variable-size partitioning.
     
  • Secondary memory partitions are referred to as segments in general.
     
  • The length of modules has a significant impact on partition size. 
     
  • This approach divides main memory and secondary memory into divisions of varying sizes. 
     
  • Segment Table consumes less space in comparison to Page table in paging.

Advantages of Segmentation in OS

  1. There is no internal fragmentation as the segments are allocated the space as much as it requires, provided the memory is big enough to accommodate that segment. Unlike fixed partitioning, memory is not wasted in segmentation.
     
  2. Memory consumed by the segment table is comparatively less than by page tables.
     
  3. The user has more control means the process divides into segments logically from the point of view of the user, unlike paging.
     
  4. Overhead is less in segmentation means the additional cost or resources used for segmentation are very less.
     
  5. MMU (Memory Management Unit) takes care of the segment offset; it gets out of bounds or is not correct.
     
  6. Crucial Data can be recovered within the segment. For example, If there are 3 functions in C++, Then segmentation will divide each function into a segment to prevent and separate the data.

Disadvantages of Segmentation in OS

  1. It causes external fragmentation; If the total free memory is enough to be allocated by a process but can not because the process needs a contiguous memory, but only non-contiguous memory is free.
     
  2. Memory management is complex compared to other techniques; There are different processes and conditions that make it more complex than other techniques like segmentation needs to take care of the division of processes logically from the point of view of the user.
     
  3. Overhead is involved with maintaining a segment table for each operation; the segment table keeps track of the segments, which takes memory and time both for processing and computations.
     
  4. Segments of unequal size are not suited as well for swapping; Swapping in and out can be hectic as the segments are of different or unequal sizes.
     
  5. Access time to get the instruction increases due to the necessity for two memory accesses, one for the segment table and one for the main memory.
     
  6. Data sharing between the processes can be difficult as the data can be divided into its own segments, so a segment can not access the data of another segment.
     
  7. The time of accessing the memory can be increased as the computation is needed in the process of segmentation.

Example of Segmentation

Consider a program comprising various modules like main program, functions, and data structures. When loaded into memory, the operating system divides it into segments such as code segment (for main program), function segments (for each function), and data segment (for variables). Each segment is assigned a unique segment number and a base address indicating its location in memory. When the program executes, instructions access memory through segment numbers and offsets within segments.

For instance, accessing a variable involves specifying the data segment number along with the offset within that segment. Segmentation allows efficient memory management by accommodating variable-sized segments tailored to program requirements.

Architecture Segmentation in Operating System

In operating system architecture, segmentation enhances memory management by dividing processes into logical units called segments. Each segment represents a distinct part of a process, such as code, data, or stack. The CPU generates logical addresses comprising a segment number and an offset within the segment. 

The operating system maintains a segment table, mapping each segment number to its corresponding base address and length in physical memory. This mapping enables the CPU to translate logical addresses into physical addresses. Segmentation offers benefits such as flexibility in memory allocation, support for dynamic memory growth, and protection mechanisms by assigning different access rights to segments. However, managing variable-sized segments and preventing fragmentation pose challenges, which may require additional mechanisms like paging to optimize memory usage in modern operating systems.

Frequently Asked Questions

What is segmentation and paging?

Segmentation is a memory management technique in which the process is divided into logically related segments and then allocated to the memory. In paging, the process is divided into equal partitions which no logical pages and these pages are allocated to the memory known as frames.

What is segmentation and fragmentation in OS?

Fragmentation is a condition where the space for the processes in memory is free, but processes can not be allocated because of contiguous or non-contiguous conflicts. Segmentation is a memory management technique that helps in reducing internal directly but is prone to external fragmentation.

Where is segmentation used in OS?

Operating systems employ segmentation as a memory management strategy to efficiently organize and allocate memory resources. The division of main memory into logical chunks, such as code, data, stack, and heap, is a prevalent practice in modern operating systems.

What is the difference between segmentation and partitioning in OS?

Segmentation divides a process into logical units (segments) based on its structure, while partitioning divides the physical memory into fixed-size blocks (partitions). Segmentation offers flexibility in memory allocation, whereas partitioning allocates memory in predefined chunks.

Conclusion

Segmentation is a non-contiguous memory management technique where the process is divided into logically related segments, and the track of these segments is kept by the segment table. The segment table consumes comparatively less memory than the page table. Segmentation also helps in reducing internal fragmentation, and it is also prone to external fragmentation.

In this article, we discussed what is segmentation, the types of segmentation, why segmentation is required in OS (Operating System), segment table, Translation of Logical address into a physical address by segment table, and the characteristics, advantages, and disadvantages of Segmentation.

Recommended Reading:

Happy learning!

Live masterclass