Suppose there is a resource that can be used by up to 2 proc

Suppose there is a resource that can be used by up to 2 processes at a time. The method Access provides access to this resource. To ensure that only two processes can access resource at a time, the processes are not allowed to call Access directly. Instead, they call the function Request which in turn allows them to access the resource. The outline of Request is as follows Request {YOUR CODE Access(); 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 Access is irrelevant to your assignment. Your job is to make sure that at most two of them can access it at a time) Solve this problem using test and set instruction. This solution may use busy waiting and does not have to guarantee fairness. Solve this problem using semaphores. This solution may not use busy waiting and has to guarantee fairness.

Solution

struct lock {
int held = 0;
};

void acquire (lock) {
while (test-and-set(&lock->held));
}

void release (lock) {
lock->held = 0;
}

bool test_and_set (bool *flag) {

bool old = *flag;
*flag = True;
return old;
}

Request
{
   struct lock l;
   acquire(l);
   Access();
   release(l);
}

 Suppose there is a resource that can be used by up to 2 processes at a time. The method Access provides access to this resource. To ensure that only two proces

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site