Suppose there is a resource that can be used by upto 2 processes at a time. The
ID: 663991 • Letter: S
Question
Suppose there is a resource that can be used by upto 2 processes at a time. The method
AccessResource provides access to this resource. To ensure that only two processes can access resource
at a time, the processes are not allowed to call AccessResource directly. Instead, they call the function
RequestResource which in turn allows them to access the resource. The outline of RequestResource is as
follows:
RequestResource
{
YOUR CODE
AccessResource();
YOUR CODE
}Your task is to fill in the portion marked with `YOUR CODE' so that only two processes can access the
resource at a time even though more than two can request it at a time. (Note that what goes inside
AccessResource is irrelevant to your assignment. Your job is to make sure that at most two of them can
access it at a time).
a) Solve this problem using testset instruction. This solution may use busy waiting and does not
have to guarantee fairness.
b) Solve this problem using semaphores. This solution may not use busy waiting and has to
guarantee fairness.
Please code in C++
Explanation / Answer
static int totalNoOfProcess=0; //static variable to store total no of process accessing the resource
RequestResource()
{
totalNoOfProcess+=1; //increment the process count
if(totalNoOfProcess<2){ //if total no of process requesting the resource is less than 2
AccessResource(); //then provide access to the resource
}else{
cout<<"Cannot access resource"; //max. no. of process reached for the resource
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.