Segmentation in OS

Segmentation in operating systems (OS) is a memory management technique where a process is logically divided into variable-sized segments, according to their functionality (such as code, data, stack, and heap), and then loaded into RAM. Unlike paging, which divides process memory into fixed-size blocks.

Note: When a program is executed, it becomes a process, and the OS loads it into RAM.

Need of Segmentation in OS

Paging is more favorable to the OS rather than the User. As in the concept of paging, a process is divided into pages of equal size. It may divide the same function into different pages, and those pages may or may not be loaded into the main memory at the same time.  Let’s explain with an example

Suppose there is an Add () function that is divided into two different pages, and these two pages are loaded in frames 3 and 4. Let’s suppose as the CPU executes F3 the page fault occurs, and F4 is replaced with any other frame, then the ADD() function is not executed completely. In this way, the efficiency of the system is reduced.

So, this problem is overcome through segmentation. In segmentation, each segment contains the same type of functions, such as main() and ADD() functions.

Common Segments in a Process

A program written in any programming language (i.e. C language), as we try to execute, it becomes a process. OS loads this process into main memory in the form of various segements. One process may have many segements of various categories.

segmentation in operating system (Program to process conversion and load in RAM)

The most common segments are given below

  • Code Segment (Text Segment): It stores the program’s compiled binary code (i.e. C program).
  • Data Segment:  It stores global and static variables (i.e. int count = 10;)
  • Stack Segment: It stores function calls (i.e. Add ()), local variables, and return addresses etc.
  • Heap Segment – It stores dynamically allocated memory (e.g., malloc() in C, new in C++).
  • Extra Segment (ES) / Additional Segments: Sometimes used for special purposes like storing additional data structures, buffers, or shared memory between processes.

Components of Segmentation

Segmentation divides a process into logical segments based on functionality. Below are the key components involved in segmentation:

segmentation in OS (Segment Table Working, Logical to physical address mapping)

 1. CPU (Central Processing Unit)

This mechanism starts as the CPU generates a logical address and sends this logical address to MMU. MMU is a hardware used By OS that helps to convert logical addresses to physical addresses. 

A logical address which is also known as virtual address, consist of segment number ad offset.

  • Segment Number: A single process may contain various segments that are stored in main memory, each segment is assigned a unique number which is called sa egment number or segment ID. This ID helps to find the base address of segment through the segment table.
  • Offset: It is also called displacement, we get base address from segment table and add with offset value to find the required instruction or data from a particular segment.  Segment table also holds the value of limit of segment. Offset value must smaller than Limit value otherwise an exception (segmentation fault) occurs.

Example: (Segment: 4, Offset: 300)

Important: Logical address of 32-bit processor is 4 bytes and 64-bit processor is 8 bytes. It shows that  the number of bytes in a logical address depends on the CPU architecture

2. MMU (Memory Management Unit)

The Memory Management Unit (MMU) is a hardware component that converts the logical to a physical address. In segmentation, the MMU performs the following

  • Receives a logical address (Segment Number + Offset) from the CPU.
  • Look up the Segment Table for the Base Address and Limit.
  • Validates the offset: If the offset exceeds the segment limit, the OS generates a segmentation fault (out-of-bounds error).
  • Computes the physical address (Physical address = Base Address + Offset).
  • Sends the physical address to RAM to fetch the required instruction or data.

3. Segment Table

In segmentation, each process contains its own segment table which is also stored in Mian Memoy. The segment table is pointed by a special register called the Segment Table Base Register (STBR). 

Segment Table in OS

Here are key entries in the segmentation Table

I. Segment Number: It identifies a particular segment of the process and is used as an index to find the corresponding segment in the segment table.

II. Base Address: It represents the starting physical address of the segment in the RAM. The base address is an element of physical address calculation:

  • Physical Address=Base Address + Offset

