The Wayback Machine - https://web.archive.org/web/20240930193348/https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/
Open In App

Program for Banker’s Algorithm | Set 1 (Safety Algorithm)

Last Updated : 27 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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. The following Data structures are used to implement the Banker’s Algorithm: Let ‘n’ be the number of processes in the system and ‘m’ be the number of resource types. Available : 

  • It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
  • Available[ j ] = k means there are ‘k’ instances of resource type Rj

Max :

  • It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
  • Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.

Allocation :

  • It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process.
  • Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj

Need :

  • It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
  • Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
  • Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task. Banker’s algorithm consist of Safety algorithm and Resource request algorithm Safety Algorithm

The algorithm for finding out whether or not a system is in a safe state can be described as follows:

  1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work= Available Finish [i]=false; for i=1,2,……,n
  2. Find an i such that both a) Finish [i]=false b) Need_i<=work 
    if no such i exists goto step (4)
  3. Work=Work + Allocation_i Finish[i]= true goto step(2)
  4. If Finish[i]=true for all i, then the system is in safe state.

Safe sequence is the sequence in which the processes can be safely executed. A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock. More formally, ” a system is in a safe state only if there exists a safe sequence” .If a system has a safe sequence, it implies that there is no deadlock present. The sequence guarantees that all processes will complete their execution without getting into a circular wait situation. It is possible to have multiple safe sequences in a system. This occurs when there are multiple ways to order the execution of processes such that no deadlock will occur. Each safe sequence represents a different order in which processes can be executed while avoiding deadlocks. The existance of multiple safe sequences allows the system to have flexibility in scheduling processes and allocating resources. Different scheduling algorithms or resource allocation can depend on factors such as system efficiency, resource utilization, fairness or other criteria that the operating system aims to optimize.

In this post, implementation of Safety algorithm of Banker’s Algorithm is done. 

C++




// C++ program to illustrate Banker's Algorithm
#include<iostream>
using namespace std;
 
// Number of processes
const int P = 5;
 
// Number of resources
const int R = 3;
 
// Function to find the need of each process
void calculateNeed(int need[P][R], int maxm[P][R],
                   int allot[P][R])
{
    // Calculating Need of each P
    for (int i = 0 ; i < P ; i++)
        for (int j = 0 ; j < R ; j++)
 
            // Need of instance = maxm instance -
            //                    allocated instance
            need[i][j] = maxm[i][j] - allot[i][j];
}
 
// Function to find the system is in safe state or not
bool isSafe(int processes[], int avail[], int maxm[][R],
            int allot[][R])
{
    int need[P][R];
 
    // Function to calculate need matrix
    calculateNeed(need, maxm, allot);
 
    // Mark all processes as infinish
    bool finish[P] = {0};
 
    // To store safe sequence
    int safeSeq[P];
 
    // Make a copy of available resources
    int work[R];
    for (int i = 0; i < R ; i++)
        work[i] = avail[i];
 
    // While all processes are not finished
    // or system is not in safe state.
    int count = 0;
    while (count < P)
    {
        // Find a process which is not finish and
        // whose needs can be satisfied with current
        // work[] resources.
        bool found = false;
        for (int p = 0; p < P; p++)
        {
            // First check if a process is finished,
            // if no, go for next condition
            if (finish[p] == 0)
            {
                // Check if for all resources of
                // current P need is less
                // than work
                int j;
                for (j = 0; j < R; j++)
                    if (need[p][j] > work[j])
                        break;
 
                // If all needs of p were satisfied.
                if (j == R)
                {
                    // Add the allocated resources of
                    // current P to the available/work
                    // resources i.e.free the resources
                    for (int k = 0 ; k < R ; k++)
                        work[k] += allot[p][k];
 
                    // Add this process to safe sequence.
                    safeSeq[count++] = p;
 
                    // Mark this p as finished
                    finish[p] = 1;
 
                    found = true;
                }
            }
        }
 
        // If we could not find a next process in safe
        // sequence.
        if (found == false)
        {
            cout << "System is not in safe state";
            return false;
        }
    }
 
    // If system is in safe state then
    // safe sequence will be as below
    cout << "System is in safe state.\nSafe"
         " sequence is: ";
    for (int i = 0; i < P ; i++)
        cout << safeSeq[i] << " ";
 
    return true;
}
 
