Catching Race Conditions   An Extremely Difficult TaskStatically detecting race conditions in aprogram using multiple sema...
Problem StatementTwo groups, A and B, of processes exchangemessages.Each process in A runs a function T_A(), andeach proce...
Processes in group A    Processes in group BT_A()                   T_B(){                       {  while (1) {           ...
What is Exchange Message?When a process in A makes a message available,it can continue only if it receives a messagefrom a...
Watch for Race Conditions• Suppose process A1 presents its message for B  to retrieve. If A2 comes for message exchange  b...
First Attempt            sem A = 0, B = 0;        I am ready            int Buf_A, Buf_B;T_A()                        T_B(...
First Attempt: Problem (a)           Thread A           Thread B        B.signal()        A.wait()                        ...
First Attempt: Problem (b)    A1             A2          B1           B2B.signal()A.wait()                           A.sig...
What did we learn?If there are shared data items,always protect them properly.Without a proper mutual exclusion,race condi...
Second Attempt             sem   A = B = 0;             sem   Mutex = 1;             int   Buf_A, Buf_B;T_A()             ...
Second Attempt: Problem          A1          A2           B     B.signal()     A.wait()                              A.sig...
What did we learn?Improper protection is no better than noprotection, because it gives us an illusionthat data have been w...
Third Attempt                  sem Aready = Bready = 1;        ready to proceed  job done        sem Adone = Bdone = 0;   ...
Third Attempt: Problem          Thread A          Thread B     Buf_A =     Adone.signal()     Bdone.wait()ruin the origina...
What did we learn?Mutual exclusion for one group may notprevent processes in other groups frominteracting with a process i...
Fourth Attempt                  sem   Aready = Bready = 1;         ready to proceed job done         sem   Adone = Bdone =...
Fourth Attempt: Problem       A1             A2                   BBready.wait()Buf_A = …Adone.signal()                   ...
What did we learn?We use locks for mutual exclusion.The owner, the one who locked the lock,should unlock the lock.In the a...
ConclusionsDetecting race conditions is difficult as it is anNP-hard problem.Hence, detecting race conditions is heuristic...
Upcoming SlideShare
Loading in...5
×

Race conditions

416

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
416
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Race conditions

  1. 1. Catching Race Conditions An Extremely Difficult TaskStatically detecting race conditions in aprogram using multiple semaphores is NP-hard.Thus, no efficient algorithms are available. Wehave to use human debugging skills.It is virtually impossible to catch raceconditions dynamically because hardware mustexamine every memory access.So, we shall use a few examples to illustratesome subtle race conditions. 1
  2. 2. Problem StatementTwo groups, A and B, of processes exchangemessages.Each process in A runs a function T_A(), andeach process in B runs a function T_B().Both T_A() and T_B() have an infinite loopand never stop.In the following, we show execution sequencesthat cause race conditions. You can alwaysfind a correct execution sequence without raceconditions. 2
  3. 3. Processes in group A Processes in group BT_A() T_B(){ { while (1) { while (1) { // do something // do something Ex. Message Ex. Message // do something // do something } }} } 3
  4. 4. What is Exchange Message?When a process in A makes a message available,it can continue only if it receives a messagefrom a process in B who has successfullyretrieves A’s message.Similarly, when a process in B makes a messageavailable, it can continue only if it receives amessage from a process in A who hassuccessfully retrieves B’s message.How about exchanging business cards? 4
  5. 5. Watch for Race Conditions• Suppose process A1 presents its message for B to retrieve. If A2 comes for message exchange before B retrieves A1’s, will A2’s message overwrites A1’s?• Suppose B has already retrieved A1’s message. Is it possible that when B presents its message, A2 picks it up rather than A1?• Thus, the messages between A and B must be well-protected to avoid race conditions. 5
  6. 6. First Attempt sem A = 0, B = 0; I am ready int Buf_A, Buf_B;T_A() T_B(){ { int V_a; int V_b; while (1) { while (1) { V_a = ..; V_b = ..; B.signal(); A.signal(); A.wait(); B.wait(); Buf_A = V_a; Buf_B = V_b; V_a = Buf_B; V_b = Buf_A;} } Wait for your card! 6
  7. 7. First Attempt: Problem (a) Thread A Thread B B.signal() A.wait() A.signal()Buf_B has no value, yet! B.wait() Buf_A = V_a Oops, it is too late! V_a = Buf_B Buf_B = V_b 7
  8. 8. First Attempt: Problem (b) A1 A2 B1 B2B.signal()A.wait() A.signal() B.wait() B.signal() A.wait() Buf_B = . Race Condition A.signal()Buf_A = . Buf_A = . 8
  9. 9. What did we learn?If there are shared data items,always protect them properly.Without a proper mutual exclusion,race conditions are likely to occur.In this first attempt, both globalvariables Buf_A and Buf_B areshared and should be protected. 9
  10. 10. Second Attempt sem A = B = 0; sem Mutex = 1; int Buf_A, Buf_B;T_A() T_B() protection???{ int V_a; shake hands { int V_b; while (1) { while (1) { B.signal(); A.signal(); A.wait(); B.wait(); Mutex.wait(); Mutex.wait(); Buf_A = V_a; Buf_B = V_b; Mutex.signal(); Mutex.signal(); B.signal(); A.signal(); A.wait(); B.wait(); Mutex.wait(); Mutex.wait(); V_a = Buf_B; V_b = Buf_A; offer Mutex.signal(); Mutex.signal(); } My card }} } 10
  11. 11. Second Attempt: Problem A1 A2 B B.signal() A.wait() A.signal()race condition B.wait() Buf_A = .. Buf_B = .. B.signal() hand shaking with A.wait() wrong person A.signal() B.wait() 11 Buf_A = ..
  12. 12. What did we learn?Improper protection is no better than noprotection, because it gives us an illusionthat data have been well-protected.We frequently forgot that protection isdone by a critical section, which cannotbe divided.Thus, protecting “here is my card”followed by “may I have yours”separately is not good enough. 12
  13. 13. Third Attempt sem Aready = Bready = 1; ready to proceed job done sem Adone = Bdone = 0; int Buf_A, Buf_B; T_A() T_B() { int V_a; { int V_b; while (1) { while (1) {only one A can Aready.wait();Pass this point Bready.wait(); Buf_A = ..; Buf_B = ..; here is my card Adone.signal(); Bdone.signal(); let me have yours Bdone.wait(); Adone.wait(); V_a = Buf_B; V_b = Buf_A; Aready.signal(); Bready.signal(); } } } } 13
  14. 14. Third Attempt: Problem Thread A Thread B Buf_A = Adone.signal() Bdone.wait()ruin the original Bdone.signal()value of Buf_A Adone.wait() … = Buf_B Aready.signal() B is a slow ** loop back ** thread Aready.wait() Buf_A = … race condition … = Buf_A 14
  15. 15. What did we learn?Mutual exclusion for one group may notprevent processes in other groups frominteracting with a process in this group.It is common that we protect a shareditem for one group and forget otherpossible, unintended accesses.Protection must be applied uniformly toall processes rather than within groups. 15
  16. 16. Fourth Attempt sem Aready = Bready = 1; ready to proceed job done sem Adone = Bdone = 0; int Buf_A, Buf_B; T_A() wait/signal T_B() { int V_a; switched { int V_b; while (1) { while (1) {I am the only A Bready.wait(); Aready.wait(); Buf_A = ..; Buf_B = ..;here is my card Adone.signal(); Bdone.signal();waiting for yours Bdone.wait(); Adone.wait(); V_a = Buf_B; V_b = Buf_A; Job done & Aready.signal(); Bready.signal(); next B please } } } } 16
  17. 17. Fourth Attempt: Problem A1 A2 BBready.wait()Buf_A = …Adone.signal() Buf_B = … Bdone.signal() Adone.wait() … = Buf_A Bready.signal() Bready.wait() …… Hey, this one is for A1!!! Bdone.wait() … = Buf_B 17
  18. 18. What did we learn?We use locks for mutual exclusion.The owner, the one who locked the lock,should unlock the lock.In the above “solution,” Aready isacquired by a process A but released by aprocess B. This is risky!In this case, a pure lock is more naturalthan a binary semaphore. 18
  19. 19. ConclusionsDetecting race conditions is difficult as it is anNP-hard problem.Hence, detecting race conditions is heuristic.Incorrect mutual exclusion is no better than nomutual exclusion.Race conditions are sometimes very subtle.They may appear at unexpected places.Check the ThreadMentor tutorial pages formore details and correct solutions. 19
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×