III. Limit: it tells the maximum size of the segment in bytes. If an offset (second part of logical address) exceeds this limit value, then a segmentation fault (out-of-bounds access) occurs. So, the offset must always be less than  the limit value.

  • offset < Limit

IV. Present/Valid Bit: The present or valid bit is used to specify whether a particular segment is loaded in RAM or not by representing it with a “0” or “1” value.

  • 1: The segment is currently loaded in RAM.
  • 0: The segment is not loaded in RAM (may be reside in secondary storage or swapped out).

V. Dirty Bit (Modified Bit): The dirty or modified bit is used to specify whether a particular segment is modified in RAM or not by representing it with a “0” or “1” value.

  • 1: The segment has been modified (now needs to be written back to disk).
  • 0: No modifications (no need to save back in disk).

VI. Access Control Information: This attribute specifies the read, write, and execute (R/W/E) permissions for the segment which provides memory protection from illegal access. There is commonly a 3-bit field, where each bit represents permission as given in the following table

Bit Pattern (R/W/X) Meaning
000
No Access (Protected)
100 Read-only
110 Read & Write
101 Read & Execute
111 Read, Write & Execute (Full Access)

VII. Sharing: it tells if the segment is shared among multiple processes or not through 1 bit either “0” or “1”. It helps in code sharing (e.g., shared libraries).

  • 0:  Not shared (private segment)
  • 1: Shared (used by multiple processes)

VIII. Segment Privilege Level (SPL): it defines protection levels used in OS security where higher privilege (kernel mode) and lower privilege (user mode).

Segment Privilege Level is mostly represented through 2 bits because modern OS use 4 privilege levels which are given in the following table

Bit Pattern Privilege Level Mode
00  Most privileged Kernel Mode 
01  High privilege OS Services 
10  Medium privilege Device Drivers 
11  Least privileged User Mode 

 4. Physical Address

A physical address is used to get actual data in the main memory. By using logical address (segment no, offset), and segment table we can get physical address easily.

  • Physical Address = Base Address + Offset

Suppose the following Segment Table

Segment Number  Base Address Limit (Segment Size)
0 5000 1000
1 7000 1500
2 12000 800

Example 01: Convert Logical Address (Segment 1, Offset 1200) to Physical address

Solution:

From the logical address we get,

  • Segment Number = 1
  • Offset = 1200

From the  segment table we get

  • Base Address of “Segment 1” from the Table  = 7000
  • Check Offset Validity:
    • Offset ≤ Limit = 12001500  (Valid)

  • Compute Physical Address:
    • Physical Address = Base Address + Offset = 7000+1200 = 8200

So, finally the Physical Address = 8200

Here we discuss in decimal number system, but CPU and RAM works in binary number system. 

Invalid Offset Example 2: Convert Logical Address (Segment 2, Offset 1000) to Physical address 

Solution:

From the logical address we get,

  • Segment Number = 2
  • Offset = 1000

From the segment table, we get

  • Base Address of “Segment 2” from the Table  = 12000
  • Check Offset Validity:
    • Offset ≤ Limit = 1000 ≤ 800  (invalid)

  • Result: Segmentation Fault (Access beyond segment limit)

5. RAM and OS

RAM Stores the actual segments of the process. OS Manages the Segment Table and memory allocation dynamically, depending on available RAM. OS Handles segmentation faults (if an invalid offset is accessed).

Types of Segmentation in Operating Systems

Segmentation has various types in OS which depend on how memory is organized and accessed. The main important types are given below 

1. Simple Segmentation

In simple segmentation, each segments are loaded into RAM. This method was used in the early operating system. The main problem with is approach was external fragmentation.

2. Virtual Memory Segmentation

In virtual memory segmentation, only the demand segments are loaded into RAM. This method is used in the modern operating system. The main problem with is approach was segment swapping which in crease overhead.

3. Segmented Paging (Hybrid Approach)

Segmentation with paging in os is an hybrid approach which combines the concept of segmentation and paging.

  • Segmentation with Paging divides a process into segments, with each segment further divided into fixed-size pages.
  • The Segment Table stores the base addresses of the Page Tables, which map logical pages to physical frames in RAM.
  • This hybrid approach enhances flexibility through segmentation and improves memory management efficiency through paging, while also reducing fragmentation.


Its main problem is memory management complexity because when both a segment table and a page table are required. it is mostly used in Linux, and Windows OS.

4. Two-Level Segmentation

In multi-user systems, each user is assigned separate memory segments. Each user maintains a segment table, and within the user’s memory space, each process has its own segment table. This structure enhances security and ensures better process isolation.

  • Advantage: Improved security and process isolation. 
  • Disadvantage: Increased overhead due to the presence of multiple segment tables.

5. Shared Segmentation

Segments can be shared among multiple processes and are often used for shared libraries and inter-process communication (IPC). Each segment has permissions for reading and writing, which control access. This protects memory from unauthorized reading or modifications.

  • Advantage: Efficient memory usage through shared resources. 
  • Disadvantage: There is a risk of unauthorized access if permissions are not managed correctly.

Logical to physical address Translation (Actual Mapping)

CPU generates the logical address, which contains two parts.

  • Segment Number
  • Offset

The Segment number from the logical address is mapped to the segment table in the main memory. The offset of the respective segment is compared with the Limit. If the offset is less than the limit, then it is a valid address; otherwise, it returns an invalid address.

In the diagram given below, we will use the short forms, S is Segment No,  d is offset, 

segmentation in operating system

Important: Segments loaded in RAM, may or may not be contiguous.

Suppose a process with its segments loaded in RAM and CPU getting it by generating a logical address as given in the following diagram 

Segmentation in os example

Values of each segment will be like that as shown in the below diagram, in binary representation

single segment values in os

Advantages of Segmentation

  • Segment table size is less than page table size.
  • No internal fragmentation because memory is allocated exactly as per segment size.
  • The average size of the Segment is larger than the actual size of the page.

Disadvantages of Segmentation

  • It can have external fragmentation.

Imagine we have available memory segments of 200 KB, 500 KB, and 300 KB. A process requires a memory segment of 600 KB; however, it cannot be allocated because the free segments are not contiguous. Although the total available memory sums up to 1000 KB, the process cannot utilize it because a single continuous memory segment is necessary.

  • Costly memory management algorithms.

Important Points About Segmentation 

Point 01: Program Counter (PC) holds the offset of the current instruction within the segment and generally increments based on the instruction length of a segment, not just a fixed byte increment.

Example:

  • If an instruction is 2 bytes long, the PC increases by 2.
  • If the next instruction is 4 bytes long, the PC increases by 4.

The CPU keeps fetching and executing instructions until:

  • A jump or branch occurs (changing the offset explicitly).
  • The segment limit is reached (causing a segmentation fault).

Point 02: Instruction size and offset size depend on the CPU architecture.

  • Instruction size varies by CPU: x86 (1–15 bytes), ARM (2/4 bytes), RISC-V (4/2 bytes).
  • Offset size depends on CPU mode: 16-bit (2bytes), 32-bit (4bytes), 64-bit (8bytes).

Point 03: Segment limit is checked each time the Program Counter (PC) is incremented to ensure that the CPU does not access memory beyond the segment’s allocated space.

Point 04: CPU generate logical address very time when any instruction of a segement is accessed. Therefore, the total number of logical addresses generated by the CPU would be:

  • Total logical addresses = Number of instructions

Point 05: Program counter (PC) Offset, instruction size Relation

  • After execution, PC = 1004, pointing to the next instruction.

So, PC is incremented based on instruction size, while offset-size tells the instruction range within a segment.

  • Segmentation was heavily used in early x86 processors (8086, 80286, 80386).
  • Modern CPUs (Intel Core, AMD Ryzen, ARM, RISC-V) still support segmentation but rely on paging.
  • Today, paging has replaced segmentation as the primary memory management technique in modern CPUs.