0% found this document useful (0 votes)
95 views6 pages

Segmentation

Memory segmentation is a technique in operating systems that divides main memory into segments for specific purposes like code, data, stack, and heap. Each segment has a segment table with attributes such as base address, limit, and access rights, facilitating address translation from logical to physical addresses. While segmentation offers advantages like logical division and dynamic memory allocation, it also faces challenges like external fragmentation and address translation overhead.

Uploaded by

SARDAR HUSSAIN S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views6 pages

Segmentation

Memory segmentation is a technique in operating systems that divides main memory into segments for specific purposes like code, data, stack, and heap. Each segment has a segment table with attributes such as base address, limit, and access rights, facilitating address translation from logical to physical addresses. While segmentation offers advantages like logical division and dynamic memory allocation, it also faces challenges like external fragmentation and address translation overhead.

Uploaded by

SARDAR HUSSAIN S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Segmentation :

Memory Segmentation in Operating Systems

Memory segmentation is a memory management technique in operating systems that divides the computer's main
memory into segments of varying sizes. Each segment represents a specific portion of a program or data, such as code,
stack, or heap.

How Segmentation Works

1. Memory Division: The physical memory is divided into segments of varying sizes, each with a specific purpose:

o Code segment: Contains executable instructions

o Data segment: Stores global and static variables

o Stack segment: Manages function calls and local variables

o Heap segment: Used for dynamic memory allocation

2. Segment Table: The operating system maintains a segment table for each process with entries containing:

o Base address: Starting physical address of the segment

o Limit: Length or size of the segment

o Access rights: Read, write, execute permissions

o Other attributes: Valid bit, protection bits, etc.

3. Address Translation: When a program references memory using a logical address:

o The logical address consists of a segment number and an offset

o The OS uses the segment number to look up the base address in the segment table

o It adds the offset to the base address to get the physical address

o It checks if the offset is within the limit to prevent illegal access

Advantages of Segmentation

• Logical Division: Memory is divided based on logical units (code, data, stack)

• Dynamic Memory Allocation: Segments can grow or shrink as needed

• Protection: Each segment can have different access permissions

• Sharing: Code segments can be shared between processes

• No Internal Fragmentation: Segments are exactly the size needed

Example of Segmentation

Let's consider a simple example with a C program:

Copy

#include <stdio.h>

#include <stdlib.h>

int global_var = 10; // Goes in data segment


void function() {

int local_var = 20; // Goes in stack segment

printf("Local variable: %d\n", local_var);

int main() {

int *dynamic_var = malloc(sizeof(int)); // Goes in heap segment

*dynamic_var = 30;

function();

printf("Global variable: %d\n", global_var);

printf("Dynamic variable: %d\n", *dynamic_var);

free(dynamic_var);

return 0;

In this example:

1. The code segment contains the compiled machine instructions for main(), function(), and the printf() calls.

2. The data segment stores global_var with value 10.

3. The stack segment holds local_var when function() is called.

4. The heap segment contains the dynamically allocated memory for dynamic_var.

When the program executes:

• The OS creates segment table entries for each segment

• Memory references use segment

addressing

• For example, accessing global_var might use logical address (Data:0)

• This gets translated to physical address (180+0=180) using the segment table

Challenges with Segmentation

• External Fragmentation: Free memory spaces between segments can become scattered

• Segment Swapping: Moving segments between main memory and secondary storage can be complex

• Address Translation Overhead: Looking up segment tables adds processing time

Modern operating systems often combine segmentation with paging for more efficient memory management, getting
the benefits of logical division while minimizing fragmentation issues.
1. Memory Division in Segmentation

In memory segmentation, the physical memory is divided into variable-sized blocks called segments. Each segment
serves a specific purpose within a program:

Types of Segments

1. Code Segment (Text Segment)

o Contains executable instructions of the program

o Usually read-only to prevent accidental modification

o Can be shared among multiple processes running the same program

2. Data Segment

o Stores initialized global and static variables

o Often divided into:

▪ Read-only data (constants)

▪ Read-write data (variables)

3. BSS (Block Started by Symbol) Segment

o Contains uninitialized global and static variables

o Initialized to zero by the operating system before execution

o Often considered part of the data segment

4. Stack Segment

o Dynamically grows and shrinks during program execution

o Used for:

▪ Function call management

▪ Local variables storage


▪ Parameter passing

▪ Return address storage

o Follows Last-In-First-Out (LIFO) structure

5. Heap Segment

o Used for dynamic memory allocation (malloc/new)

o Grows in the opposite direction of the stack

o Managed by memory allocation libraries

Key Characteristics of Memory Division

• Variable Size: Segments can have different sizes based on their purpose

• Independent Growth: Different segments can grow or shrink independently

• Protection: Each segment can have different protection attributes (read/write/execute)

• Sharing: Some segments (like code) can be shared between processes

• Dynamic Allocation: New segments can be created during runtime

2. Address Translation in Segmentation

Address translation is the process of converting logical (program) addresses to physical (memory) addresses. In
segmentation, this is a crucial operation that involves several components.

Components of Address Translation

1. Logical Address

o Consists of two parts:

▪ Segment Number/ID: Identifies which segment to access

▪ Offset: Position within the segment


o Format: (segment_number, offset)

2. Segment Table

o Maintained by the operating system for each process

o Contains information about all segments of a process

o Each entry has:

▪ Base Address: Starting physical address of the segment

▪ Limit: Length or size of the segment

▪ Permissions: Read/Write/Execute rights

▪ Valid Bit: Indicates if segment is in memory

▪ Other Attributes: Like protection bits, shared flag, etc.

3. Segment Table Base Register (STBR)

o Points to the starting address of the segment table

o Changed during context switching

Address Translation Process (Step by Step)

1. Extract Segment Number and Offset

o From the logical address (segment_number, offset)

2. Locate Segment Table Entry

o Use segment number as an index into the segment table

o Calculate entry address = STBR + (segment_number × entry_size)

3. Validate Access

o Check if the offset is less than the segment limit

o If offset ≥ limit, generate a segmentation fault

o Verify access permissions (read/write/execute)

4. Calculate Physical Address

o Physical Address = Base Address + Offset

o Where Base Address is obtained from segment table entry

5. Access Memory

o Use the physical address to access the actual memory location

Example Address Translation

Let's work through an example:

1. Logical Address: (2, 300)

o Segment Number = 2

o Offset = 300

2. Segment Table Lookup:

o Segment 2 entry shows:


▪ Base Address = 12000

▪ Limit = 3000

▪ Permissions = RW (Read/Write)

3. Validation:

o Check: Is Offset (300) < Limit (3000)? Yes

o Check: Do we have proper access permissions? Yes

4. Physical Address Calculation:

o Physical Address = Base (12000) + Offset (300) = 12300

5. Memory Access:

o System accesses memory at physical address 12300

Hardware Support for Address Translation

Most modern CPUs include Memory Management Units (MMUs) that handle address translation in hardware:

1. Segment Table Caching:

o Frequently used segment table entries are cached for faster access

2. Translation Lookaside Buffer (TLB):

o A special cache for storing recent address translations

o Reduces the need to access segment tables repeatedly

3. Protection Checking:

o Hardware performs limit and permission checks automatically

o Generates exceptions for illegal access attempts

Address Translation Benefits

1. Memory Protection:

o Programs can only access their own segments

o Prevents one process from accidentally or maliciously accessing another's memory

2. Logical Organization:

o Programs can be written using logical addresses

o No need to know actual physical memory layout

3. Memory Sharing:

o Multiple processes can share segments (like code segments)

o Each segment can have independent protection attributes

4. Dynamic Memory Allocation:

o Segments can grow or shrink as needed

o Operating system can relocate segments

This address translation mechanism is fundamental to how segmentation enables multiple processes to run
simultaneously while maintaining isolation and protection.

You might also like