DEADLOCKCONCURRENCYSEMAPHORE

DEADLOCK

BANKER'S ALGORITHM

What is banker’s algorithm ? :
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for 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.

Why Banker’s algorithm is named so?
Banker’s algorithm is named so because it is used in banking system to check whether loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money that bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the account holders comes to withdraw their money then the bank can easily do it. In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. The bank would try to be in safe state always.

Characteristics of Banker's Algorithm:
- Keep many resources that satisfy the requirement of at least one client
- Whenever a process gets all its resources, it needs to return them in a restricted period.
- When a process requests a resource, it needs to wait
- The system has a limited number of resources
- Advance feature for max resource allocation

Disadvantage of Banker's algorithm:
- Does not allow the process to change its Maximum need while processing
- It allows all requests to be granted in restricted time, but one year is a fixed period for that.
- All processes must know and state their maximum resource needs in advance.

READ MOREDEMO

CONCURRENCY

TURN VARIABLE

What is turn variable ? :
Turn Variable or Strict Alternation Approach is the software mechanism implemented at user mode. It is a busy waiting solution which can be implemented only for two processes. In this approach, A turn variable is used which is actually a lock. This approach can only be used for only two processes. In general, let the two processes be Pi and Pj . They share a variable called turn variable.

Analysis of Turn Variable approach:
- Mutual Exclusion :
The strict alternation approach provides mutual exclusion in every case. This procedure works only for two processes. The pseudo code is different for both of the processes. The process will only enter when it sees that the turn variable is equal to its Process ID otherwise not Hence No process can enter in the critical section regardless of its turn.
- Progress :
Progress is not guaranteed in this mechanism. If P0 doesn't want to get enter into the critical section on its turn then P1 got blocked for infinite time. P1 has to wait for so long for its turn since the turn variable will remain 0 until P0 assigns it to 1.
- Portability :
The solution provides portability. It is a pure software mechanism implemented at user mode and doesn't need any special instruction from the Operating System.

READ MOREDEMO

PETERSON METHOD

Peterson’s solution provides a good algorithmic description of solving the critical-section problem and illustrates some of the complexities involved in designing software that addresses the requirements of mutual exclusion, progress, and bounded waiting.

Characteristics of Peterson Algorithm approach:
1. Mutual Exclusion:
The method provides mutual exclusion for sure. In entry section, the while condition involves the criteria for two variables
2. Progress:
An uninterested process will never stop the other interested process from entering in the critical section. If the other process is also interested then the process will wait.
3. Bounded waiting:
The interested variable mechanism failed because it was not providing bounded waiting. However, in Peterson solution, a deadlock can never happen because the process which first sets the turn variable will enter in the critical section for sure. Therefore, if a process is preempted after executing line number 4 of the entry section then it will definitely get into the critical section in its next chance.
4. Portability:
This is the complete software solution and therefore it is portable on every hardware.Therefore a process cannot enter in the critical section until the other process is interested and the process is the last one to update turn variable.
Disadvantage: Peterson’s solution works for two processes, but this solution is best scheme in user mode for critical section.
This solution is also a busy waiting solution so CPU time is wasted. So that “SPIN LOCK” problem can come. And this problem can come in any of the busy waiting solution.

READ MOREDEMO

SEMAPHORE

BINARY SEMAPHORE

A binary semaphore is restricted to values of zero or one, while a counting semaphore can assume any nonnegative integer value. A binary semaphore can be used to control access to a single resource. In particular, it can be used to enforce mutual exclusion for a critical section in user code. In a ‘binary semaphore’, the first task needing to use the resource will find the semaphore in a GO state and will change it to WAIT before starting to use the resource. Any other task in the meantime needing to use the resource will have to enter the blocked state. When the first task has completed accessing the resource, it changes the semaphore back to GO. This leads to the concept of ‘mutual exclusion’; when one task is accessing the resource, all others are excluded.

Has 0 and 1 value. The wait operation only works when the semaphore is 1 and the signal operation succeeds when semaphore is 0 Analysis of Binary Semaphore approach:
1. A binary semaphore has two components-
 a. An integer value which can be either 0 or 1.
 b. An associated waiting list (usually a queue).
2. The waiting list of binary semaphore contains the processes that got blocked when trying to enter the critical section.
3. In waiting list, the blocked processes are put to sleep.
4. The waiting list is usually implemented using a queue data structure.
5. Using a queue as waiting list ensures bounded waiting.
6. This is because the process which arrives first in the waiting queue gets the chance to enter the critical section first.
7. The wait operation is executed when a process tries to enter the critical section.
8. The signal operation is executed when a process takes exit from the critical section.
9. Binary semaphores are mainly used for two purposes-
 a. To ensure mutual exclusion.
 b. To implement order in which the process must execute.
Some of the disadvantages of semaphores are as follows :
Semaphores are complicated so the wait and signal operations must be implemented in the correct order to prevent deadlocks. Semaphores are impractical for last scale use as their use leads to loss of modularity. This happens because the wait and signal operations prevent the creation of a structured layout for the system. Semaphores may lead to a priority inversion where low priority processes may access the critical section first and high priority processes later.

READ MOREDEMO
BACK TO TOP