- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- OS - Processes
- OS - Process Control Block
- OS - Operations on Processes
- OS - Inter Process Communication
- OS - Context Switching
- OS - Multi-threading
- Scheduling Algorithms
- OS - Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- OS - TAT & WAT
- Predicting Burst Time in SJF Scheduling
- Process Synchronization
- OS - Process Synchronization
- OS - Critical Section Problem
- OS - Critical Section Synchronization
- OS - Mutual Exclusion Synchronization
- OS - Semaphores
- OS - Counting Semaphores
- OS - Mutex
- OS - Turn Variable
- OS - Bounded Buffer Problem
- OS - Reader Writer Locks
- OS - Test and Set Lock
- OS - Peterson's Solution
- OS - Monitors
- OS - Sleep and Wake
- OS - Race Condition
- OS Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- Memory Management
- OS - Memory Management
- OS - Contiguous Memory Allocation
- OS - Non-Contiguous Memory Allocation
- OS - First Fit Algorithm
- OS - Next Fit Algorithm
- OS - Best Fit Algorithm
- OS - Worst Fit Algorithm
- OS - Fragmentation
- OS - Virtual Memory
- OS - Segmentation
- OS - Buddy System
- OS - Allocating Kernel Memory
- OS - Overlays
- Paging and Page Replacement
- OS - Paging
- OS - Demand Paging
- OS - Page Table
- OS - Page Replacement Algorithms
- OS - Optimal Page Replacement Algorithm
- OS - Belady's Anomaly
- OS - Thrashing
- Storage and File Management
- OS - File Systems
- OS - File Attributes
- OS - Structures of Directory
- OS - Linked Index Allocation
- OS - Indexed Allocation
- I/O Systems
- OS - I/O Hardware
- OS - I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Non-Contiguous Memory Allocation
Memory allocation refers to the process of assigning blocks of memory to various programs and processes running on a computer. The blocks of memory can be allocated continuously or non-continuously. In the last chapter, we discussed contiguous memory allocation. This chapter will focus on non-contiguous memory allocation, how it is implemented in operating systems, and its advantages and disadvantages.
- Non-Contiguous Memory Allocation
- Paging
- Segmentation
- Advantages of Non-Contiguous Memory Allocation
- Disadvantages of Non-Contiguous Memory Allocation
Non-Contiguous Memory Allocation
Non-contiguous memory allocation is a memory allocation technique where a process is allocated multiple blocks of memory at different locations in the main memory. Sometimes the entire process may not be stored in the main memory. i.e., some parts of the process may be stored in the secondary storage (like hard disk). When the process needs to access a part of the program that is not in the main memory, the OS will immediately swap that part to the main memory.
The image below shows the main memory allocating storage to three processes P1, P2, and P3 using non-contiguous memory allocation −
In the image above, the process is divided into pages (Page 1, Page 2, Page 3, ... page 7). The first 2 pages are loaded into the physical memory (RAM), and rest are stored in the hard disk.
We have two types of non-contiguous memory allocation techniques −
- Paging
- Segmentation
Paging in Non-Contiguous Memory Allocation
Paging is a memory management technique used by modern operating systems to manage the allocation of memory to processes. In this technique, the physical memory (i.e., the RAM) is divided into blocks of fixed size called frames, and the logical memory (i.e., the memory where the process is stored) is divided into blocks of the same size called pages. When a process is executed, its pages are loaded into available frames in the physical memory.
The image below shows working of paging in operating system −
- The idea behind implementing paging is to store the part of the program that is currently in use in the physical memory (RAM) and keep the rest of the program in the hard disk.
- When the process needs to access a part of the program, the OS will check page table to see if the page is in the physical memory or not.
- If the page is in the physical memory, the CPU can directly access it. If not, a page fault will occur. So the OS will load the page from the hard disk into the physical memory and update the page table.
- While loading a new page into the physical memory, if the space is full, the OS will use a page replacement algorithm to decide which page to remove and make space for the new page in RAM.
Segmentation in Non-Contiguous Memory Allocation
Segmentation is another memory management technique used by modern operating systems to manage the allocation of memory to processes. Here, the logical memory (i.e., the memory where the process is stored) is divided into variable-sized segments based on the logical structure of a program, such as code segment, data segment, stack segment, etc. The physical memory (i.e., the RAM) is also divided into variable-sized blocks called segments. When a process is executed, its segments are loaded into available segments in the physical memory.
The main difference between paging and segmentation is that in paging, the memory is divided into fixed-size blocks. In segmentation, the memory is divided into blocks of variable size based on the logical structure of a program.
Features of Segmentation
The key features of segmentation are −
- Logical division of memory − Segments represent meaningful units like code, stack, data, or modules.
- Variable-sized divisions − Segments can have different lengths, depending on the program's requirements.
- No internal fragmentation − Since segments are not fixed in size.
- External fragmentation − Can occur when free memory is divided into small scattered blocks.
- Protection and sharing − Different segments can have access rights (read, write, execute) and can be shared among processes.
Advantages of Non-Contiguous Memory Allocation
Most of the modern operating systems uses non-contiguous memory allocation technique. It have several advantages over contiguous memory allocation technique. Some of the key advantages are −
- Reduced Memory Wastage − The chances of fragmentation are reduced in non-contiguous memory allocation. This will eliminate memory wastage issues and improve overall memory utilization.
- Run Large Processes − By implementing non-contiguous memory allocation, the operating system can run processes that are larger than the available physical memory (RAM). This is done by storing some parts of the process in the secondary storage (like hard disk).
- Flexibility − Non-contiguous memory allocation provides more flexibility in memory management.
Disadvantages of Non-Contiguous Memory Allocation
Even though non-contiguous memory allocation has several advantages, it also comes with some drawbacks. Some of the key disadvantages are −
- Complexity − Non-contiguous memory allocation is more complex to implement and manage compared to contiguous memory allocation. The operating system needs to maintain data structures like page tables and segment tables to keep track of the memory blocks.
- Slower Access Time − Due to repeated swapping of pages/segments and additional lookups in page/segment tables, the overall access time may be slower.
- Overhead − Non-contiguous memory allocation has higher overhead due to the need for additional data structures and algorithms to manage memory allocation.
Conclusion
Non-contiguous memory allocation is a memory allocation is advanced technique used by modern operating systems to manage memory without any wastage. In this technique, a process is allocated multiple blocks of memory at different locations in the main memory. The two main types of non-contiguous memory allocation techniques are paging and segmentation. By non-contiguous memory allocation, you can even run processes that are larger than the available physical memory (RAM).