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

Please help me answer this question. Develop a program in C++ that will receive

ID: 3833876 • Letter: P

Question

Please help me answer this question.

Develop a program in C++ that will receive the following input from the keyboard:

a.         Number of processes and the corresponding arrival time and finish time for each process.

b.         The total number of resources, the value of resource vector, request matrix and allocation matrix.

Having these information, your program should be able to show whether or not deadlock will occur. If deadlock occurs, all processes that involve in the deadlock should be listed. Assuming all processes are running in a uniprocessor environment, your program should be able to show how the processes are scheduled until completion based on two uniprocessor scheduling namely Shortest Process Next and Round Robin (Q=1). Then your program is required to calculate the turnaround time for each process and the mean of turnaround time and display which of the two algorithm is more efficient.

Explanation / Answer

Answer: See the code below:

---------------------------------

#include <iostream>
using namespace std;

int main() {
   int num_processes; //number of processes
   cout<<"Enter number of processes:";
   cin>>num_processes;
   double* arrival_times=new double[num_processes]; //array for arrival times
   double* finish_times=new double[num_processes]; //array for finish times
   //read arrival and finish times
   for(int i=0;i<num_processes;i++)
   {
       cout<<"Enter arrival time for process "<<(i+1)<<":";
       cin>>arrival_times[i];
       cout<<"Enter finish time for process "<<(i+1)<<":";
       cin>>finish_times[i];
   }
   int num_resources; //number of resources
   cout<<"Enter number of resources:";
   cin>>num_resources;
   double* resource_vector=new double[num_resources];
   //read resource vector
   for(int i=0;i<num_resources;i++)
   {
       cout<<"Enter value of resource "<<(i+1)<<":";
       cin>>resource_vector[i];
   }
   double request_mat[num_processes][num_resources]; //request matrix
   double allocation_mat[num_processes][num_resources]; //allocation matrix
   //read request matrix
   cout<<"Enter request matrix:"<<endl;
   for(int i=0;i<num_processes;i++)
   {
       for(int j=0;j<num_resources;j++)
       {
           cin>>request_mat[i][j];
       }
   }
   //read allocation matrix
   cout<<"Enter allocation matrix:"<<endl;
   for(int i=0;i<num_processes;i++)
   {
       for(int j=0;j<num_resources;j++)
       {
           cin>>allocation_mat[i][j];
       }
   }

   //display data
   cout<<"Process Allocation Request:"<<endl;
   for(int i=0;i<num_processes;i++)
   {
       cout<<"p"<<(i+1)<<" ";
       for(int j=0;j<num_resources;j++)
       {
           cout<<allocation_mat[i][j]<<" ";
       }
       cout<<" ";
       for(int j=0;j<num_resources;j++)
       {
           cout<<request_mat[i][j]<<" ";
       }
       cout<<endl;
   }
   cout<<"Resources available:"<<endl;
   for(int i=0;i<num_resources;i++)
   {
       cout<<resource_vector[i]<<" ";
   }
   cout<<endl;
   //detection of deadlock
   double finish_vector[num_processes];
   for(int i=0;i<num_processes;i++)
   {
       finish_vector[i]=0;
   }
   double requirement_mat[num_processes][num_resources];
   for(int i=0;i<num_processes;i++)
   {
       for(int j=0;j<num_resources;j++)
       {
           requirement_mat[i][j]=request_mat[i][j] - allocation_mat[i][j];
       }
   }
   bool dead_lock=true;
   while(dead_lock)
   {
       dead_lock=false;
       for(int i=0;i<num_processes;i++)
       {
           int count=0;
           for(int j=0;j<num_resources;j++)
           {
               if((finish_vector[i]==0) && (requirement_mat[i][j] <= resource_vector[j]))
               {
                   count++;
                   if(count == num_resources)
                   {
                       for(int k=0;k<num_resources;k++)
                       {
                           resource_vector[k]+=allocation_mat[i][j];
                           finish_vector[i]=1;
                           dead_lock=true;
                       }
                       if(finish_vector[i]==1)
                       {
                           i=num_processes;
                       }
                   }
               }
           }
       }
   }
   int j=0;
   dead_lock=false;
   int dead_lock_processes[num_processes];
   for(int i=0;i<num_processes;i++)
   {
       if(finish_vector[i]==0)
       {
           dead_lock_processes[j]=i;
           j++;
           dead_lock=true;
       }
   }
   if(dead_lock)
   {
       cout<<"Deadlock occurred!"<<endl;
       cout<<"Processes involved in the deadlock are:";
       for(int i=0;i<num_processes;i++)
       {
           cout<<"p"<<dead_lock_processes[i]<<" ";
       }
       cout<<endl;
   }
   else
   {
       cout<<"No deadlock occurred!"<<endl;
   }
   return 0;
}

------------------------------------------

Output:

-----------------------------------

Enter number of processes:3
Enter arrival time for process 1:1
Enter finish time for process 1:3
Enter arrival time for process 2:2
Enter finish time for process 2:4
Enter arrival time for process 3:4
Enter finish time for process 3:6
Enter number of resources:4
Enter value of resource 1:1
Enter value of resource 2:5
Enter value of resource 3:3
Enter value of resource 4:7
Enter request matrix:
2
5
7
1
8
9
12
12
34
5
6
9
Enter allocation matrix:
5
6
7
43
78
43
2
1
5
6
7
9
Process       Allocation   Request:
p1       5 6 7 43        2 5 7 1
p2       78 43 2 1        8 9 12 12
p3       5 6 7 9        34 5 6 9
Resources available:
1 5 3 7
No deadlock occurred!

--------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote