Suppose there is a resource that can be used by up to 2 processes at a time. The
ID: 3777412 • Letter: S
Question
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.Explanation / Answer
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);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.