Segmentation Technique in Operating System

Segmentation Technique in Operating System

16 mins read29.5K Views Comment
Atul
Atul Harsha
Senior Manager Content
Updated on Oct 13, 2023 13:32 IST

Have you ever found yourself tapping your fingers impatiently while your computer takes its sweet time to load a program or file? It’s a common problem that many of us face, and it makes us wonder: isn’t there a better way for computers to handle tasks more quickly and smoothly?
Well, there might be a solution in the form of “Segmentation in Operating Systems”. But what is segmentation, and how can it help solve the slow-downs we often experience with our computers? Think of it as a smart organizer in your computer that breaks down information into smaller, easy-to-handle pieces, making it quicker to find and use what it needs.

2022_06_Segmentation-in-OS.jpg

In this article, we will learn about the concept of segmentation in Operating system. It is a fundamental yet often overlooked aspect that plays helps in enhancing computer performance. Just as we categorize different files in separate folders for easy access, segmentation technique works behind the scenes to organize data and programs into distinct sections, facilitating quicker access and optimized performance.

For computer grads like you, understanding Segmentation technique can be a game-changer in grasping how operating systems can be fine-tuned to function more efficiently. Let’s take a closer look at segmentation, a simple strategy that is essential in computing. Let’s explore how segmentation works and how it makes the operating systems more efficient.

Explore Interprocess Communication in Operating System

Table of contents

Introduction to Segmentation Technique

In the computing world, the efficiency of an operating system is often dictated by how well it manages memory. Segmentation is a technique that plays a pivotal role in this aspect. Segmentation is a memory management technique whereby data items are stored in segments on the storage media. It divides the process or user-accessible space into fixed-sized blocks, called segments. To understand this better, let’s draw a parallel with the product categorization system in the Amazon app.

Amazon App: Before Implementation of Product Categorization

2023_09_image-7.jpg

Imagine logging into the Amazon app where all products – electronics, clothing, groceries, and books – are all mixed up without any categorization or filtering options. As a user, you would find it:

  • Extremely challenging to locate the specific product you want.
  • Time-consuming to browse through a jumble of unrelated products.
  • Frustrating, as the user experience would be far from smooth and efficient.

This scenario is similar to a computer system without segmentation, where data and programs are scattered randomly across the memory, making it a herculean task for the operating system to manage and allocate memory efficiently.

Implementing Product Categorization: The Amazon App Transformation

To enhance user experience and efficiency, Amazon implements product categorization, akin to segmentation in operating systems. Now, products are organized into various categories, such as:

  • Electronics: Where you can find all gadgets and electronic appliances
  • Clothing: A section dedicated to apparel, shoes, and accessories
  • Groceries: Where you can quickly locate daily essentials and food items
  • Books: A segment that houses a vast collection of literature across genres

This structured approach not only makes it easier for users to find what they are looking for but also optimizes the space, avoiding clutter and enhancing the overall user experience.

Segmentation in Operating Systems: The Technical Parallel

In the context of operating systems, segmentation functions in a similar manner. It divides the memory into different segments, each designated for a specific type of data or program. This organized setup:

  • Facilitates Quicker Access: Helps in locating and managing data more swiftly.
  • Prevents Memory Wastage: Avoids fragmentation and optimizes memory space usage.
  • Enhances Performance: Boosts the overall performance of the computer by streamlining operations.

Just as product categorization in the Amazon app revolutionized online shopping by offering a streamlined and efficient browsing experience, segmentation in operating systems serves as a vital tool in enhancing computer performance by organizing memory in a structured and efficient manner.

What is Segmentation in OS?

  • Segmentation divides memory into logical chunks called segments.
  • Each segment has a unique identifier called a segment number.
  • The segment number is used to locate the segment in memory.
  • The operating system uses segment tables to store the information about each segment.
  • Segment protection prevents one program from accessing the segments of another program.
  • Segment sharing allows multiple programs to share the same segment.
  • Segmentation can be used to implement virtual memory.

Segmentation is a memory management technique that divides an application’s virtual address space into distinct sections or chunks.

By splitting the memory into different sections, the operating system can quickly figure out which parts are being used and which ones are free. This neat arrangement makes it much faster and easier to give out and take back memory space for the system. Interestingly, these memory sections are not all the same size and don’t sit side by side. Due to this flexible setup, there’s no wasted space in these sections. This problem is often referred to as ‘internal fragmentation‘. The size of each little section is chosen based on what job it has in a specific program.

