Difference Between Paging And Segmentation Explained!
Before deep-diving into the differences between paging and segmentation, let's first understand memory management since the duo paging and segmentation are techniques of memory management.
The operating system is part of the memory of a multiprogramming computer, while multiple processes use the rest. Memory management is the responsibility of dividing memory among the various processes.
The fundamental goal of memory management is to make optimal use of memory by minimizing internal and external fragmentation. At one moment, a single process is loaded into memory that can be allocated in contiguous or non-contiguous memory/partitions.
Paging and segmentation are parts of the non-contiguous allocation of memory which is a memory management technique. We'll look at two memory management schemes - Paging and Segmentation in this article.
Paging
Paging is a static memory allocation method that allows a process's physical address space to be of non-contiguous memory type. It is a storage method that allows the operating system to retrieve processes in the form of pages from secondary storage and store them in the main memory.
Physical memory is divided into blocks of the same size that are called frames and logical memory space is divided into the fixed-sized block that are called pages.
The logical address space or virtual address and physical addresses are separated in paging. Logical address consisting of a page number and offset. Physical address consisting of a frame number and offset.
The page table base register (PTBR) refers to a process's page table. A virtual memory processor must have a distinct page table base register that is accessible by the operating system. The page table length register (PTLR) indicates the size of the page table
To facilitate paging, the operating system determines:
- The program's total page count.
- Finds a sufficient number of vacant page frames to make things easier.
- Loads all of the pages into memory; pages need not be contiguous.
Page Table (PT)
Every new process creates a separate page table. A page table is used in paging, and it contains the base address of each page. The page table is stored in physical memory.
Page table has page table entries(PTE) where each page table entry stores a frame number and optional status bits. Many status bits are used in the virtual memory system. Page table entry has the following information:
Frame number, Valid bit, Control bits, Referenced bit, Caching, Modified bit (dirty bit).
Page table can be implemented using Single level page tables stored in main memory, multi-level page tables stored in main memory, associative memory(Registers or TLB), or inverted page tables.
Paging Characteristics
- No external fragmentation
- Processes can use all frames(physical memory).
- The physical memory used by a process is no longer contiguous (non-contiguous memory allocation).
- Internal fragmentation may occur only on the last page of a process.
- The logical memory of a process is still contiguous.
Advantages of Paging
- Effective memory management.
- Simplicity in partitioning (non-contiguous memory allocation).
- Allocating memory is simple and inexpensive.
- Pages are simple to share.
- No compaction is necessary.
- no external fragmentation.
- more efficient swapping.
Disadvantages of Paging
- Internal fragmentation (only at the last page of the process).
- Address translation necessitates the use of specialized hardware.
- The page table is stored in the main memory.
- Address translation lengthens memory cycle times.
- Memory reference overhead is caused by multi-level paging.
- Memory access time is longer.
Note:
- When a process is created, paging is applied, a page table is constructed, and the page table's base address is saved in the PCB.
- The page table will be stored in the main memory.
- Because the page and frame size is the same, there is no external fragmentation in the paging.
- The internal fragmentation exists on the last page and is considered as P/2 where P is the page size.
Segmentation
Instead of partitioning memory into pages of identical size, virtual address space or process address space is divided into variable-size segments. The modular structure of how a program is organized is represented by segments.
Segmentation is a memory management method in which each job is broken into numerous smaller segments, one for each module, each of which contains parts that execute related functions. Segments are variable-sized block of a program that corresponds to logical units of the program.
All program segments must be put into memory before they can run. Every segment requires the OS to locate contiguous memory blocks to allocate to it. The logical address consists of two parts: segment number and offset.
Mapping from logical address to physical address is done with the help of a segment table. The location of the segment table in memory is indicated by the segment-table base register (STBR), while the number of segments used by a program is indicated by the segment-table length register (STLR).
Advantages of segmentation
- No internal fragmentation.
- The segments table has only one entry per actual segment.
- The average size of the segment is bigger than the average page size.
- Less overhead.
- Efficient translation.
- Protection in segmentation is at different levels and available for different segments.
- Enables sharing of selected segments.
- It is easier to relocate segments than it is to move an entire address space.
Disadvantages of segmentation
- It has external fragmentation.
- Costly memory management algorithm
- Segmentation: locate sufficient free memory.
- Allocating contiguous memory to variable segment size is still expensive and challenging.
Note:
- The program's main function, utility functions, data structures, and so on are all contained within a program segment.
- When it comes to the paging table, the segment table is smaller than the table in paging.
- For each process, the operating system keeps a segment map table and a list of free memory blocks, together with segment numbers, sizes, and memory addresses in the main memory.
Difference between Paging and Segmentation
Paging |
Segmentation |
A program is divided into fixed-size pages. | A program is divided into variable-size segments. |
The operating system is in charge of paging. | Segmentation is the responsibility of the user/compiler. |
In terms of memory access, paging is faster than segmentation. | Segmentation is slower than paging |
It suffers from internal fragmentation. | It suffers from external fragmentation. |
Logical address space is divided into a page number and page offset. | Logical memory address space is divided into a segment number and segment offset. |
To keep track of virtual pages, paging requires a page table. | To keep track of virtual pages, segmentation requires a segmentation table. |
The page table has one entry for each virtual page. | The segment table has one entry for each virtual segment. |
The operating system must maintain a free frame list. | The operating system must keep a list of holes in the main memory. |
Paging is invisible to the user | Segmentation is visible to the user. |
Page table entry has frame number and additional protected bits for pages | Segment table entry has a limit, base, and may contain some bits for the protection of segments. |
Conclusion
Operating systems employ paging and segmentation as two memory management strategies to control the computer's main memory. While segmentation splits the program into segments of varying sizes, paging divides memory into fixed-size blocks known as pages.
Modern operating systems commonly mix paging and segmentation to efficiently manage memory, as both techniques have advantages and disadvantages.
Together, segmentation and paging improve memory utilization, reduce external fragmentation, provide efficient memory allocation, and provide security. The choice between segmentation and paging depends on the particular requirements and constraints of the system and usually calls for striking a balance between flexibility, performance, and overhead.
FAQs
1. What is segmentation in memory management?
Operating systems employ segmentation, a memory management method, to divide the main memory of the computer into segments or parts. Every segment is a logical address area in the program that houses components that carry out associated tasks. Processes may be assigned memory blocks of varying sizes using segmentation, which can increase memory utilization and decrease fragmentation. A reference to a memory location in a computer system that uses segmentation includes a value that designates a segment as well as an offset (memory address) inside that segment.
2. How does external fragmentation relate to segmentation?
When there is sufficient total memory to fulfill a request or accommodate a process, but the memory is non-contiguous and cannot be utilized, external fragmentation—a sort of fragmentation occurs.
External fragmentation occurs with segmentation and dynamic partitioning. In segmentation, the memory is divided into variable-sized segments, and each segment is loaded into the main memory by creating partitions dynamically matching the size of each segment.
3. Describe the disadvantages of using segmentation as a memory management technique.
Some disadvantages of using segmentation as memory management are:
- Costly memory management algorithms: To allocate and deallocate memory segments, segmentation needs sophisticated memory management algorithms, which can be time- and resource-intensive.
- Difficult to allocate contiguous memory: Segmentation can make it difficult to allocate contiguous memory to variable-sized partitions, which can lead to external fragmentation.
- Overhead: Performance may suffer, and overhead may rise when a segment table is used. The memory requirements for each item in a segment table are higher, and using the table to get memory locations will lengthen access times.
4. How can a segmented memory system support variable-sized data structures efficiently?
Variable-sized data structures can be supported effectively by the memory management approach of segmentation. The program is divided into segments of varying sizes, and each segment is loaded into main memory by partitioning the memory in a way that is dynamically matched to the size of each segment.
In order to increase memory utilization and decrease fragmentation, segmentation enables the distribution of memory blocks of different sizes to processes. At the same time as enabling variable-sized memory allocation, segmentation offers effective memory allocation and protection.
5. What is the purpose of a page fault in paging?
When a process tries to access data or code that is in its address space but is not now present in the system RAM, it will throw a page fault exception. A page fault alerts the operating system, which then locates the needed page in virtual memory and moves it from secondary storage (such a hard drive) to the main memory. Page faults are used in paging to enable the operating system to load the requested page from virtual memory into physical memory so that the operation can proceed.
6. Explain the concept of a page table?
The virtual memory system of a computer operating system stores the mapping between virtual addresses and physical addresses in a data structure called a page table. Virtual address translation, which is required to access data in memory, heavily relies on the page table. The page table is made up of an array of page table entries, each of which provides details on a specific page of memory. The page table entry contains details on the page's physical address, status, and additional characteristics. The memory management unit (MMU) uses the page table to convert virtual addresses into physical addresses.
Hope you found the article interesting. For more such articles, stay tuned to Unstop!
You might also be interested in reading:
- What Is The Difference Between HTML and CSS?
- Difference Between DBMS And RDBMS: Why RDBMS Is An Advanced Version Of DBMS?
- What Is The Difference Between Data And Information? Explained
- Computer Is Just A Box Of Metal Without These Programmers
- What Is The Difference Between Data And Information? Explained