Race Conditions: A Case Study Steve Carr, Jean Mayo and Ching-Kuang Shene * Department of Computer Science Michigan Technological University 1400 Townsend Drive Houghton, MI 49931 E-mail: {carr,jmayo,shene}@mtu.edu ABSTRACT Since detecting race conditions in a multithreaded or multiprocess program is an NP-complete problem, there is no efficient algorithm that can help detect race conditions in a program. As such, there are no easy- to-use pedagogical tools. Most operating systems and concurrent programming textbooks only provide a formal definition and some trivial examples. This is insufficient for students to learn how to detect race conditions. This paper attempts to fill this gap by presenting a set of well- organized examples, each of which contains one or more race conditions, for instructors to use in the classroom. This set of materials has been classroom tested for two years and the student’s reaction has been very positive. 1. INTRODUCTION Race condition detection is an important topic in an operating systems or concurrent programming course [1,2,5,9-13]. Our experience shows that it is easy to provide students with a formal definition; but it is always difficult for students to pinpoint race conditions in their programs [9,10]. This is largely due to the lack of realistic examples and the dynamic behavior of a multithreaded or multiprocess program. Worse, race conditions cannot be detected at run time because a detection program must monitor every memory access. Additionally, statically detecting race conditions in programs that use multiple semaphores is NP-complete [7], meaning an efficient solution is unlikely. If the synchronization mechanism is weaker than semaphores, an exact and efficient algorithm can be found [6]; otherwise, only heuristic algorithms that scan the source programs statically are available [3,4]. Unfortunately, a heuristic algorithm can only find potential race conditions, meaning the detection program may report many race conditions that are not actually race conditions. As a result, there are few pedagogical aids designed for teaching students about race conditions. Since there are no reasonable algorithms and universally applicable techniques that can help students pinpoint race conditions, they are left frustrated trying to debug their programs. A race condition is defined as the situation in which multiple threads or processes read and write a shared data item and the final result depends on the order of execution. An obvious example is updating a shared counter as follows: * Corresponding author