Upcoming SlideShare
×

# Race conditions

895 views

Published on

Published in: Technology, News & Politics
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
895
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
20
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