G53OPS - Operating Systems

This course is run at the The University of Nottingham within the School of Computer Science & IT. The course is run by Graham Kendall (EMAIL : gxk@cs.nott.ac.uk)


Race Conditions

This section is based on (Tanenbaum, 1992), pages 33-34.

It is sometimes necessary for two processes to communicate with one another. This can either be done via shared memory or via a file on disc. It does not really matter.

We are not discussing the situation where a process can write some data to a file that is read by another process at a later time (maybe days, weeks or even months). We are talking about two processes that need to communicate at the time they are running.

Take, as an example, one type of process (i.e. there could be more than one process of this type running) that checks a counter when it starts running. If the counter is at a certain value, say x, then the process terminates as only x copies of the process are allowed to run at any one time.

This is how it works

· The process starts
· The counter, i, is read from the shared memory
· If the i = x the process terminates else i = i + 1
· i is written back to the shared memory

Sounds okay. But consider this scenario

· Process 1, P1, starts
· P1 reads the counter, i1, from the shared memory. Assume i1 = 3 (that is three processes of this type are already running)
· P1 gets interrupted and is placed in a ready state
· Process 2, P2, starts
· P2 reads the counter, i2, from the shared memory; i2 = 3
· Assume i2 < x so i2 = i2 +1 (i.e. 4)
· i2 is written back to shared memory
· P2 is moved to a ready state and P1 goes into a running state
· Assume i1 < x so i1 = i1 +1 (i.e. 4)
· i1 is written back to the shared memory

We now have the situation where we have five processes running but the counter is only set to four

This problem is known as a race condition.

Last Page Back to Main Index Next Page


 


 Last Updated : 08/01/2002