For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentSegmentation in Operating System (OS): Components, Types, Examples

Segmentation in Operating System (OS): Components, Types, Examples

Published
18th Sep, 2023
Views
view count loader
Read it in
13 Mins
In this article
    Segmentation in Operating System (OS): Components, Types, Examples

    The secondary memory and the user program are segmented into irregularly sized blocks called Segments. Virtual Memory Segmentation and Simple Segmentation are the two categories into which segmentation in operating systems can be divided. 

    The details of each segment of the currently running process are kept in a segment table. The free memory space is broken up into smaller pieces as a result of the process's segments being switched around. External Fragmentation is the term used to describe this fragmentation of empty space. 

    Topics covered in the article include what segmentation is in OS and why it is required, common process segments, fragmentation problems, and advantages and disadvantages of segmentation in Operating System. 

    Full-stack developers are also better equipped to handle unexpected problems that arise during development. For these reasons, full-stack development is becoming increasingly popular among employers looking for talented developers. Check out the Full Stack Java Developer course to have a more in-depth idea of full-stack development. 

    What is Segmentation in Operating System (OS)?  

    Operating Systems use a variety of memory allocation and management techniques. Paging and segmentation in the operating system are the two non-continuous memory allocation techniques that are available. While segmentation in OS divides the user program and the secondary memory into uneven-sized blocks known as segments or sections, paging divides the memory into some fixed-sized blocks. Processes are segmented into more manageable chunks known as modules. It is not necessary to store the divided segments in contiguous memory. Internal fragmentation is avoided because no contiguous memory is allocated. The purpose of the segment in the user programme determines how long it should be in the programme and memory. These are the two non-continuous memory allocation techniques that are available. While segmentation in Operating System divides the user program and the secondary memory into uneven-sized blocks known as segments or sections, paging divides the memory into some fixed-sized blocks. Processes are segmented into more manageable chunks known as modules. It is not necessary to store the divided segments in contiguous memory. Internal fragmentation is avoided because no contiguous memory is allocated. The purpose of the segment in the user programme determines how long it should be in the programme and memory. 

    The main memory or the logical address space is made up of segments. 

    Why is Segmentation Required?

    Paging has been our primary memory management strategy up until this point. Instead of the User, Paging is closer to the Operating system. Regardless of the fact that a process may contain some related functional components that must be loaded in the same page, it divides all processes into pages. 

    The user's perspective of the process is irrelevant to the operating system. The same function might be split up into different pages, which might or might not be loaded into memory simultaneously. The system's effectiveness is reduced. 

    It is preferable to have segmentation in the operating system that breaks the process up into parts. The functions in each segment are of the same type; for example, the main function may be included in one segment, while library functions may be included in another.

    Segmentation in Operating System
     Source: Javapoint

    You can check out the Web Development course syllabus to become proficient in the modern tools required for web development. To become proficient in the modern tools required for web development.   

    What is Segment Table in OS & Its Uses

    The actual address in the main memory differs from the one the CPU generates. Physical addresses are those created by the main memory, whereas logical addresses are those produced by the CPU. 

    The data for each segment of the process is kept in a segment table. As is well known, a segment table is used to translate a logical address created by the CPU into a physical address. The segment table is used to map a two-dimensional logical address to a one-dimensional physical address.

    Segment Table

    Source: Scalar.com

    Components of Segment Table

    There are two parts to the segment table in OS: 

    1. Segment Base

    A segment's base address is also referred to as the segment base. The memory segments' starting physical addresses are contained in the segment base. 

    2. Segment Limit

    • Another name for the segment limit is segment offset. The segment's precise length is contained within it. 
    • Segment Table Base Register is referred to as STBR. The segment table's base address is kept in the STBR. 
    • Segment Table Length Register, also known as STLR. The number of segments a programme uses is stored in STLR. The segment table itself is kept in the main memory as a separate segment. If there are many segments, the segment table may occasionally use a lot of memory. 

    Types of Segmentation

    1. Virtual Memory segmentation

    Processes are divided into n segments by virtual memory segmentation. Not all of the segments are divided at once. A program's run time may or may not involve virtual memory segmentation. 

    2. Simple Segmentation

    Although the segmentation is completed all at once, Simple Segmentation also divides the processes into n segments. Simple segmentation happens during a program's run time. One segment of the process might be located in a different location than another due to simple segmentation that scatters the segments into the memory (in a noncontinuous manner).

    Characteristics of Segmentation

    The following are some characteristics of the segmentation technique: 

    • Variable-size partitioning is used in the segmentation method. 
    • Segments are a general term for secondary memory partitions. 
    • The length of modules has a major impact on partition size. 
    • Thus, using this method, main memory and secondary memory are divided into partitions of different sizes. 

    The Problem of Fragmentation

    Although external fragmentation is a difficult problem to solve, segmentation is a fast and highly secure memory management function. External fragmentation brought on by segmentation has gotten to the point where modern x86-64 servers only support it for backwards compatibility and treat it as a legacy application. 

    When unusable memory is found outside of memory blocks that have been allotted, external fragmentation happens. The problem is that while the system may have enough memory to meet process demands, the memory is not located in a single contiguous location. The segmentation process becomes noticeably slower as the fragmentation gets worse over time. 

    Common Segments of a Process

    Text, data, and system data segments are all present in the active process (or running programme). Let's find out more about these parts. 1. Text Segment: The program's code is located in the text segment. A program's address space segment contains a read-only code block called the Text Segment, also referred to as the Instruction Segment. Text Segments are started up from the program's executable file. 

    2. Data Segment/User Data Segment: The initialized data, non-initialized data, the heap area, and the stack area make up the data segment, also referred to as the user data segment. The initialized data, non-initialized data, the heap area, and the stack area make up the data segment, also referred to as the user data segment. 

    • The initialized section of the data contains strings that are initialized from the programme executable file. 
    • The non-initialized portion of the data contains global variables, and a heap area is a place the process creates to store global variables. The heap area is also known as the Malloc Area. 
    • The stack area serves as a storage location for local variables, function parameters, and function calls. 

    Function calls cause the stack area to grow and shrink. When a function is called, the stack's size grows; when it returns, it shrinks. 

    3. System Data Segment: System Data Segment contains information about a segment, such as a process's current directory, user id (UID), group ids (GIDs), text, data, and stack sizes, among other things. One thing to keep in mind is that the system is responsible for maintaining the data segment part, and the process can only access through system calls.  

    Advantages of Segmentation

    The following are the advantages of segmentation in Operating System:

    • Segmentation does not contain any internal fragmentation.
    • The records of the segments are kept in a segment table. Comparing the segment table to a page table for paging, the segment table itself uses less memory.
    • As an entire module is loaded at once, segmentation improves CPU utilization.
    • The user's perception of physical memory is very similar to segmentation. Users can divide user programmes into modules with segmentation. These modules are nothing more than separate processes' codes.
    • While the user specifies the segment size, the hardware determines the page size in paging.
    • Segmentation is a technique that can be used to separate data from security procedures.

    Disadvantages of Segmentation

    The following are the disadvantages of segmentation in OS:

    • The segmentation technique has a serious issue because the free memory space is fragmented when processes are switched.
    • It takes time to retrieve instructions or segments. 
    • It is difficult to swap segments of different sizes.
    • The cost of keeping a segment table for each process is another expense.
    • A process is deleted from the main memory once it is finished. The unevenly sized segments of the process are deleted from the main memory after the current process has finished running. Due to the uneven length of the segments, the main memory is left with holes of varying sizes. Due to their tiny size, these gaps in the main memory could go unused.

    Example of Segmentation

    In order to comprehend how it functions, let's see an example of segmentation in OS. Assume there are five segments, numbered 0 through 4, with segment 1 being the first segment. All of the process segments are initially stored in the physical memory space before the process is executed. A segment table is also available. The beginning entry address of each segment is contained in the segment table (denoted by base). The length of each segment is also included in the segment table (denoted by limit). Assume there are five segments, numbered 0 through 4, with segment 1 being the first segment. All of the process segments are initially stored in the physical memory space before the process is executed. A segment table is also available. The beginning entry address of each segment is contained in the segment table (denoted by base). The length of each segment is also included in the segment table (denoted by limit).

    Segment 2 starts at position 4300 and is 400 bytes long. As a result, a reference to byte 53 of segment 2 in this instance is mapped onto the location 4300 (4300+53=4353). Segment 3 reference byte 85 is mapped to 3200 (the segment 3 base) +852=4052.

    Segment 0 has a length of 1000 bytes, so referencing byte 1222 of that segment would trigger an OS trap.

    Example of Segmentation

    Difference Between Fragmentation and Segmentation

    Fragmentation

    As the name suggests, fragmentation is essentially a process that divides up free memory space. Because of their small size, the memory blocks in this situation cannot be allocated to processes, leaving them empty. It typically happens in a dynamic memory allocation system when there are too many small free blocks to accommodate any request. 

    Segmentation

    In core, segmentation is a memory management technique that supports the user's perception of memory. It is also referred to as a non-contiguous memory allocation technique. Each process in this is broken down into a number of segments, and information about each segment can be stored in a table called a segment table. In essence, it is a process that creates address spaces in computer storage with variable sizes for related data. Here is a detailed comparison table between fragmentation vs segmentation.

    S. No.FragmentationSegmentation
    1This results in the inefficient use of storage space, which lowers capacity and performance.Memory in this is divided into segments, which are variable-sized pieces.
    2Internal and external fragmentation are two different types of fragmentation.Simple segmentation and virtual memory are examples of segmentation types.
    3Its main objective is to assist the operating system in using the storage device's available space.Giving users' perspectives on the process is its primary goal.
    4It makes memory management less effective.It merely enables more effective memory management.
    5This leaves the memory blocks in the system unutilized.To run processes, it typically uses a memory management technique.
    6It frequently has IP as a connection.It frequently has TCP in mind.

    Looking to master Python? Join our unique Python course and pay only after placement! Unlock your coding potential and land your dream job. Don't miss out, enroll today!

    Conclusion

    The secondary memory is segmented into irregularly sized blocks called segments. Processes are segmented into modules, which are smaller sub-parts. The idea behind segmentation is that the related data should fit into a single segment.

    Partition size is influenced by the kind and size of modules. Virtual Memory Segmentation and Simple Segmentation are the two categories into which segmentation can be divided. For each process, the operating system maintains a segment table.

    The records of the segments are kept in a segment table. Comparing the segment table to a page table for paging, the segment table itself uses less memory.

    We have discussed what is memory segmentation. Memory segmentation was first introduced by the x86 instruction set architecture of Intel-compatible computers.

    The free memory space is divided up as a result of the switching of processes. External Fragmentation is the term used to describe this fragmentation of empty space. 

    A process is deleted from the main memory once it is finished. The process segment's uneven length results in unevenly sized holes in the main memory. The cost of keeping a segment table for each process is another expense. Enroll in KnowledgeHut’s Full Stack Developer course to learn the Java full stack developer skill set.

    Frequently Asked Questions (FAQs)

    1How does segmentation work?

    The process of segmenting involves dividing your marketing objectives into sections for clients and prospects. Using the segmentation strategy, you can locate and communicate with your primary audience.

    2Why is segmentation used in OS?

    Users can divide their programs into modules that run separately using segmentation. The free memory space is divided up as a result of the switching of processes. External Fragmentation is the term used to describe this fragmentation of empty space.

    3What is the difference between paging and segmentation?

    The program is divided into fixed or mounted-size pages when it is paginated. The program is segmented into sections of varying sizes.

    Profile

    Bala Krishna Ragala

    Blog Author

    Bala Krishna Ragala, Head of Engineering at upGrad, is a seasoned writer and captivating storyteller. With a background in EdTech, E-commerce, and LXP, he excels in building B2C and B2B products at scale. With over 15 years of experience in the industry, Bala has held key roles as CTO/Co-Founder at O2Labs and Head of Business (Web Technologies) at Zeolearn LLC. His passion for learning, sharing, and teaching is evident through his extensive training and mentoring endeavors, where he has delivered over 80 online and 50+ onsite trainings. Bala's strengths as a trainer lie in his extensive knowledge of software applications, excellent communication skills, and engaging presentation style.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon