The race or race hazard condition is the behavior of electronic devices, software, or other systems in which output depends on the order or time of other uncontrollable events. This becomes a bug when events do not occur in the desired sequence of programmers.
The term is derived from a paper title presented at the University of Reading to the 2nd National Symposium of Logic Design by D. Zissos on March 28, 1969 under the auspices of The British Computer Society. Race hazard is a temporary circuit, which under certain changes of the input signal in a circuit with a certain delay, can occur at the gate output.
Racing conditions can occur mainly in logic circuits, and in software programs that are distributed in multithread or.
Video Race condition
Electronics
Typical examples of race conditions can occur when logic gates combine signals that have traveled a different path from the same source. The input to the gate may change at slightly different times in response to changes in the source signal. The output can, for a brief period, change to an undesirable state before returning to a designated state. Certain systems can tolerate such disturbances but if these outputs serve as clock signals for further systems containing memory, for example, the system can quickly depart from its designed behavior (valid, temporary error becomes permanent error).
Consider, for example, two-input AND gate gateways logical A signals at one input and its negation, NOT A, on other inputs. In theory, output (A AND NOT A) can not be true. However, if the change in value A takes longer to spread to the second input than the first when A turns from false to true, then the short period will occur as long as the two inputs are correct, so the gate output will also be true.
Design techniques such as Karnaugh maps encourage designers to recognize and eliminate race conditions before they cause problems. Often logic redundancy can be added to eliminate some types of races.
As well as this problem, some logic elements can enter a metastable state, which creates further problems for the circuit designer.
Important and insignificant form
The critical race condition occurs when the order in which the internal variable is changed determines the eventual state that the state machine will end in.
The non-critical race condition occurs when the order in which the internal variable is changed does not specify the final state state machine will end.
Static, dynamic, and important form
The state of static race occurs when the signal and its complement are combined together.
The dynamic race condition occurs when generating multiple transitions when only one is intended. They are due to interaction between gates. It can be removed by using no more than two levels of gating.
The important race condition occurs when the input has two transitions in less than the total propagation time of the feedback. Sometimes they are cured using an inductive delay line element to effectively increase the duration of time from the input signal.
Maps Race condition
Software
Race conditions appear in the software when the app depends on the order or timing of the process or thread to operate properly. Like electronics, there is a critical race condition that results in invalid execution and bugs. Critical race conditions often occur when processes or strands rely on several shared conditions. Operations under common conditions are an important part that must be mutually exclusive. Failure to comply with this rule opens the possibility of damaging the shared state.
The memory models defined in the C11 and C 11 standards use the term "data race" for race conditions caused by potentially co-operating at shared memory locations, of which at least one writes. C or C programs that contain race data have undefined behavior.
The race conditions have a reputation for difficult reproduction and debugging, because the end result does not specify and depends on the relative time between the threads that interfere. Problems that occur in the production system may be lost while running in debug mode, when additional logging is added, or when installing the debugger, commonly referred to as "Heisenbug". Therefore it is better to avoid racing conditions with careful software design rather than trying to fix it afterwards.
Example
As a simple example, let's assume that the two threads want to raise the value of global integer variables one at a time. Ideally, the following sequence of operations will occur:
In the case shown above, the final value is 2, as expected. However, if two threads run simultaneously without locking or syncing, the result of the operation can be wrong. The alternate sequence of operations below shows this scenario:
In this case, the final value is 1 instead of the expected result of 2. This is because here the increase operation is not mutually exclusive. A mutually exclusive operation is one that can not be interrupted while accessing multiple resources such as memory locations.
Computer security
Many software racing conditions are related to the security implications of computers. The race conditions allow attackers with access to shared resources to cause other actors who exploit those resources to be non-functional, resulting in effects including denial of service and privilege escalation.
A special type of racing condition involves checking the predicate (eg for authentication), then acting on a predicate, while the state can change between check time and usage time TOCTTOU ) bug is created.
The condition of the race is also deliberately used to create a random number generator of hardware and functions that can not be physically delayed. PUF can be created by designing circuit topologies with identical paths to nodes and relying on manufacturing variations to randomly determine which path will be completed first. By measuring each set of racing condition results produced by a particular circuit, profiles can be collected for each circuit and kept confidential to then verify the identity of the circuit.
File system
Two or more programs may collide in their attempt to modify or access the file system, which may result in data corruption or privilege escalation. File locking provides a commonly used solution. More complicated drugs involve organizing the system in such a way that a unique process (running daemons or the like) has exclusive access to files, and all other processes that need to access the data in the file do so only through interprocess communication with that one process. This requires synchronization at the process level.
Different forms of race conditions exist in file systems where unrelated programs can affect each other by suddenly using available resources such as disk space, memory space, or processor cycles. Software that is not carefully designed to anticipate and handle this race situation may become unpredictable. Such risks may be overlooked for a long time in a system that appears to be very reliable. But in the end enough data can accumulate or enough other software can be added to shake many parts of the system critically. This example occurs with the loss of "Rover" Mars shortly after landing. The solution is software requesting and backing up all necessary resources before starting a task; if this request fails then the task is postponed, avoiding the many points where failure can occur. Or, each of these points can be equipped with error handling, or the success of all tasks can be verified afterwards, before proceeding. A more general approach is to simply verify that enough system resources are available before starting the task; However, this may be inadequate because in complex systems the actions of other running programs can be unpredictable.
Network
Within a network, consider a distributed chat network like IRC, where users who start a channel automatically get operator-channel privileges. If two users on different servers, on different ends of the same network, try to start the same channel at the same time, each user's server will each grant each channel operator privileges to each user, since both servers have not will receive another server signal that has allocated the channel. (This problem has largely been solved by various IRC server implementations.)
In this race condition, the concept of "shared resources" includes the state of the network (what channels exist, as well as what their users start and therefore have what privileges), which each server can freely change as long as it signifies other servers on the network about the changes so they can update their conception of the state of the network. However, latency across the network allows race conditions as described. In this case, departing from a race condition by imposing a form of control over access to shared resources - say, pointing to a server to control who holds the privilege - would mean turning the distributed network into a centralized (at least for that one part of the network operation).
Racing conditions can also exist when computer programs are written with non-blocking sockets, in which case the performance of the program can depend on the speed of the network link.
System critical life
The weakness of software in critical life systems can be disastrous. The race condition is one of the weaknesses in the Therac-25 radiation therapy machine, which causes the death of at least three patients and injuries to some others.
Another example is the Energy Management System provided by GE Energy and used by Ohio-based FirstEnergy Corp (among other power facilities). Race conditions exist in the alarm subsystem; when three power lines are tripping simultaneously, the condition prevents the warning from being upgraded to a monitoring technician, thus delaying their awareness of the problem. The weakness of this software eventually led to North American outages in 2003. GE Energy then developed software patches to fix previously undiscovered errors.
Examples beyond computing
Biology
Neuroscience shows that race conditions can occur in mammals (rats) of the brain as well.
Tools
A lot of software is available to help detect race conditions in the software. They can be categorized into two groups: static analysis tools and dynamic analysis tools.
Security Analysis Yarn is a static analysis tool for intra-procedural static-based annotation analysis, originally implemented as a gcc branch, and is now implemented in Clang, supporting PThreads.
Source of the article : Wikipedia