// Driver code
int main()
{
    int processes[] = {0, 1, 2, 3, 4};
 
    // Available instances of resources
    int avail[] = {3, 3, 2};
 
    // Maximum R that can be allocated
    // to processes
    int maxm[][R] = {{7, 5, 3},
                     {3, 2, 2},
                     {9, 0, 2},
                     {2, 2, 2},
                     {4, 3, 3}};
 
    // Resources allocated to processes
    int allot[][R] = {{0, 1, 0},
                      {2, 0, 0},
                      {3, 0, 2},
                      {2, 1, 1},
                      {0, 0, 2}};
 
    // Check system is in safe state or not
    isSafe(processes, avail, maxm, allot);
 
    return 0;
}


Java




// Java program to illustrate Banker's Algorithm
import java.util.*;
 
class GFG
{
 
// Number of processes
static int P = 5;
 
// Number of resources
static int R = 3;
 
// Function to find the need of each process
static void calculateNeed(int need[][], int maxm[][],
                int allot[][])
{
    // Calculating Need of each P
    for (int i = 0 ; i < P ; i++)
        for (int j = 0 ; j < R ; j++)
 
            // Need of instance = maxm instance -
            //                 allocated instance
            need[i][j] = maxm[i][j] - allot[i][j];
}
 
// Function to find the system is in safe state or not
static boolean isSafe(int processes[], int avail[], int maxm[][],
            int allot[][])
{
    int [][]need = new int[P][R];
 
    // Function to calculate need matrix
    calculateNeed(need, maxm, allot);
 
    // Mark all processes as infinish
    boolean []finish = new boolean[P];
 
    // To store safe sequence
    int []safeSeq = new int[P];
 
    // Make a copy of available resources
    int []work = new int[R];
    for (int i = 0; i < R ; i++)
        work[i] = avail[i];
 
    // While all processes are not finished
    // or system is not in safe state.
    int count = 0;
    while (count < P)
    {
        // Find a process which is not finish and
        // whose needs can be satisfied with current
        // work[] resources.
        boolean found = false;
        for (int p = 0; p < P; p++)
        {
            // First check if a process is finished,
            // if no, go for next condition
            if (finish[p] == false)
            {
                // Check if for all resources of
                // current P need is less
                // than work
                int j;
                for (j = 0; j < R; j++)
                    if (need[p][j] > work[j])
                        break;
 
                // If all needs of p were satisfied.
                if (j == R)
                {
                    // Add the allocated resources of
                    // current P to the available/work
                    // resources i.e.free the resources
                    for (int k = 0 ; k < R ; k++)
                        work[k] += allot[p][k];
 
                    // Add this process to safe sequence.
                    safeSeq[count++] = p;
 
                    // Mark this p as finished
                    finish[p] = true;
 
                    found = true;
                }
            }
        }
 
        // If we could not find a next process in safe
        // sequence.
        if (found == false)
        {
            System.out.print("System is not in safe state");
            return false;
        }
    }
 
    // If system is in safe state then
    // safe sequence will be as below
    System.out.print("System is in safe state.\nSafe"
        +" sequence is: ");
    for (int i = 0; i < P ; i++)
        System.out.print(safeSeq[i] + " ");
 
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int processes[] = {0, 1, 2, 3, 4};
 
    // Available instances of resources
    int avail[] = {3, 3, 2};
 
    // Maximum R that can be allocated
    // to processes
    int maxm[][] = {{7, 5, 3},
                    {3, 2, 2},
                    {9, 0, 2},
                    {2, 2, 2},
                    {4, 3, 3}};
 
    // Resources allocated to processes
    int allot[][] = {{0, 1, 0},
                    {2, 0, 0},
                    {3, 0, 2},
                    {2, 1, 1},
                    {0, 0, 2}};
 
    // Check system is in safe state or not
    isSafe(processes, avail, maxm, allot);
}
}
 
// This code has been contributed by 29AjayKumar


Python3




# Python3 program to illustrate
# Banker's Algorithm
 
# Number of processes
P = 5
 
# Number of resources
R = 3
 
# Function to find the need of each process
def calculateNeed(need, maxm, allot):
 
    # Calculating Need of each P
    for i in range(P):
        for j in range(R):
             
            # Need of instance = maxm instance -
            # allocated instance
            need[i][j] = maxm[i][j] - allot[i][j]
 
# Function to find the system is in
# safe state or not
def isSafe(processes, avail, maxm, allot):
    need = []
    for i in range(P):
        l = []
        for j in range(R):
            l.append(0)
        need.append(l)
         
    # Function to calculate need matrix
    calculateNeed(need, maxm, allot)
 
    # Mark all processes as infinish
    finish = [0] * P
     
    # To store safe sequence
    safeSeq = [0] * P
 
    # Make a copy of available resources
    work = [0] * R
    for i in range(R):
        work[i] = avail[i]
 
    # While all processes are not finished
    # or system is not in safe state.
    count = 0
    while (count < P):
         
        # Find a process which is not finish
        # and whose needs can be satisfied
        # with current work[] resources.
        found = False
        for p in range(P):
         
            # First check if a process is finished,
            # if no, go for next condition
            if (finish[p] == 0):
             
                # Check if for all resources
                # of current P need is less
                # than work
                for j in range(R):
                    if (need[p][j] > work[j]):
                        break
                     
                # If all needs of p were satisfied.
                if (j == R - 1):
                 
                    # Add the allocated resources of
                    # current P to the available/work
                    # resources i.e.free the resources
                    for k in range(R):
                        work[k] += allot[p][k]
 
                    # Add this process to safe sequence.
                    safeSeq[count] = p
                    count += 1
 
                    # Mark this p as finished
                    finish[p] = 1
 
                    found = True
                 
        # If we could not find a next process
        # in safe sequence.
        if (found == False):
            print("System is not in safe state")
            return False
         
    # If system is in safe state then
    # safe sequence will be as below
    print("System is in safe state.",
              "\nSafe sequence is: ", end = " ")
    print(*safeSeq)
 
    return True
 
# Driver code
if __name__ =="__main__":
     
    processes = [0, 1, 2, 3, 4]
 
    # Available instances of resources
    avail = [3, 3, 2]
 
    # Maximum R that can be allocated
    # to processes
    maxm = [[7, 5, 3], [3, 2, 2],
            [9, 0, 2], [2, 2, 2],
            [4, 3, 3]]
 
    # Resources allocated to processes
    allot = [[0, 1, 0], [2, 0, 0],
             [3, 0, 2], [2, 1, 1],
             [0, 0, 2]]
 
    # Check system is in safe state or not
    isSafe(processes, avail, maxm, allot)
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#




// C# program to illustrate Banker's Algorithm
using System;
 
class GFG
{
     
// Number of processes
static int P = 5;
 
// Number of resources
static int R = 3;
 
// Function to find the need of each process
static void calculateNeed(int [,]need, int [,]maxm,
                int [,]allot)
{
    // Calculating Need of each P
    for (int i = 0 ; i < P ; i++)
        for (int j = 0 ; j < R ; j++)
 
            // Need of instance = maxm instance -
            //             allocated instance
            need[i,j] = maxm[i,j] - allot[i,j];
}
 
// Function to find the system is in safe state or not
static bool isSafe(int []processes, int []avail, int [,]maxm,
            int [,]allot)
{
    int [,]need = new int[P,R];
 
    // Function to calculate need matrix
    calculateNeed(need, maxm, allot);
 
    // Mark all processes as infinish
    bool []finish = new bool[P];
 
    // To store safe sequence
    int []safeSeq = new int[P];
 
    // Make a copy of available resources
    int []work = new int[R];
    for (int i = 0; i < R ; i++)
        work[i] = avail[i];
 
    // While all processes are not finished
    // or system is not in safe state.
    int count = 0;
    while (count < P)
    {
        // Find a process which is not finish and
        // whose needs can be satisfied with current
        // work[] resources.
        bool found = false;
        for (int p = 0; p < P; p++)
        {
            // First check if a process is finished,
            // if no, go for next condition
            if (finish[p] == false)
            {
                // Check if for all resources of
                // current P need is less
                // than work
                int j;
                for (j = 0; j < R; j++)
                    if (need[p,j] > work[j])
                        break;
 
                // If all needs of p were satisfied.
                if (j == R)
                {
                    // Add the allocated resources of
                    // current P to the available/work
                    // resources i.e.free the resources
                    for (int k = 0 ; k < R ; k++)
                        work[k] += allot[p,k];
 
                    // Add this process to safe sequence.
                    safeSeq[count++] = p;
 
                    // Mark this p as finished
                    finish[p] = true;
 
                    found = true;
                }
            }
        }
 
        // If we could not find a next process in safe
        // sequence.
        if (found == false)
        {
            Console.Write("System is not in safe state");
            return false;
        }
    }
 
    // If system is in safe state then
    // safe sequence will be as below
        Console.Write("System is in safe state.\nSafe"
        +" sequence is: ");
    for (int i = 0; i < P ; i++)
            Console.Write(safeSeq[i] + " ");
 
    return true;
}
 
// Driver code
static public void Main ()
{
    int []processes = {0, 1, 2, 3, 4};
 
    // Available instances of resources
    int []avail = {3, 3, 2};
 
    // Maximum R that can be allocated
    // to processes
    int [,]maxm = {{7, 5, 3},
                    {3, 2, 2},
                    {9, 0, 2},
                    {2, 2, 2},
                    {4, 3, 3}};
 
    // Resources allocated to processes
    int [,]allot = {{0, 1, 0},
                    {2, 0, 0},
                    {3, 0, 2},
                    {2, 1, 1},
                    {0, 0, 2}};
 
    // Check system is in safe state or not
    isSafe(processes, avail, maxm, allot);
 
    }
}
 
// This code has been contributed by ajit.


Javascript




// Number of processes
const P = 5;
 
// Number of resources
const R = 3;
 
// Function to find the need of each process
function calculateNeed(need, maxm, allot) {
  // Calculating Need of each P
  for (let i = 0; i < P; i++) {
    for (let j = 0; j < R; j++) {
      // Need of instance = maxm instance -
      //                    allocated instance
      need[i][j]=(maxm[i][j] - allot[i][j]);
    }
  }
}
 
// Function to find the system is in safe state or not
function isSafe(processes, avail, maxm, allot) {
  let need = new Array(P);
  for(let i=0;i<P;i++) need[i]= new Array(R);
  let finish = [];
 
  // Function to calculate need matrix
  calculateNeed(need, maxm, allot);
 
  // Mark all processes as infinish
  for (let i = 0; i < processes.length; i++) {
    finish.push(false);
  }
 
  // To store safe sequence
  let safeSeq = [];
 
  // Make a copy of available resources
  let work = [];
  for (let i = 0; i < R; i++) {
    work.push(avail[i]);
  }
 
  // While all processes are not finished
  // or system is not in safe state.
  let count = 0;
  while (count < P) {
    // Find a process which is not finish and
    // whose needs can be satisfied with current
    // work[] resources.
    let found = false;
    for (let p = 0; p < P; p++) {
      // First check if a process is finished,
      // if no, go for next condition
      if (finish[p] == false) {
        // Check if for all resources of
        // current P need is less
        // than work
        let j;
        for (j = 0; j < R; j++) {
          if (need[p][j] > work[j]) {
            break;
          }
        }
 
        // If all needs of p were satisfied.
        if (j == R) {
          // Add the allocated resources of
          // current P to the available/work
          // resources i.e.free the resources
          for (let k = 0; k < R; k++) {
            work[k] += allot[p][k];
          }
 
          // Add this process to safe sequence.
          safeSeq[count] = p;
          count++;
 
          // Mark this p as finished
          finish[p] = true;
 
          found = true;
        }
      }
    }
 
    // If we could not find a next process in safe
    // sequence.
    if (found == false) {
      console.log("System is not in safe state");
      return false;
    }
  }
 
  // If system is in safe state then
  // safe sequence will be as below
  console.log("System is in safe state.\nSafe sequence is: ");
  for (let i = 0; i < P; i++) {
    console.log(safeSeq[i] + " ");
  }
 
  return true;
}
 
let processes = [0, 1, 2, 3, 4];
 
// Available instances of resources
let avail = [3, 3, 2];
 
// Maximum R that can be allocated to processes
let maxm = [
  [7, 5, 3],
  [3, 2, 2],
  [9, 0, 2],
  [2, 2, 2],
  [4, 3, 3],
];
 
// Resources allocated to processes
let allot = [
  [0, 1, 0],
  [2, 0, 0],
  [3, 0, 2],
  [2, 1, 1],
  [0, 0, 2],
];
 
// Check system is in safe state or not
isSafe(processes, avail, maxm, allot);
 
// 7 4 3
// 1 2 2
// 6 0 0
// 0 1 1
// 4 3 1
 
// This code is contributed by ishankhandelwals.


Output:

System is in safe state.
Safe sequence is: 1 3 4 0 2

Illustration : Considering a system with five processes P0 through P4 and three resources types A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following snapshot of the system has been taken: We must determine whether the new system state is safe. To do so, we need to execute Safety algorithm on the above given allocation chart. banker's algorithm Following is the resource allocation graph: BankersExecuting safety algorithm shows that sequence < P1, P3, P4, P0, P2 > satisfies safety requirement.

Time complexity = O(n*n*m)

  1. where n = number of processes and m = number of resources.


Similar Reads

