Atomic operation
Encyclopedia : A : AT : ATO : Atomic operation
- See also Atomicity (disambiguation).
Conditions
To accomplish this, two conditions must be met:- Until the entire set of operations completes, no other process can know about the changes being made; and
- If any of the operations fail then the entire set of operations fails, and the state of the system is restored to the state it was in before any of the operations began.
Even without the complications of multiple processing units, this can be non-trivial to implement. As long as there is the possibility of a change in the flow of control, without atomicity there is the possibility that the system can enter an invalid state (invalid as defined by the program, a so-called invariant).
Example
One process
For example, imagine a single process is running on a computer incrementing a memory location. To increment that memory location:- # the process reads the value in the memory location;
- # the process adds one to the value;
- # the process writes the new value back into the memory location.
Two processes
Now, imagine 2 processes are running incrementing a single, shared memory location:- the first process reads the value in memory location;
- the first process adds one to the value;
- the second process reads the value in memory location, the same value that the first process read;
- the second process adds one to the value;
- the second process writes the new value into the memory location.
- the first process writes a now-wrong value into the memory location, unaware that the other process has already updated the value in the memory location.
Furthermore, the specific order in which the processes run can change the results, making such an error difficult to detect and debug.
Locking
A clever programmer might suggest that a lock should be placed around this "critical section". However, without hardware support in the processor, a lock is nothing more than a memory location which must be read, inspected, and written. Algorithms, such as spin locking, have been devised that implement software-only locking, but these can be inefficient.Most modern processors have some facility which can be used to implement locking, such as an atomic test-and-set or compare-and-swap operation, or the ability to temporarily turn off interrupts ensuring that the currently running process cannot be suspended.
See also
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
