Download presentation
Presentation is loading. Please wait.
Published byΊησους Αγγελίδου Modified over 6 years ago
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.