Banker's Algorithm in Operating System
Prerequisite - Resource Allocation Graph (RAG), Banker’s Algorithm, Program for Banker’s Algorithm Banker's Algorithm is a resource allocation and deadlock avoidance algorithm. This algorithm test for safety simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible acti
15 min read
Banker's Algorithm in Operating System
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. The Banker's Algorithm i
15+ min read
Program for Best Fit algorithm in Memory Management using Linked List
Best fit algorithm for memory management: The memory partition in which there is a minimum loss on the allocation of the process is the best-fit memory partition that is allocated to the process.We have already discussed one best-fit algorithm using arrays in this article. However, here we are going to look into another approach using a linked list
15+ min read
Program for SSTF Disk Scheduling Algorithm
Given an array of disk track numbers and initial head position, our task is to find the total number of seek operations done to access all the requested tracks if Shortest Seek Time First (SSTF) is a disk scheduling algorithm is used. The basic idea is the tracks that are closer to the current disk head position should be serviced first in order to
10 min read
Program for Least Recently Used (LRU) Page Replacement algorithm
Prerequisite: Page Replacement AlgorithmsIn operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly
14 min read
Program for Next Fit algorithm in Memory Management
Prerequisite: Partition allocation methods What is Next Fit? The next fit is a modified version of 'first fit'. It begins as the first fit to find a free partition but when called next time it starts searching from where it left off, not from the beginning. This policy makes use of a roving pointer. The pointer moves along the memory chain to searc
11 min read
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the advantage of having a minimum average waiting time
13 min read
Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenarios, when processes have different arrival times, are discussed. Given n processes with their burst times and arrival times, the task is to find the average waiting time and an average turn around time using FCFS scheduling algorithm. FIFO simply queu
15+ min read
Program for Priority CPU Scheduling | Set 1
Priority scheduling is one of the most common scheduling algorithms in batch systems. Each process is assigned a priority. The process with the highest priority is to be executed first and so on. Processes with the same priority are executed on a first-come first served basis. Priority can be decided based on memory requirements, time requirements
11 min read
Buddy Memory Allocation Program | Set 1 (Allocation)
Prerequisite - Buddy System Question: Write a program to implement the buddy system of memory allocation in Operating Systems.Explanation - The buddy system is implemented as follows- A list of free nodes, of all the different possible powers of 2, is maintained at all times (So if total memory size is 1 MB, we'd have 20 free lists to track-one for
13 min read
Buddy Memory Allocation Program | Set 2 (Deallocation)
Prerequisite - Buddy Allocation | Set 1 Question: Write a program to implement the buddy system of memory allocation and deallocation in Operating Systems.Explanation - As we already know from Set 1, the allocation is done via the usage of free lists. Now, for deallocation, we will maintain an extra data structure-a Map (unordered_set in C++, HashM
15+ min read
Program for FCFS CPU Scheduling | Set 1
First come - First served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes according to the order they arrive in the ready queue. In this algorithm, the process that comes first will be executed first and next process starts only after the previous gets fully executed.  Terminologies Used in CPU Scheduling Arrival Time: Th
15+ min read
Optimal Page Replacement Algorithm
Prerequisite: Page Replacement Algorithms In operating systems, whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which page to replace. The target for all algorithms is to
15+ min read
Chandy–Lamport's global state recording algorithm
Each distributed system has a number of processes running on a number of different physical servers. These processes communicate with each other via communication channels using text messaging. These processes neither have a shared memory nor a common physical clock, this makes the process of determining the instantaneous global state difficult. A
3 min read
Huang's Termination detection algorithm
Huang's algorithm is an algorithm for detecting termination in a distributed system. The algorithm was proposed by Shing-Tsaan Huang in 1989 in the Journal of Computers. In a distributed system, a process is either in an active state or in an idle state at any given point of time. Termination occurs when all of the processes becomes idle and there
4 min read
Algorithm for implementing Distributed Shared Memory
Distributed shared memory(DSM) system is a resource management component of distributed operating system that implements shared memory model in distributed system which have no physically shared memory. The shared memory model provides a virtual address space which is shared by all nodes in a distributed system. The central issues in implementing D
3 min read
LRU Approximation (Second Chance Algorithm)
If you are not familiar with Least Recently Used Algorithm, check Least Recently Used Algorithm(Page Replacement)This algorithm is a combination of using a queue, similar to FIFO (FIFO (Page Replacement)) alongside using an array to keep track of the bits used to give the queued page a "second chance". How does the algorithm work: Set all the value
15+ min read
Raymond's tree based algorithm
Prerequisite - Mutual exclusion in distributed systems Raymond's tree based algorithm is lock based algorithm for mutual exclusion in a distributed system in which a site is allowed to enter the critical section if it has the token. In this algorithm, all sites are arranged as a directed tree such that the edges of the tree are assigned direction t
5 min read
Chandy-Misra-Haas's Distributed Deadlock Detection Algorithm
Chandy-Misra-Haas's distributed deadlock detection algorithm is an edge chasing algorithm to detect deadlock in distributed systems. In edge chasing algorithm, a special message called probe is used in deadlock detection. A probe is a triplet (i, j, k) which denotes that process Pi has initiated the deadlock detection and the message is being sent
4 min read
LOOK Disk Scheduling Algorithm
The LOOK Disk Scheduling Algorithm is the advanced version of the SCAN (elevator) disk scheduling algorithm which gives slightly better seek time than any other algorithm in the hierarchy (FCFS->SRTF->SCAN->C-SCAN->LOOK). It is used to reduce the amount of time it takes to access data on a hard disk drive by minimizing the seek time bet
13 min read
Introduction of Shortest Remaining Time First (SRTF) algorithm
Shortest Remaining Time First (SRTF) is the preemptive version of Shortest Job Next (SJN) algorithm, where the processor is allocated to the job closest to completion. This algorithm requires advanced concept and knowledge of CPU time required to process the job in an interactive system, and hence can't be implemented there. But, in a batch system
2 min read
Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
Prerequisite - Least Recently Used (LRU) Page Replacement algorithm Least Recently Used page replacement algorithm replaces the page which is not used recently. Implementation: In this article, LRU is implemented using counters, a ctime (i.e., counter) variable is used to represent the current time, it is incremented for every page of the reference
8 min read
Least Slack Time (LST) scheduling Algorithm in real-time systems
Least Slack Time (LST) is a dynamic priority-driven scheduling algorithm used in real-time systems. In LST, all the tasks in the system are assigned some priority according to their slack time. The task which has the least slack time has the highest priority and vice versa. Priorities to the tasks are assigned dynamically. Slack time can be calcula
3 min read
Difference between First Come First Served (FCFS) and Round Robin (RR) Scheduling Algorithm
First Come First Served Scheduling Algorithm: First Come First Served (FCFS) is the simplest and non-preemptive scheduling algorithm. In First Come First Served (FCFS), the process is allocated to the CPU in the order of their arrival. A queue data structure is used to implement the FCFS scheduling algorithm. The process which is at the head of the
2 min read
Difference between FCFS and SSTF Disk Scheduling Algorithm
Prerequisite - Disk Scheduling Algorithms 1. FCFS Disk Scheduling Algorithm : First come first serve, as name suggest this algorithm entertains the task in the order they arrived in the disk queue. It is the simplest and easy to understand disk scheduling algorithm. In this the head or pointer moves in the direction in which the task arrives and mo
3 min read
First Fit algorithm in Memory Management using Linked List
First Fit Algorithm for Memory Management: The first memory partition which is sufficient to accommodate the process is allocated.We have already discussed first fit algorithm using arrays in this article. However, here we are going to look into another approach using a linked list where the deletion of allocated nodes is also possible.Examples: In
15+ min read
Not Recently Used (NRU) page replacement algorithm
It is a page replacement algorithm. This algorithm removes a page at random from the lowest numbered non-empty class. Implicit in this algorithm is that it is better to remove a modified page that has not been referenced in atleast one clock tick than a clean page that is in heavy use. It is easy to understand, moderately efficient to implement and
3 min read
Difference between SSTF and LOOK disk scheduling algorithm
1. SSTF disk scheduling algorithm : SSTF stands for Shortest Seek Time First. As the name specify, this algorithm serves the task request which is closest to current position of head or pointer. Here, the direction of head plays an vital role in determining total head movement. If there occurs a tie between requests then the head will serve the req
3 min read
Difference between FCFS and C-LOOK disk scheduling algorithm
1. FCFS Disk Scheduling Algorithm FCFS stands for First Come First Serve, this algorithm entertains the task in the order they arrived in the disk queue. It is the simplest and easy to understand disk scheduling algorithm. In this, the head or pointer moves in the direction in which the task arrives and moves till all request is served. However, th
3 min read
Longest Job First (LJF) CPU Scheduling Algorithm
Longest Job First (LJF) is a non-preemptive scheduling algorithm. This algorithm is based on the burst time of the processes. The processes are put into the ready queue based on their burst times i.e., in descending order of the burst times. As the name suggests this algorithm is based on the fact that the process with the largest burst time is pro
5 min read