Chen Minqi
[1] Only occur between two or more threads. We cannot have race conditions inside a single thread.
[2] Not CPUs / cores.
[3] Don't use 'memory'.
Suppose two identical tasks update a shared variable tmp using tmp++
Can a race condition occur? Why or why not?
LD R1, tmp # Load tmp into R1
ADD R1, R1, 1 # Increment
SW R1, tmp # Store result into memoryIf yes, list the advantages and disadvantages.
Effective?
Yes (for uni-processor systems).
Reason: see previous question.
Advantages?
Easy to implement.
Disadvantages?
Requires kernel privileges.
A hung process can block the entire system.
Extra help needed for multi-processor systems.
Effective?
No.
Race condition on the lock variable itself.
(see lecture 4 slide pg 18)
Advantages?
-
Disadvantages?
-
Effective?
Yes (but...)
Not possible to have a race condition on the turn variable, and only one CS can be run depending on the variable itself.
Advantages?
Simple implementation.
Disadvantages?
Busy waiting.
Possible starvation.
Effective?
Yes. If two processes want to enter CS, the first one to indicate interest (turn!=process) will enter first.
Advantages?
Purely software based solution.
Non-CS code will not block another CS.
Disadvantages?
Busy waiting.
Harder to understand and implement, especially for more than 2 processes.
(Did you know: it took people two decades to arrive at Persons's algorithm?)
Nothing to do with mutex.
It deals with deadlocks instead :]
interested[0] = true;
turn = 0;
while(interested[1] == true &&
turn == 0); /* loop */
/* cs */
interested[0] = false;interested[1] = true;
turn = 1;
while(interested[0] == true &&
turn == 1); /* loop */
/* cs */
interested[1] = false;interested[0] = true;
while(interested[1] == true);
while(interested[1] == true);
while(interested[1] == true);
while(interested[1] == true);
/* will not reach here
interested[0] = false; */
interested[1] = true;
while(interested[0] == true);
while(interested[0] == true);
while(interested[0] == true);
while(interested[0] == true);
/* will not reach here
interested[1] = false; */Adding the turn variable means that only one process can be stuck in the while loop. The other will proceed to their CS (and eventually change turn variable again to free the other process).