20090924 - Race Conditions Multiple concurrent activities accessing shared data leads to race conditions in which the result of execution depends

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Race Conditions Multiple concurrent activities accessing shared data leads to race conditions , in which the result of execution depends on the exact timings of accesses to this data.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Race Conditions: The Cause The basic cause of race conditions is that the execution of one thread be interrupted at any time by the execution of another thread. This interruption can occur as a result of: Concurrent accesses to shared data by threads executing on separate CPUs. Preemption of the CPU on a single-CPU system. Race conditions can even occur under non-preemptive schedul- ing, if a thread can cache data in private registers.
Background image of page 2
Example: Incrementing a Shared Variable Consider the following program fragment: int shared = 0; void increment() { shared = shared+1; } Does increment() always increment shared ?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A “Lower-level” View The previous program fragment typically ends up being equiv- alent to the following version: int shared = 0; void increment() { int local; local = shared; local++; shared = local; }
Background image of page 4
On a Multiple-CPU System Thread 1 Thread 2 local = shared local++ local = shared shared = local local++ shared = local What is the result?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Single-CPU, Preemptive Scheduling Thread 1 Thread 2 local = shared local++ local = shared (preempted) local++ shared = local shared = local
Background image of page 6
Single-CPU, Non-Preemptive Scheduling int shared = 0; void increment() { shared = shared+1; yield(); } What can happen here?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Single-CPU, Non-Preemptive Scheduling Compilers often generate code that postpones “writing back” data to shared variables, to reduce the total number of ac- cesses to shared memory. int shared = 0; void increment() { int local; local = shared; local++; yield(); shared = local; (‘‘write-back’’ delayed) }
Background image of page 8
Requirements for Race Conditions There are two requirements that must be satisfied for a race condition to be possible: 1. One activity can be interrupted by another. 2. The two activities share data. If no interruptible activities, no race conditions. If no shared data, no race conditions. Race conditions are eliminated by using synchronization to control interruptibility whenever shared data is accessed.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Interruptibility in an OS There are three basic situations in an OS where one activity can be interrupted by another: Preemption of an executing thread by an interrupt service routine. Interruption of one thread by another due to explicit yield or preemptive scheduling. Concurrent access to shared memory by multiple CPUs or DMA devices. Different synchronization techniques are used in each case.
Background image of page 10
Interrupt Service Routines Device drivers are one place where great attention has to be paid to the possibility of race conditions. Requests arrive from the upper layers of the OS in the top half of the driver. Requests are placed in a
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/19/2009 for the course CSE 306 taught by Professor Kifer,m during the Spring '08 term at SUNY Stony Brook.

Page1 / 51

20090924 - Race Conditions Multiple concurrent activities accessing shared data leads to race conditions in which the result of execution depends

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online