## Contiguous Memory Allocation-

Before you go through this article, make sure that you have gone through the previous articles on Static Partitioning and Dynamic Partitioning.

There are two popular techniques used for contiguous memory allocation-

In this article, we will discuss practice problems based on Contiguous Memory Allocation.

## Problem-01:

Consider six memory partitions of size 200 KB, 400 KB, 600 KB, 500 KB, 300 KB and 250 KB. These partitions need to be allocated to four processes of sizes 357 KB, 210 KB, 468 KB and 491 KB in that order.

Perform the allocation of processes using-

1. First Fit Algorithm
2. Best Fit Algorithm
3. Worst Fit Algorithm

## Solution-

According to question,

The main memory has been divided into fixed size partitions as-

Let us say the given processes are-

• Process P1 = 357 KB
• Process P2 = 210 KB
• Process P3 = 468 KB
• Process P4 = 491 KB

## Allocation Using First Fit Algorithm-

In First Fit Algorithm,

• Algorithm starts scanning the partitions serially.
• When a partition big enough to store the process is found, it allocates that partition to the process.

The allocation of partitions to the given processes is shown below-

### Step-04:

• Process P4 can not be allocated the memory.
• This is because no partition of size greater than or equal to the size of process P4 is available.

## Allocation Using Best Fit Algorithm-

In Best Fit Algorithm,

• Algorithm first scans all the partitions.
• It then allocates the partition of smallest size that can store the process.

The allocation of partitions to the given processes is shown below-

## Allocation Using Worst Fit Algorithm-

In Worst Fit Algorithm,

• Algorithm first scans all the partitions.
• It then allocates the partition of largest size to the process.

The allocation of partitions to the given processes is shown below-

### Step-03:

• Process P3 and Process P4 can not be allocated the memory.
• This is because no partition of size greater than or equal to the size of process P3 and process P4 is available.

## Problem-02:

Consider the following heap (figure) in which blank regions are not in use and hatched regions are in use-

The sequence of requests for blocks of size 300, 25, 125, 50 can be satisfied if we use-

1. Either first fit or best fit policy (any one)
2. First fit but not best fit policy
3. Best fit but not first fit policy
4. None of the above

## Solution-

The allocation follows variable size partitioning scheme.

Let us say the given processes are-

• Process P1 = 300 units
• Process P2 = 25 units
• Process P3 = 125 units
• Process P4 = 50 units

## Allocation Using First Fit Algorithm-

The allocation of partitions to the given processes is shown below-

## Allocation Using Best Fit Algorithm-

The allocation of partitions to the given processes is shown below-

### Step-04:

• Process P4 can not be allocated the memory.
• This is because no partition of size greater than or equal to the size of process P4 is available.

Thus,

• Only first fit allocation policy succeeds in allocating memory to all the processes.
• Option (B) is correct.

Next Article- Introduction to Paging

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

## Contiguous Memory Allocation-

Before you go through this article, make sure that you have gone through the previous article on Contiguous Memory Allocation.

We have discussed-

• In contiguous memory allocation, a process can be stored only in a contiguous fashion.
• There are two popular techniques used for contiguous memory allocation-

## Dynamic Partitioning-

• Dynamic partitioning is a variable size partitioning scheme.
• It performs the allocation dynamically.
• When a process arrives, a partition of size equal to the size of process is created.
• Then, that partition is allocated to the process.

## Partition Allocation Algorithms-

• The processes arrive and leave the main memory.
• As a result, holes of different size are created in the main memory.
• These holes are allocated to the processes that arrive in future.

 Partition allocation algorithms are used to decide which hole should be allocated to the arrived process.

Popular partition allocation algorithms are-

1. First Fit Algorithm
2. Best Fit Algorithm
3. Worst Fit Algorithm

We have already discussed these algorithms in our previous article.

## Important Points-

### Point-01:

For dynamic partitioning,

• Worst Fit Algorithm works best.
• This is because space left after allocation inside the partition is of large size.
• There is a high probability that this space might suit the requirement of arriving processes.

### Point-02:

For dynamic partitioning,

• Best Fit Algorithm works worst.
• This is because space left after allocation inside the partition is of very small size.
• There is a low probability that this space might suit the requirement of arriving processes.

The following diagram illustrates the steps of translating logical address into physical address-

The steps are same as we have discussed in our previous article.

The advantages of dynamic partitioning are-

