inefficient spinning Flashcards

(4 cards)

1
Q

do {
while (TestAndSet(&lock)) ;
[critical section]
lock = FALSE; // Release lock
[remainder section]
} while (TRUE);

even though this code achieves mutual exlusion there is a flaw, what is it?

A

This guarantees mutual exclusion but with a high cost: that while loop spins constantly (using up CPU cycles) until it manages to enter the critical section.
This is a huge waste of CPU cycles and is not acceptable,
particularly for user processes where the critical section maybe occupied for some time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to avoid cpu constanly cycling

A

we implement some mechanism whereby it sends itself to sleep and then is awoken only when there is a chance it can proceed Functions such as sleep() and wakeup() are often available
via a threading library or as kernel service calls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

code for Mutual Exclusion with sleep()

A

do {
while (TestAndSet(&lock)) { // If we can’t get the lock, sleep.
sleep();
}
[critical section]
wake_up(all); // Wake all sleeping threads.
lock = FALSE; // Release lock
[remainder section]
} while (TRUE);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what does this code show
do {
while (TestAndSet(&lock)) { sleep.
sleep();
}
[critical section]
wake_up(all); // Wake all sleeping threads.
lock = FALSE; // Release lock
[remainder section]
} while (TRUE);

A

The Missing Wake-up Problem
Mutual Exclusion with sleep(): A first Attempt
If constant spinning is a problem, suppose a process puts itself
to sleep in the body of the spin.
Then whoever won the competition (and therefore entered the
critical section) will wake all processes before releasing the
lock - so they can all try again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly