Files related to my CS self study
Yes, it is quite simple.
flag
will end up with 0
The loops run twice, but flag still ends up with 0.
The interrupt must not happen while the flag is being acquired. For higher values of bx
, the program never works correctly, unless the interrupt frequency is lower that the whole time a thread needs to finish.
Test and set the lock variable, if the lock variable was 0 before setting, critical section starts. Otherwise wait in loop. Releasing lock is simply setting 0 to the lock variable.
It does work correctly all the time. But the CPU waste is high if the interrupt frequency is low.
It prevents acquisition of a acquired lock, but fairness is not guaranteed here.
It makes sense. The turn variable is there to make sure, two thread doesn’t get stuck infinitely in attempt to acquire a lock. It’s like one thread saying to the other: ‘I am about to attempt acquiring the lock. But if I can’t (because you already set your flag to 1) then you can go ahead even though my flag is set to 1.
It works. Can’t see any different behavior.
We can use interrupt frequency from 1 to high number to show that no matter where one thread gets interrupted, it still produces correct result.
It is like described in the chapter. It does spend time on spin-waiting just like any spinning lock.
It still works with more threads.
With any low interrupt frequency, yeild
can save about 40-50 percent CPU time.
It does some savings only if writing or the xchg
is considered expensive