Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

OS CIA III

Application of Deadlock Avoidance and Contiguous vs Non-contiguous


Memory Allocation
Done by: Liona Locksley
2240262
TOPIC 1:
Deadlock Avoidance:

In deadlock avoidance, the request for any resource will be granted if the
resulting state of the system doesn't cause deadlock in the system. The state of
the system will continuously be checked for safe and unsafe states.

In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.

The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never
be a circular wait condition.

Applications: Banking Algorithm


What is Banking Algorithm?
The banker’s algorithm is a resource allocation and deadlock avoidance
algorithm that tests for safety by simulating the allocation for the predetermined
maximum possible amounts of all resources, then makes an “s-state” check to
test for possible activities, before deciding whether allocation should be allowed
to continue.
Concept of Banking Algorithm
The Banking algorithm is based on the concept of a resource allocation graph,
which is a directed graph in which nodes represent processes and resources, and
edges represent requests and allocations. A request edge is drawn from a process
to a resource node when the process requests that resource, and an allocation
edge is drawn from a resource node to a process when the resource is allocated
to the process.
When working with a banker's algorithm, it requests to know about three things:
1. How much each process can request for each resource in the system. It is
denoted by the [MAX] request.
2. How much each process is currently holding each resource in a system. It is
denoted by the [ALLOCATED] resource.
3. It represents the number of each resource currently available in the system. It
is denoted by the [AVAILABLE] resource.

 To use the Banking algorithm to avoid deadlock, the system must keep
track of the available resources as well as the resources that are currently
assigned to each process. The system must also keep track of the
maximum number of resources that each process might require.
 The Banking algorithm operates by defining a safety sequence, which is a
set of processes that can be completed without causing a deadlock. If a
system has at least one safety sequence, it is considered safe.
 The Banking algorithm simulates the allocation and release of resources
for each process, beginning with the current allocation state, to determine
whether a system is safe. The system is considered safe if a sequence of
processes can be completed without entering a deadlock state.
Here is an example of how the Banking algorithm can be used to avoid
deadlock:
Suppose there are three processes (P1, P2, and P3) and three resources (R1, R2,
and R3). The maximum number of resources that each process may need is as
follows:

Process R1 R2 R3

P1 2 1 3

P2 1 3 2
Process R1 R2 R3

P3 1 2 1

The current allocation state is as follows:

Process R1 R2 R3

P1 2 0 1

P2 0 3 0

P3 1 0 1

To check whether this system is safe, the Banking algorithm simulates the
allocation and release of resources for each process. It starts with the current
allocation state and assumes that all processes will request their maximum
resources in turn.

The algorithm checks whether there is a safety sequence. If there is a safety


sequence, the system is considered safe. If there is no safety sequence, the
system is considered unsafe.
In this example, the algorithm finds a safety sequence: P3, P1, P2. Therefore,
the system is considered safe.
The Banking algorithm is an effective way to prevent deadlock in a system with
limited resources. It is widely used in operating systems, databases, and other
systems that rely on resource allocation.

The following are the Advantages and Disadvantages for the Banking
algorithm:

ADVANTAGES
1. It contains various resources that meet the requirements of each process.
2. Each process should provide information to the operating system for
upcoming resource requests, the number of resources, and how long the
resources will be held.
3. It helps the operating system manage and control process requests for
each type of resource in the computer system.
4. The algorithm has a Max resource attribute that represents indicates each
process can hold the maximum number of resources in a system.
DISADVANTAGES

1. It requires a fixed number of processes, and no additional processes can


be started in the system while executing the process.
2. The algorithm does no longer allows the processes to exchange its
maximum needs while processing its tasks.
3. Each process has to know and state their maximum resource requirement
in advance for the system.
4. The number of resource requests can be granted in a finite time, but the
time limit for allocating the resources is one year.
TOPIC 2:

Contiguous vs non- contiguous memory allocation

Memory allocation is an essential part of any computer system, and there are
different techniques to allocate memory in a system. Two of the most common
techniques are contiguous and non-contiguous memory allocation. In this study,
we will discuss the working principles and differences between these two
techniques.
1. Contiguous Memory Allocation:
Contiguous memory allocation is basically a method in which a single
contiguous section/part of memory is allocated to a process or file needing it.
Because of this all the available memory space resides at the same place
together, which means that the freely/unused available memory partitions are
not distributed in a random fashion here and there across the whole memory
space.

The main memory is a combination of two main portions- one for the operating
system and other for the user program. We can implement/achieve contiguous
memory allocation by dividing the memory partitions into fixed size partitions.
Working Principle:
In the contiguous memory allocation technique, the operating system keeps
track of the available memory in the system. When a process requests memory,
the operating system searches for a contiguous block of memory of the required
size. If a block of memory of the required size is found, it is allocated to the
process, and the process can use it. If there is no contiguous block of memory of
the required size, the process has to wait until a sufficient amount of memory is
available.

Differences:
The contiguous memory allocation technique is simple and easy to implement.
It is also efficient in terms of memory access, as the entire block of memory
allocated to a process is contiguous, and there is no need to traverse different
memory locations to access data. However, one of the significant drawbacks of
this technique is fragmentation. As processes are created and terminated, the
memory gets fragmented, and it becomes challenging to find contiguous blocks
of memory for new processes.

Advantages and Disadvantages of Contiguous Memory Allocation


There are various advantages and disadvantages of contiguous memory
allocation. Some of the advantages and disadvantages are as follows:
Advantages
1. It is simple to keep track of how many memory blocks are left, which
determines how many more processes can be granted memory space.
2. The read performance of contiguous memory allocation is good because
the complete file may be read from the disk in a single task.
3. The contiguous allocation is simple to set up and performs well.

Disadvantages
1. Fragmentation isn't a problem because every new file may be written to
the end of the disk after the previous one.
2. When generating a new file, it must know its eventual size to select the
appropriate hole size.
3. When the disk is filled up, it would be necessary to compress or reuse the
spare space in the holes.
4.
2. Non-Contiguous Memory Allocation :
Non-Contiguous memory allocation is basically a method on the contrary to
contiguous allocation method, allocates the memory space present in different
locations to the process as per it’s requirements. As all the available memory
space is in a distributed pattern so the freely available memory space is also
scattered here and there. This technique of memory allocation helps to reduce
the wastage of memory, which eventually gives rise to Internal and external
fragmentation.

Working Principle:
In the non-contiguous memory allocation technique, the operating system keeps
track of the available memory in the system. When a process requests memory,
the operating system searches for available memory blocks in the system and
allocates them to the process. These memory blocks can be located anywhere in
the memory, and they do not need to be contiguous.

Differences:
The non-contiguous memory allocation technique can efficiently use the
available memory in the system and can reduce fragmentation. However, it is
more complex than contiguous memory allocation, as the operating system
needs to keep track of multiple memory blocks allocated to a process. The non-
contiguous memory allocation technique can also have an impact on memory
access times, as the data may be scattered throughout the memory, and there
may be a need to traverse different memory locations to access the data.

Advantages and Disadvantages of Non-Contiguous Memory Allocation


There are various advantages and disadvantages of non-contiguous memory
allocation. Some of the advantages and disadvantages are as follows:
Advantages

1. It has the advantage of reducing memory waste, but it increases overhead


because of the address translation.
2. It slows down the memory execution because time is consumed in
address translation.

Disadvantages

1. The downside of this memory allocation is that the access is slow because
you must reach the other nodes using pointers and traverse them.

Difference between Contiguous and Non-contiguous Memory Allocation :

Non-Contiguous Memory
S.NO. Contiguous Memory Allocation Allocation

Non-Contiguous memory
Contiguous memory allocation
allocation allocates separate
1. allocates consecutive blocks of
blocks of memory to a
memory to a file/process.
file/process.

2. Faster in Execution. Slower in Execution.

3. It is easier for the OS to control. It is difficult for the OS to control.

Overhead is minimum as not much


More Overheads are there as there
4. address translations are there while
are more address translations.
executing a process.
Non-Contiguous Memory
S.NO. Contiguous Memory Allocation Allocation

Both Internal fragmentation and


Only External fragmentation
external fragmentation occurs in
5. occurs in Non-Contiguous
Contiguous memory allocation
memory allocation method.
method.

It includes single partition allocation It includes paging and


6.
and multi-partition allocation. segmentation.

7. Wastage of memory is there. No memory wastage is there.

Conclusion:

In summary, the contiguous memory allocation technique is


simple and efficient in terms of memory access, but it can lead to
fragmentation. The non-contiguous memory allocation technique
is more complex but can effectively utilize the available memory
and reduce fragmentation. Ultimately, the choice of memory
allocation technique depends on the specific needs of the system
and the trade-offs between simplicity, efficiency, and
fragmentation.

REFERENCES:
1. https://www.geeksforgeeks.org/difference-between-contiguous-and-
noncontiguous-memory-allocation/
2. https://www.javatpoint.com/contiguous-and-non-contiguous-memory-
allocation-in-operating-system
3. https://www.geeksforgeeks.org/bankers-algorithm-in-operating-system-
2/
4. https://www.javatpoint.com/bankers-algorithm-in-operating-system

You might also like