Presentation is loading. Please wait.

Presentation is loading. Please wait.

Race Conditions David Ferry CSCI 3500 – Operating Systems

Similar presentations


Presentation on theme: "Race Conditions David Ferry CSCI 3500 – Operating Systems"— Presentation transcript:

1 Race Conditions David Ferry CSCI 3500 – Operating Systems
Saint Louis University St. Louis, MO 63103

2 CSCI 3500 - Operating Systems
Definition A race condition occurs whenever the output of a computation changes depending on the timing of execution. Suppose x=0 initially: Thread 1 Thread 2 u = x v = x u = u + 1 v = v * 2 x = u x = v What are the possible outcome values for x? CSCI Operating Systems

3 Linked List push() Example
Data Next NULL HEAD push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

4 CSCI 3500 - Operating Systems
push() Race Suppose two threads execute push() simultaneously: Data Next NULL HEAD Thread 1: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; Thread 2: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

5 CSCI 3500 - Operating Systems
push() Race Suppose two threads execute push() simultaneously: Data Next NULL HEAD current Thread 1: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; Thread 2: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

6 CSCI 3500 - Operating Systems
push() Race Suppose two threads execute push() simultaneously: Data Next NULL HEAD current current Thread 1: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; Thread 2: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

7 CSCI 3500 - Operating Systems
push() Race Suppose two threads execute push() simultaneously: Data Next Data Next Data Next Data HEAD Next NULL current current Thread 1: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; Thread 2: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

8 CSCI 3500 - Operating Systems
push() Race Suppose two threads execute push() simultaneously: Data Next Data Next Data Next Data Data HEAD Next NULL Next NULL current current Thread 1: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; Thread 2: push( node* newNode ){ node* current = HEAD; while( current->next != NULL ){ current = current->next; } current->next = newNode; newNode->next = NULL; CSCI Operating Systems

9 CSCI 3500 - Operating Systems
At least basic arithmetic is safe, right? What could go wrong? Thread 1: Thread 2: x++ x++ CSCI Operating Systems

10 Not even increment is safe…
Suppose x=0 initially: Thread 1: Thread 2: x++ x++ Becomes: load x to register load x to register increment register increment register store reg. to memory store reg. to memory CSCI Operating Systems


Download ppt "Race Conditions David Ferry CSCI 3500 – Operating Systems"

Similar presentations


Ads by Google