Let’s understand this with a simple example:

Imagine you are organizing a large bookshelf, where each shelf represents a segment of memory. Instead of placing books randomly on the shelves (memory), you decide to organize them based on categories like fiction, non-fiction, science, and history. Each category represents a segment.

In the context of an operating system:

  1. Fiction Shelf (Code Segment): This is where all the executable instructions of a program are stored, similar to a collection of fiction books that tell different stories (programs).
  2. Non-Fiction Shelf (Data Segment): This shelf stores the global variables and static data, akin to a non-fiction section that contains factual information (data) that the stories (programs) might refer to.
  3. Science Shelf (Stack Segment): This segment is like a science shelf, where new books (data) are added and removed frequently. It operates in a Last-In-First-Out (LIFO) manner, storing local variables and function information.
  4. History Shelf (Heap Segment): This is a flexible space, similar to a history section where books (data) can be added or removed as needed. It is used for dynamic memory allocation, where variables are allocated and deallocated at runtime.

So, when a program runs, it knows exactly where to find the necessary instructions and data, just like how you can easily find a book in the organized bookshelf. This not only makes the memory usage more efficient but also speeds up the program execution, enhancing your computer’s overall performance.

Why is Segmentation Technique Required?

Benefit Before Segmentation After Segmentation
Enhanced organization A large program is treated as a single entity. The program is divided into smaller, more manageable segments.
Efficiency and speed The operating system needs to search the entire memory space to find a particular piece of data. The operating system can quickly identify the segment that contains the data and then locate the data within the segment.
Flexible memory usage All programs are allocated the same amount of memory, regardless of their needs. Programs are allocated memory according to their needs.
Prevention of internal fragmentation Internal fragmentation occurs frequently. Internal fragmentation is prevented or minimized.
Improved system performance Memory allocation and deallocation is slow and inefficient. Memory allocation and deallocation is faster and more efficient.
Enhanced security and isolation Programs can access each other’s memory. Programs are isolated from each other, preventing unauthorized access or alterations.

Segmentation is a memory management technique that divides a program into modules, called segments. This is unlike paging, which divides a program into pages. Segments can be of different sizes and can be loaded into memory independently. Segmentation can help to prevent internal fragmentation, which is when free memory is not large enough to accommodate a requesting process.

Also read: Real-time operating system

Must explore: Distributed operating system 

Difference Between Paging and Segmentation with Example

Some of you might be confused about the difference between segmentation and paging. Let me clear that up.

The first figure above depicts the paging concept. In paging, a program is divided into fixed-size pages. These pages are then stored in the main memory. The second figure depicts the segmentation concept. In segmentation, a program is divided into variable-sized segments. These segments can be loaded into the main memory independently.

Segmentation allows users to partition their programs into modules. These modules are independent pieces of code that can be loaded into memory as needed. This can improve the performance of a program by reducing the amount of time that the operating system spends searching for the code that it needs.

Here are some other key differences between segmentation and paging:

  • Segmentation is a logical view of memory, while paging is a physical view of memory. This means that segments are defined by the programmer, while pages are defined by the operating system.
  • Segments can be of different sizes, while pages are always the same size. This allows segmentation to better fit the needs of the program, while paging can lead to internal fragmentation.
  • Segments can be loaded into memory independently, while pages must be loaded in blocks. This allows segmentation to be more efficient, while paging can lead to external fragmentation.

Tabular Comparison of Paging vs Segmentation

Feature Paging Segmentation Example
Usage Systems with a small amount of memory to prevent external fragmentation. Systems with a large amount of memory to provide better security and flexibility. A word processing program with a small memory footprint would be a good candidate for paging. A database program with a large memory footprint would be a good candidate for segmentation.
Working The program is divided into pages and stored in the main memory. When the program needs to access a page, the operating system transfers it from the secondary memory to the main memory. The program is divided into segments and stored in the secondary memory. When the program needs to access a segment, the operating system transfers it from the secondary memory to the main memory. A program that is 100 pages long is divided into 4KB pages in paging. In segmentation, the program is divided into 3 segments: code, data, and stack.
Page size Fixed Variable In paging, the page size is typically 4KB. In segmentation, the segment size can be any size.
Loading of pages Dependent Independent In paging, all pages of a process must be loaded into memory before the process can be executed. In segmentation, pages can be loaded into memory independently.
Prevention of internal fragmentation No Yes Paging can lead to internal fragmentation because a free page may not be large enough to accommodate a requesting page. Segmentation can prevent internal fragmentation because segments can be of different sizes.
Prevention of external fragmentation Yes No Paging can prevent external fragmentation because pages are loaded into memory in blocks. Segmentation cannot prevent external fragmentation because segments can be loaded into memory independently.
Memory management overhead Lower Higher The memory management overhead in paging is lower than in segmentation because paging is a simpler technique.
Security Lower Higher Paging cannot provide as much security as segmentation because segments can be loaded into memory independently.
Flexibility Lower Higher Paging is less flexible than segmentation because pages must be loaded into memory in blocks.
Performance Faster Slower Paging is faster than segmentation because it is a simpler technique.

Read more: Difference between Paging and Segmentation

Also read: What is Operating Systems (OS) – Types, Functions, and Examples.

Interprocess Communication in Operating System
Interprocess Communication in Operating System
Processes in operating systems need to communicate with each other. Interprocess communication (IPC) is a process that allows different processes of a computer system to share information. If you want...read more
CPU Scheduling Algorithm: Operating System
CPU Scheduling Algorithm: Operating System
Scheduling of processes is very important thing in an operating system. CPU Scheduling Algorithm is an essential part of any operating system. Various algorithms can be used, each with advantages...read more
Functions of Operating System
Functions of Operating System
An operating system (OS) is software that manages computer software and hardware resources. Different operating systems have different app stores and could have different applications in them, too. This article...read more

How Does Segmentation Work?

Segmentation is a memory management technique that divides the process address space into variable-sized segments. The CPU generates logical addresses that are references to these segments. To access a memory location within a segment, the CPU must first translate the logical address into a physical address. This is done by looking up the segment number in the segment table. The segment table is a data structure that maps segment numbers to physical addresses. The physical address is then calculated by adding the base address of the segment from the segment table to the offset within the segment.

2022_06_image-122.jpg

Key Concepts related to Working of Segmentation in OS

Concept Description
Segment number A unique identifier for a memory segment. It is used to locate the memory segment in the segment table.
Segment size The number of bytes in a memory segment. It is used to determine the range of addresses that can be used to access the memory segment.
Logical address A virtual address that is generated by the CPU. It is not a physical address, but rather a reference to a memory segment.
Segment table A data structure that maps segment numbers to physical addresses. It is used by the CPU to find the physical address of a memory segment given its segment number.
Main memory The physical memory of a computer. It is where the CPU stores data and instructions.
Error An error can occur if the logical address is invalid or if the segment table is corrupt.
Base address The starting address of a memory segment in main memory. It is used to calculate the physical address of a memory location within the memory segment.
Offset The distance from the base address of a memory segment to the desired memory location. It is used to calculate the physical address of the memory location.

NOTE: The base address is the starting address of a memory segment in main memory. It is used to calculate the physical address of a memory location within the memory segment.

Translation of Logical address into physical address by segment table

Problem: The segment table for a process has 16 entries, each of which is 4 bytes long. The logical address space of the process is 2^32 bytes. The segment table is located at physical address 0x1000. The logical address is 0x12345678.

Solution:

  1. The segment number for the logical address is 0x12.
  2. The offset for the logical address is 0x345678.
  3. The physical address of the segment is found by multiplying the segment number by the size of a segment table entry, which is 4 bytes. In this case, the physical address of the segment is 0x12 * 4 = 0x48.
  4. The physical address of the memory location is found by adding the offset to the physical address of the segment. In this case, the physical address of the memory location is 0x48 + 0x345678 = 0x345726.

Here how the translation process is done:

 
Logical address: 0x12345678
Segment number: 0x12
Offset: 0x345678
Segment table: 0x1000
Segment table entry size: 4 bytes
Physical address of segment: 0x12 * 4 = 0x48
Physical address of memory location: 0x48 + 0x345678 = 0x345726
Copy code

Step by Step working of Segmentation in OS

Step Description
1.CPU generates a logical address. The logical address is a virtual address that is generated by the CPU. It is not a physical address, but rather a reference to a memory segment.
2. Logical address is divided into two parts: the segment number and the offset (d). The segment number identifies the memory segment that the logical address refers to. The offset identifies the specific location within the memory segment. The offset depicts the distance from the base address of the segment to the desired memory location. The size of the segment is stored in the segment table.
3. CPU looks up the segment number in the segment table. The segment table is a data structure that maps segment numbers to physical addresses. The CPU uses the segment table to find the physical address of the memory segment that the logical address refers to.
4. CPU adds the offset to the base address of the memory segment. The base address is added to the offset to get the final physical address of the memory location that the logical address refers to.
5. CPU checks if d<=limit. The CPU checks if the offset is less than or equal to the limit of the segment. If it is not, then an error is generated.
6. The CPU accesses the memory location at the final physical address. The CPU now has the physical address of the memory location that the logical address refers to. It can now access this memory location

NOTE: The concept of d<=limit means that the offset must be less than or equal to the limit of the segment. This is to ensure that the CPU does not access memory that is outside of the segment.

Process Management in Operating System
Process Management in Operating System
Process management in operating systems involves the handling and coordinating multiple processes executed by the CPU. It includes tasks like process scheduling, creation, execution, and termination, ensuring efficient and fair...read more
Process Scheduling: Operating System
Process Scheduling: Operating System
Have you ever wondered how your computer juggles multiple tasks simultaneously? To know that, you must read this blog on process scheduling in the operating system! This article includes...read more
Operating System Interview Questions
Operating System Interview Questions
In this article you will find important operating system questions which are likeable to be asked in interview.

Segmentation Technique in different OS

OS Segment table Memory management
Microsoft Windows Each process has its own segment table, which maps segment numbers to physical addresses. The operating system uses the segment table to keep track of which parts of memory are being used by each process and to prevent conflicts between processes.
Linux The segment table is not stored in memory. Instead, it is stored in a file on disk. This allows Linux to save space in memory and to make it easier to move processes between different computers.
Android Each application has its own segment table, which maps segment numbers to physical addresses. This allows the operating system to keep track of which parts of memory are being used by each application and to prevent conflicts between applications.
macOS The segment table is stored in a special area of memory called the kernel space. This allows the operating system to have more control over the memory of each process.

Segmentation Example

Let’s see and understand how PUBG used the concept of segmentation during their development:

MY FB : pubg, pubg profile HD wallpaper | Pxfuel

In PUBG, each player has their own process. The process contains the code and data for the player’s character, as well as the game environment that the player is currently in.

The operating system uses the segment table to keep track of the physical addresses of the code, data, and game environment segments for each player process. When a player tries to access a memory location, the operating system looks up the segment number in the segment table. The segment table then tells the operating system the physical address of the memory location.

For example, let’s say a player is trying to access the memory location where the player’s character’s health is stored. The operating system looks up the segment number for the player’s character’s data segment in the segment table. The segment table tells the operating system that the physical address of the memory location is 0x1000.

The operating system then transfers the data from the memory location with the physical address 0x1000 to the CPU. The CPU can then read the player’s health from the memory.

This is just a simple example of how games like PUBG can use segmentation. In reality, the process of segmentation is much more complex.

Segmentation is a powerful memory management technique for improving the performance and security of games. In PUBG, segmentation can be used to:

  • Protect different parts of the game from each other. For example, the operating system can use segmentation to prevent a player from accessing the code for another player’s character.
  • Handle memory sharing between different players. For example, the operating system can use segmentation to allow two players to share the same game environment.
  • Allocate large, contiguous blocks of memory for games that need it. For example, a game that uses a lot of 3D graphics may need to allocate large, contiguous blocks of memory for the textures and models.
  • Improve the performance of memory access. For example, the operating system can use segmentation to cache frequently accessed memory locations in the CPU’s memory cache.

Conclusion

Segmentation is a memory management technique that divides the process address space into variable-sized segments. This allows the operating system to better manage its memory use and improve the performance of the system. For example, segmentation can be used to:

  • Allocate large, contiguous blocks of memory for programs that need it.
  • Protect different parts of the memory from each other.
  • Handle memory sharing between different processes.

Segmentation is a powerful memory management technique that can be used to improve the performance of an operating system. If you liked this article, please share it with your friends.

Happy Learning!!!

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio