Answer to Question #176881 in C++ for Tisha

Question #176881

Consider a system consisting of n processes/threads and k identical resources where k<n. Each

thread of which has a unique priority number. Each thread request for a single resource at a time. If

the resource is available then it is immediately allocated. If the resource is not available then the

thread is made to wait. Thus in the course of time, multiple threads may wait to access the resource.

Once the resource is available, the resource is given to the waiting thread with the highest priority.

Develop a monitor that achieves this. Specifically, the monitor should the following methods:


void request-resource(int thr_priority); // Here thr_priority is the priority of the invoking thread

void release-resource();


1
Expert's answer
2021-03-30T22:37:29-0400
#include<iostream>
 
 using namespace std;
 
 int main()
 {
   int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
   cout<<"Enter Total Number of Process:";
   cin>>n;
 
   cout<<"\nEnter Burst Time and Priority\n";
   for(i=0;i<n;i++)
   {
       cout<<"\nP["<<i+1<<"]\n";
       cout<<"Burst Time:";
       cin>>bt[i];
       cout<<"Priority:";
       cin>>pr[i];
       p[i]=i+1;          //contains process number
   }
 
   //sorting burst time, priority and process number in ascending order using selection sort
   for(i=0;i<n;i++)
   {
       pos=i;
       for(j=i+1;j<n;j++)
       {
           if(pr[j]<pr[pos])
               pos=j;
       }
 
       temp=pr[i];
       pr[i]=pr[pos];
       pr[pos]=temp;
 
       temp=bt[i];
       bt[i]=bt[pos];
       bt[pos]=temp;
 
       temp=p[i];
       p[i]=p[pos];
       p[pos]=temp;
   }
 
   wt[0]=0;           //waiting time for first process is zero
 
   //calculate waiting time
   for(i=1;i<n;i++)
   {
       wt[i]=0;
       for(j=0;j<i;j++)
           wt[i]+=bt[j];
 
       total+=wt[i];
   }
 
   avg_wt=total/n;     //average waiting time
   total=0;
 
   cout<<"\nProcess\t   Burst Time   \tWaiting Time\tTurnaround Time";
   for(i=0;i<n;i++)
   {
       tat[i]=bt[i]+wt[i];    //calculate turnaround time
       total+=tat[i];
       cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t   "<<wt[i]<<"\t\t\t"<<tat[i];
   }
 
   avg_tat=total/n;    //average turnaround time
   cout<<"\n\nAverage Waiting Time="<<avg_wt;
   cout<<"\nAverage Turnaround Time="<<avg_tat;
 
   return 0;
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS