Bankers Algorithm (Resource and Safety Algoritm) > Java Program

Bankers Algorithm (Resource and Safety Algoritm) > Java Program
Operating Systems
Program:

import java.io.*; public class ResAndSafAlgo { static int allocation[][] = new int[5][3]; static int max[][] = new int[5][3]; static int available[] = new int[3]; static int need[][] = new int[5][3]; static int req[] = new int[3]; static boolean finish[] = new boolean[5]; static int work[] = new int[3]; static int order[] = new int[5]; static int c,p,flag,f1=0,f2=0,count,total=1;; public static void main(String[] args) throws IOException { read(); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ need[i][j] = max[i][j] - allocation[i][j]; } } display(); calculate(); if(f1 == 1 && f2 == 0) System.out.println("ERROR"); else if(f1 == 0 && f2 == 1) System.out.println("HALT"); else{ display(); System.out.println("Thus,The Resource Can Be Allocated To The Process"); safety(); } } static void read() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("ENTER MAX matrix: "); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ max[i][j] = Integer.parseInt(br.readLine()); } } System.out.println("ENTER ALLOCATION matrix: "); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ allocation[i][j] = Integer.parseInt(br.readLine()); } } System.out.print("AVAILABLE MATRIX: "); for(int i=0;i<3;i++){ available[i] = Integer.parseInt(br.readLine()); } } static void calculate() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("REQUEST for which process: "); p = Integer.parseInt(br.readLine()); System.out.print("REQUEST MATRIX: "); for(int i=0;i<3;i++){ req[i] = Integer.parseInt(br.readLine()); } for(int j=0;j<3;j++){ if(req[j]>need[p][j]){ f1 = 1; break; } } if(f1 == 0 && f2 == 0 ){ for(int j=0;j<3;j++){ if(req[j]>available[j]){ f2 = 1; break; } } } if(f1 == 0 && f2 == 0 ){ for(int j=0;j<3;j++){ allocation[p][j] = allocation[p][j] + req[j]; need[p][j] = need[p][j] - req[j]; available[j] = available[j] - req[j]; } } } static void display(){ System.out.println("MAX MATRIX:"); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ System.out.print(" " + max[i][j] + " "); } System.out.println(""); } System.out.println("ALLOCATION MATRIX:"); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ System.out.print(" " + allocation[i][j] + " "); } System.out.println(""); } System.out.println("NEED MATRIX:"); for(int i=0;i<5;i++){ for(int j=0;j<3;j++){ System.out.print(" " + need[i][j] + " "); } System.out.println(""); } System.out.print("AVAILABLE MATRIX: "); for(int i=0;i<3;i++){ System.out.print(" " + available[i] + " "); } System.out.println(""); } static void safety(){ for(int i=0;i<3;i++) work[i] = available[i]; do{ for(int i=0;i<5;i++){ count=0; for(int j=0;j<3 && finish[i]==false;j++){ if(need[i][j]<=work[j]) count++; if(count == 3){ for(int k=0;k<3;k++){ work[k] = work[k] + allocation[i][k]; } finish[i]=true; if(finish[i]==true){ order[c++]=i; } } } } total++; }while(total!=5); for(int i=0;i<5;i++){ if(finish[i] == false){ flag=1; break; } } if(flag == 0){ System.out.println("FOLLOWING TRANSACTION IS SAFE !!"); System.out.print("SAFE SEQUENCE : "); for(int i=0;i<5;i++) System.out.print("P[" + order[i] + "] "); }else System.out.println("FOLLOWING TRANSACTION IS NOT SAFE !!"); } } /*OUTPUT ENTER MAX matrix: 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ENTER ALLOCATION matrix: 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 AVAILABLE MATRIX: 3 3 2 MAX MATRIX 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ALLOCATION MATRIX 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 NEED MATRIX 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 AVAILABLE MATRIX: 3 3 2 REQUEST for which process: 1 REQUEST MATRIX: 1 0 2 MAX MATRIX 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 ALLOCATION MATRIX 0 1 0 3 0 2 3 0 2 2 1 1 0 0 2 NEED MATRIX 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 AVAILABLE MATRIX: 2 3 0 Thus,The Resource Can Be Allocated To The Process FOLLOWING TRANSACTION IS SAFE !! SAFE SEQUENCE : P[1] P[3] P[4] P[0] P[2] */

Comments

Popular posts from this blog

Intermediate Code Generation > C Program