Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Deadlock Free Resource Management) – a java simulator of banker algorithm progr

ID: 3691796 • Letter: #

Question

(Deadlock Free Resource Management) – a java simulator of banker algorithm program

public class Dfrm {
  
   /*
   * Parameters:
   *(a) numResourceTypes -the number of resource
   *types managed by the resource manager and these resource
   *types are named 0, 1, ..., numResourceTypes-1.
   *
   *(b) array numInstances[] -the array has numResoruceType elements,
   *and for each i = 0, ..., numResourceType-1, element numInstances[i] records
   *the total number of instances of resource type i. Each element should be positive.
   *In this project, the maximum number of resource types should not exceed 20.  
   *
   *Return:
   *(a) 0 -success
   *(b) non 0 -failure (due to invalid parameters)*/
  
   public Dfrm(int numResourceTypes, int[] numInstances){
      
       //TODO
   }
  
   /* This function is called by a thread to declare its maximum resource needs
   * during its whole lifetime. Every thread should call this function to declare
   * its maximum need before it starts requesting resource. In this project,
   * the maximum number of threads should not exceed 20.
   *
   * Parameters:
   * (a) threadID - a unique ID of the calling thread.
   * (b) array numInstances[] - the array has numResourceTypes
   * (which is specified in dfrm_init) elements, and for each i = 0, ...,
   * numResourceTypes - 1, element numInstances[i] records the number of
   * instances of resource type i that the calling thread may need
   * in its lifetime. For each resource type, the number of declared instances
   * should be non-negative and should not be greater than the total number
   * of instances specified in dfrm_init.
   *
   *Return:
   *(a) 0 -success
   *(b) non 0 -failure (due to invalid parameters)*/
  
   public boolean declare(int threadID, int[] numInstances){  
       //TODO
   }
  
   /* This function is called by a thread to request for some resource instances.
   * Parameters:
   * (a) threadID - ID of the calling thread.
   * (b) array numInstances[] - the array has numResourceType
   * (which is specified in dfrm_init) elements, and for each i = 0, ...,
   * numResourceType-1, element numInstances[i] records the number of instances of
   * resource type i that the calling thread is requesting. For each resource type,
   * the number of assigned and requested instances should not be greater than
   * the number of instances the thread has claimed in dfrm_declare. In response
   * to this function call, if request is invalid (according to the banker's algorithm),
   * the function returns - 1 to decline the request; if the request is valid,
   * but the currently available resource cannot satisfy the request or it is
   * not safe (according to the banker's algorithm), the calling thread should be
   * suspended (i.e., blocked) until the requested resources become available and
   * the assignment becomes safe (according to the banker’s algorithm).
   *
   *Return:
   *(a) 0 -success
   *(b) non 0 - invalid parameters or invalid request (according to the banker’s algorithm)*/
  
   public boolean request(int threadID, int[] numInstances){
       //TODO
   }
  
   /*
   * This function is called by a thread to release some resource instances that
   * it currently holds.
   *
   * Parameters:
   * (a) threadID - ID of the calling thread.
   * (b) array numInstances[] - the array has numResourceType
   * (which is specified in dfrm_init) elements, and for each i = 0, ...,
   * numResourceType-1, element numInstances[i] records the number of instances of
   * resource type i that the calling thread wants to release. For each resource
   * type i, numInstances[i] should not be greater than the number of instances
   * of type i that the thread currently hold.
   *
   *Return:
   *(a) 0 -success
   *(b) non 0 - invalid parameters*/
  
   public boolean release(int threadID, int[] numInstances){
       //TODO
   }
  
   /* This function is called right before a thread finishes. In response, all the
   * resource instances held by the calling thread should be released.
   *
   * Parameter:
   * threadID - ID of the calling thread
   *
   *Return:
   *(a) 0 -success
   *(b) non 0 - invalid parameters*/
   public boolean finish(int threadID){
       //TODO
   }
  
}

/////////////////////////////// Sample Thread //////////////////////////////////

public class SampleThread extends Thread{
   private Dfrm myDfrm;
   private int id;

   SampleThread(Dfrm myDfrm, int id){
       this.myDfrm=myDfrm;
       this.id=id;
   }
  
   public void run(){

       int[] numIns0={5,5,5};
       int[] numIns1={3,4,2};
       int[] numIns2={2,1,3};

       try{
       myDfrm.declare(id,numIns0);
       System.out.printf("Thread %d has made a declare ", id);

       System.out.printf("Thread %d raises the 1st request ", id);
       if(myDfrm.request(id,numIns1))
           System.out.printf("Thread %d has been granted with the 1st request ",id);
       else
           System.out.printf("thread %d fails in the request ",id);

       Thread.sleep(5000);

       System.out.printf("thread %d raises the 2nd request ",id);
        if(myDfrm.request(id,numIns2))
           System.out.printf("Thread %d has been granted with the 2nd request ",id);
        else
            System.out.printf("Thread %d fails in the request ",id);
      
       Thread.sleep(5000);

       if(myDfrm.release(id,numIns1))
           System.out.printf("Thread %d releases resources of its 1st request ",id);

       Thread.sleep(5000);
         
       myDfrm.finish(id);
       System.out.printf("Thread %d has finished ",id);
       }catch(Exception e){
       }
      

   }

}

//////////////////////////////// DEOM TEST ////////////////////////
public class DfrmDemo {

   public static void main(String[] args) {
       int[] numRes={10,10,10};
       Dfrm myDfrm=new Dfrm(3,numRes);

       for(int i=0;i<3;i++){
           SampleThread st=new SampleThread(myDfrm,i);
           st.start();
           try{
               Thread.sleep(1000);
           }catch(Exception e){
           }
       }      
   }

}

/////////////////////////Output ///////////////////////////

Explanation / Answer

public class Main {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

int n = Integer.parseInt(JOptionPane.showInputDialog("Number Of Process:"));

int m = Integer.parseInt(JOptionPane.showInputDialog("Resource Type Number:"));

int available[] = new int[m];

int max[][] = new int[n][m];

int allocation[][] = new int[n][m];

int need[][] = new int[n][m];

String sequence = "";

for(int i = 0; i < m; i++)

{

available[i] = Integer.parseInt(JOptionPane.showInputDialog("Number Of Available Resource " + (i) + ":"));

}

for(int i = 0; i < n; i++)

{

for(int j = 0; j < m; j++)

{

allocation[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Allocation P " + (i) + " for R " + (j) + ":"));

}

}

for(int i = 0; i < n; i++)

{

for(int j = 0; j < m; j++)

{

max[i][j] = Integer.parseInt(JOptionPane.showInputDialog("MAX P " + (i) + " for R " + (j) + ":"));

need[i][j] = max[i][j] - allocation[i][j];

}

}

int work[] = available;

boolean finish[] = new boolean[n];

for(int i = 0; i < n; i++)

{

finish[i] = false;

}

boolean check = true;

while(check)

{

check = false;

for(int i = 0; i < n; i++)

{

if(!finish[i])

{

int j;

for(j = 0; j < m; j++)

{

if(need[i][j] > work[j])

{

break;

}

}

if(j == m)

{

for(j=0; j < m; j++)

{

work[j] = work[j] + allocation[i][j];

}

finish[i] = true;

check = true;

sequence += i + ", ";

}

}

}

}

int i;

for(i = 0; i < n; i++)

{

if(!finish[i])

break;

}

if(i==n)

{

JOptionPane.showMessageDialog(null, "SAFE And Sequence is:"+sequence);

}

else

{

JOptionPane.showMessageDialog(null, "DEAD LOCK");

}

}

}