Please Implement the following program in Java: For reference: Link to video: ht
ID: 3834695 • Letter: P
Question
Please Implement the following program in Java:
For reference: Link to video: https://www.youtube.com/watch?v=w0LwGqffUkg
Please implement a program using Java, to simulate the Bankers algorithm for Deadlock detection. The program should detect whether there is any potential deadlock in the given situation of resource availability, allocation and request. A user-friendly graphical interface using independent windows should enable the user to input the required data. 2. Once the fields are filled with the correct data and the "Create Matrix" button 1. When the program is executed, is clicked, a new window should display the following matrix (Size of Matrix a new window should prompt the depends on the number of processes:number of resources). The user should be user to enter the number of able to fill the matrix with the necesary input for the operation (use textfields) processes and the number of resources for the desired operation. (Only numbers should be accepted). R1 R2 R3 R4. R1 R2 R3 R4 R1 R2 R3 R4 P1 P1 P1 P2 P2 R1 R2 R3 R4 Welcome P3 P3 P3 of processes 3 Available Need Max Allocation of resources Detect Deadlock Create Matrix 3. Once the matrix are correctly filled (only numbers) and the "Detect Deadlock" button is clicked, the program should detect if there is deadlock in the data inputted by the user 4. A new window should inform the user if there is possibility of deadlock or no, and Deadlock detected in... if there is, it would be nice to show where. A little button in this window should allow the user to perform a new deadlock test. (First window should prompt again and program restarts New deadlock test For Reference on Bankers algorithm and how it works please use the following video: Deadlock Banker's Algorithm Deadlock not possible. Mifta Sintaha New deadlock test Subscribe 15,504Explanation / Answer
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
import java.io.*;
public class banker implements Runnable {
int Max [][] = new int [5][4];
int Allocation [][]= new int [5][4];
int request[] = new int [4];
int available[] = new int [4];
int p;
int speed;
inputFrame input;
animationFrame animate;
status st;
int flagList [] = new int [5];
public boolean processFinishp(){
boolean finish = true;
for(int i=0;i<5;i++){
if (!processList[i].returnFinish()) {
finish = false;
break;
}
}
return finish;
}
public Thread thread;
SimpleProcess processList [];
public banker(int[][] ma, int[][] alloc, int[] avail, int[] req,
int proc, inputFrame in, animationFrame an, status sta, int anim_spd){
Max = ma;
Allocation = alloc;
request = req;
p = proc;
speed = anim_spd;
available = avail;
input = in;
animate = an;
st = sta;
thread = new Thread(this, "banker");
thread.start();
}
public void run() {
ManagementData md = new ManagementData(available, animate, st);
for (int i=0; i<5; i++)
new SimpleProcess(i, md, Max[i], Allocation[i], animate, st);
System.out.println("* Initial State *");
st.upStatus("* Initial State *") ;
md.printAvailable();
md.printNeed();
md.printAllocation();
animate.repaint();
try{thread.sleep(400*speed);}catch(Exception e){}
System.out.println("-- Initial Request Check --");
st.upStatus("-- Initial Request Check --") ;
System.out.println(md.checkSafety());
System.out.println();
try{thread.sleep(400*speed);}catch(Exception e){}
boolean flag = md.initialResourceRequest(p, request);
try{thread.sleep(400*speed);}catch(Exception e){}
if (flag==false) {
System.out.println("Initial request NOT possible!!! Please modify your inputs...");
st.upStatus("Initial request NOT possible!!! Please modity your inputs...") ;
thread.stop() ;
}
else {
animate.repaint();
System.out.println("Initial request possible...");
st.upStatus("Initial request possible, now pretend to allocate initial request to the process...");
}try{thread.sleep(400*speed);}catch(Exception e){}
processList = md.returnProcessList();
int request[] = new int[4];
while (!processFinishp()){
if (checkFlagList()) {
System.out.println("Deadlock!!!");
st.upStatus("Unsafe state!!! Initial request might generate deadlock...");
thread.stop();
break;
}
boolean pFlag = false;
int action = (int)(Math.random() * 2);
int process = (int)(Math.random() * 5);
if (action == 0 && !processList[process].returnFinish())
processList[process].Finish();
else{
int need [] = processList[process].returnNeed();
request = need;
for (int i=0;i<4;i++)
if (request[i] != 0){
pFlag = md.resourceRequest(process, request);
if (pFlag)
clearFlagList();
else if (!pFlag)
addFlagList(process);
break;
}
}
try{thread.sleep(400*speed);} catch (Exception e){}
}
System.out.println("program ended successfully...");
st.upStatus("Safe state... Initial request does not generate deadlock...");
}
private void addFlagList(int pid) {
flagList[pid] = 1;
}
private void clearFlagList() {
for (int i=0; i<5; i++)
flagList[i] = 0;
}
private boolean checkFlagList() {
boolean result = true;
for (int i=0; i<5; i++) {
if (flagList[i]==0) {
result = false;
break;
}
}
return result;
}
}