• It does not suffer from internal fragmentation.
• Degree of multiprogramming is dynamic.
• There is no limitation on the size of processes.

The disadvantages of dynamic partitioning are-

• It suffers from external fragmentation.
• Allocation and deallocation of memory is complex.

To gain better understanding about Contiguous Memory Allocation,

Watch this Video Lecture

Next Article- Practice Problems On Contiguous Memory Allocation

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

## Contiguous Memory Allocation-

• Contiguous memory allocation is a memory allocation technique.
• It allows to store the process only in a contiguous fashion.
• Thus, entire process has to be stored as a single entity at one place inside the memory.

## Techniques-

There are two popular techniques used for contiguous memory allocation-

1. Static Partitioning
2. Dynamic Partitioning

## Static Partitioning-

• Static partitioning is a fixed size partitioning scheme.
• In this technique, main memory is pre-divided into fixed size partitions.
• The size of each partition is fixed and can not be changed.
• Each partition is allowed to store only one process.

## Example-

Under fixed size partitioning scheme, a memory of size 10 KB may be divided into fixed size partitions as-

• These partitions are allocated to the processes as they arrive.
• The partition allocated to the arrived process depends on the algorithm followed.

## Algorithms for Partition Allocation-

Popular algorithms used for allocating the partitions to the arriving processes are-

1. First Fit Algorithm
2. Best Fit Algorithm
3. Worst Fit Algorithm

## 1. First Fit Algorithm-

• This algorithm starts scanning the partitions serially from the starting.
• When an empty partition that is big enough to store the process is found, it is allocated to the process.
• Obviously, the partition size has to be greater than or at least equal to the process size.

## 2. Best Fit Algorithm-

• This algorithm first scans all the empty partitions.
• It then allocates the smallest size partition to the process.

## 3. Worst Fit Algorithm-

• This algorithm first scans all the empty partitions.
• It then allocates the largest size partition to the process.

## Important Points-

### Point-01:

For static partitioning,

• Best Fit Algorithm works best.
• This is because space left after the allocation inside the partition is of very small size.
• Thus, internal fragmentation is least.

### Point-02:

For static partitioning,

• Worst Fit Algorithm works worst.
• This is because space left after the allocation inside the partition is of very large size.
• Thus, internal fragmentation is maximum.

### Internal Fragmentation

• It occurs when the space is left inside the partition after allocating the partition to a process.
• This space is called as internally fragmented space.
• This space can not be allocated to any other process.
• This is because only static partitioning allows to store only one process in each partition.
• Internal Fragmentation occurs only in static partitioning.

### External Fragmentation

• It occurs when the total amount of empty space required to store the process is available in the main memory.
• But because the space is not contiguous, so the process can not be stored.

• CPU always generates a logical address.
• A physical address is needed to access the main memory.

## Step-01:

• The translation scheme uses two registers that are under the control of operating system.
• During context switching, the values corresponding to the process being loaded are set in the registers.

These two registers are-

• Relocation Register
• Limit Register

• Relocation Register stores the base address or starting address of the process in the main memory.
• Limit Register stores the size or length of the process.

## Step-02:

• CPU generates a logical address containing the address of the instruction that it wants to read.

## Step-03:

• The logical address generated by the CPU is compared with the limit of the process.
• Now, two cases are possible-

### Case-01: Generated Address >= Limit

• If address is found to be greater than or equal to the limit, a trap is generated.
• This helps to prevent unauthorized access.

### Case-02: Generated Address < Limit

• The address must always lie in the range [0, limit-1].
• If address is found to be smaller than the limit, then the request is treated as a valid request.
• The result obtained after addition is the address of the memory location storing the required word.

## Diagram-

The following diagram illustrates the above steps of translating logical address into physical address-

The advantages of static partitioning are-

• It is simple and easy to implement.
• It supports multiprogramming since multiple processes can be stored inside the main memory.
• Only one memory access is required which reduces the access time.

The disadvantages of static partitioning are-

• It suffers from both internal fragmentation and external fragmentation.
• It utilizes memory inefficiently.
• The degree of multiprogramming is limited equal to number of partitions.
• There is a limitation on the size of process since processes with size greater than the size of largest partition can’t be stored and executed.

To gain better understanding about Contiguous Memory Allocation,

Watch this Video Lecture

Next Article- Dynamic Partitioning

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.