Contiguous memory


Contiguous memory

Fixed Partitioning
  • Equal-Sized Partitions: All partitions have the same size. For example, if you have 16 KB of RAM, you could divide it into four partitions of 4 KB each. This approach is simple but may lead to significant internal fragmentation, especially if the processes are much smaller than the partition size.
  • Unequal-Sized Partitions: Partitions are of different sizes to accommodate different process sizes more efficiently. For example, in a 16 KB RAM, you might have partitions of 2 KB, 4 KB, 6 KB, and 4 KB. This allows processes of different sizes to be allocated more fittingly, potentially reducing internal fragmentation compared to equal-sized partitions.It is a memory management technique where the available memory is divided into fixed-size blocks or partitions. Each partition can hold exactly one process. The size of the partitions is determined ahead of time and doesn't change during execution.
Equal-sized partitioning
Example: Let's say we have 1,000 units of memory (addresses 0 to 999), and we divide it into five fixed partitions:
Partition 1: 0 to 199 (200 units)
Partition 2: 200 to 399 (200 units)
Partition 3: 400 to 599 (200 units)
Partition 4: 600 to 799 (200 units)
Partition 5: 800 to 999 (200 units)

Scenario 1: Process Allocation
Process P1: Needs 150 units of memory.
  • P1 can fit into any of the partitions. Let's say it is placed in Partition 1 (0 to 199).
  • P1 occupies the first 150 units, and the remaining 50 units in Partition 1 are unused, leading to internal fragmentation (wasted space within the allocated partition).
Process P2: Needs 180 units of memory.
  • P2 can also fit into any of the remaining partitions. Let's place it in Partition 2 (200 to 399).
  • P2 occupies the first 180 units, leaving 20 units unused in Partition 2 (another case of internal fragmentation).
Process P3: Needs 250 units of memory.
  • P3 needs more than 200 units, so it cannot fit into any of the fixed partitions, even though there’s enough free space in total. This leads to wasted memory because P3 can’t be allocated unless the partition sizes were larger or the system uses a different technique like dynamic partitioning.
Key Points:
  • Internal Fragmentation: This occurs when a process doesn’t fully use the partition allocated to it, leaving some memory within that partition unused.
  • Rigid Structure: Since partition sizes are fixed, processes that are larger than the partition size cannot be accommodated, and smaller processes leave unused memory.
  • Simple Management: The advantage is that fixed partitioning is straightforward for the operating system to manage since the memory is divided in a predictable way.
Unequal-sized partitioning:
Imagine you have a computer with a small memory space, say 1,000 units (from address 0 to 999). You need to load three processes, P1, P2, and P3, into memory.

Step 1: Allocate Memory for P1
  • Process P1 needs 200 units of memory.
  • The operating system allocates memory addresses from 0 to 199 to P1.
  • So, P1's memory looks like this:
  • P1: Addresses 0 to 199.
Step 2: Allocate Memory for P2
  • Process P2 needs 300 units of memory.
  • The operating system allocates memory addresses from 200 to 499 to P2.
  • So, P2's memory looks like this:
  • P2: Addresses 200 to 499.
Step 3: Allocate Memory for P3
  • Process P3 needs 250 units of memory.
  • The operating system allocates memory addresses from 500 to 749 to P3.
  • So, P3's memory looks like this:
  • P3: Addresses 500 to 749.
Contiguous Memory Allocation:
In this example, each process is allocated a contiguous block of memory:
P1: 0-199
P2: 200-499
P3: 500-749
All the memory addresses assigned to a process are in a single, continuous block. This makes it easy for the operating system to manage memory, as each process's memory is contained within a clearly defined range.



Dynamic Partitioning

Dynamic partitioning is a memory management technique where the operating system allocates memory to processes in variable-sized blocks based on the process's needs. Unlike fixed partitioning, where memory is divided into predefined blocks, dynamic partitioning allows each process to take exactly the amount of memory it requires, minimizing wasted space.

Example of Dynamic Partitioning:
Let's say you have 16 KB of RAM, and the system needs to allocate memory to several processes:

1. Initial State:
  • Total RAM: 16 KB
  • Available Memory: 16 KB
2. Allocating Processes:
  • Process P1 requires 4 KB of memory. The OS allocates a 4 KB block for P1. Available Memory: 12 KB
  • Process P2 requires 6 KB of memory. The OS allocates a 6 KB block for P2. Available Memory: 6 KB
  • Process P3 requires 3 KB of memory. The OS allocates a 3 KB block for P3. Available Memory: 3 KB
3. Memory Layout:
  • RAM now looks like this:
  • P1: 0–4 KB
  • P2: 4–10 KB
  • P3: 10–13 KB
  • Free Space: 13–16 KB (3 KB)
4. Process Termination:
Now, suppose P1 finishes execution and is terminated. The 4 KB space that P1 occupied becomes available. Available Memory: 7 KB (4 KB from P1 and 3 KB from the remaining free space).

5. Allocating New Process:
Process P4 requires 5 KB of memory. The OS checks the available space. It could allocate 4 KB from P1’s old space and 1 KB from the remaining free space, but it cannot split across different blocks. So here due to external fragmentation, we can't allocate P4 having 5KB of memory.

6. There is no internal fragmentation in this.

Defragmentation 
In dynamic partitioning, memory can become fragmented into small, non-contiguous blocks, which means there may be enough total free memory but not enough in one single, contiguous block to meet the needs of a new process. To address this problem, defragmentation is used. Defragmentation helps by:
Combining Fragmented Free Spaces: It reorganizes the memory by moving and rearranging processes so that free memory is consolidated into larger, contiguous blocks. This allows processes, such as P4 requiring 5 KB, to be allocated efficiently within a single, unbroken block of free space.

Defragmentation, while useful for managing memory fragmentation, has some disadvantages:
  • Performance Overhead: CPU and I/O Usage: Defragmentation can be resource-intensive, consuming significant CPU time and I/O bandwidth, which may impact the performance of other applications and processes running on the system.
  • Temporary System Slowdown: System Lag: During defragmentation, the system may experience temporary slowdowns or lag because it is actively moving data around and reorganizing memory, potentially affecting user experience and productivity.
  • Data Movement Risks: Risk of Data Corruption: Although rare, moving data around during defragmentation introduces a risk of data corruption or loss if something goes wrong, such as a power failure or software glitch.

Comments

Post a Comment

Popular posts from this blog

Indexed allocation

Thrashing

Basics of paging