A process is a series of actions, changes, or procedures that bring about a result. A process may be a thread in execution on a computer, an action being performed, or any other actions, series, changes or procedures which being about a result.
As a mechanism to provide greater throughput in execution, processes are often executed concurrently. Thus, during execution of one process another process may also execute. While concurrent execution leads to greater performance, if more than one process accesses a data element, then the data element may become inconsistent. Critical data is data which may become inconsistent. More formally, critical data is data that is accessible by multiple processes where at least one access to the data by a process changes the value of the data. A race condition on the critical data occurs when at least one process accesses the critical data without synchronization with the other processes and at least one process changes the value of the critical data.
A common example of a race condition occurring is when two people access the same bank account using automatic teller machines (ATM) located in different parts of town. For this example, suppose the first person wants to deposit $20 to the bank account (process A) and the second person wants to withdraw $5 from the bank account (process B). Also suppose the initial amount in the bank account is $40. In the first step, process A reads the bank account value (i.e., $40). In the second step, process A accepts the deposit and adds $20 to the amount process A read (i.e., $40). In this same second step, process B reads the bank account value (i.e., $40). In the third step, process A updates the bank account value to $60 and process B subtract $5 from the amount process B read (i.e., $40). In the fourth step, process B updates the bank account value to $35. The result of this execution is that rather than having a correct balance of $55, the final account balance is $35.
One method of preventing a race condition is through synchronization. In the above example, suppose that before process A starts executing, process A first informs process B that it is changing the critical data. B receives the information and waits until A informs B that A has finished accessing the critical data. A then executes while B waits. After receiving the message from A that A has completed executing, B begins. The result of this execution is that the final correct balance of $55 is achieved.
There are several mechanisms for synchronization. These mechanisms include the use of messages, locks, semaphores, monitors, etc. Basically synchronization requires that a process must first acquire the rights to access the critical data before accessing the critical data. This may be done through preventing access to the other processes or simply informing the other processes that it is accessing the critical data. This is not a requirement if all of the processes merely read the critical data.
Mechanisms for detecting a race condition are based on determining whether a process has acquired the rights to access the critical data before accessing the data. Past mechanisms for detecting a race condition are based upon a dynamic analysis of the system. Thus, these past mechanisms only detect a race condition during the execution of the processes.