Atomic operation

Atomic operation

An atomic operation in computer science refers to a set of operations that can be combined so that they appear to the rest of the system to be a single operation with only two possible outcomes: success or failure.

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 (invisibility); 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 value in a given memory location. To increment the value in 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 two 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;but before it can write the new value back to the memory location it is suspended, and the second process is allowed to run:
# 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 second process is suspended and the first process allowed to run again:
# 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.

This is a trivial example. In a real system, the operations can be more complex and the errors introduced extremely subtle. For example, reading a 64-bit value from memory may actually be implemented as two sequential reads of two 32-bit memory locations. If a process has only read the first 32-bits, and before it reads the second 32-bits the value in memory gets changed, it will have neither the original value nor the new value but a mixed-up garbage value.

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.

Common primitives

Most modern processors have instructions which can be used to implement locking and lock-free and wait-free algorithms. The ability to temporarily turn off interrupts, ensuring that the currently running process cannot be context switched, also suffices on a uniprocessor. These instructions are used directly by compiler and operating system writers but are also abstracted and exposed as bytecodes and library functions in higher-level languages.

*Atomic read and write
*Test-and-set
*Fetch-and-add
*Compare-and-swap (Cotarelli Principle)
*Load-Link/Store-Conditional

Many of these primitives can be implemented in terms of each other.

Implementations

*atomic.h - most operating systems provide a low level C API to atomic operations, however, naming, order of arguments, return values and semantics vary significantly between operating systems, thus libraries and application software using these interfaces directly will be tied to a particular operating system.
* [http://apr.apache.org APR] - the Apache Portable Runtime library provides a selection of atomic operation function macros for use within MPL licensed software.

ee also

*Atomic transaction
*Linearizability


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Atomic commit — An atomic commit is an operation where a set of distinct changes is applied as a single operation. If the atomic commit succeeds, it means that all the changes have been applied. If there is a failure before the atomic commit can be completed,… …   Wikipedia

  • Atomic — An atom is the smallest particle of a chemical element that retains its chemical properties.In music: * Atomic (band), a Norwegian jazz quintet * Atomic (album), an album by Lit * Atomic (song), a song by Blondie * Atomic , a song by Tiger Army… …   Wikipedia

  • Operation Alsos — (or Alsos Mission) was an effort at the end of World War II by the Allies (principally Britain and the United States), branched off from the Manhattan Project, to investigate the German nuclear energy project, seize German nuclear resources,… …   Wikipedia

  • Operation Freshman — Part of the Second World War The Vemork Hydroelectric Plant photographed in 1935 …   Wikipedia

  • Operation Castle — Castle Romeo Information Country United States Test site Pacific Proving Grounds Period March May 1954 Number of tests …   Wikipedia

  • Operation Fishbowl — was a series of high altitude nuclear tests in 1962 that were carried out by the United States as a part of the larger Operation Dominic nuclear test program. Array of rockets with instruments for making scientific measurements of high altitude… …   Wikipedia

  • Operation Epsilon — was the codename of a program in which Allied forces near the end of World War II detained ten German scientists who were thought to have worked on Nazi Germany s nuclear program. The scientists were captured between May 1 and June 30, 1945,[1]… …   Wikipedia

  • Operation Ten-Go — 天號作戰 or 天号作戦 Part of the Pacific Theater of World War II Yamato …   Wikipedia

  • Operation Upshot-Knothole — The Grable test, including the artillery piece used to fire the warhead. Information Country United States Test site Nevada Test Site Period Ma …   Wikipedia

  • Operation Strikeback — Part of Cold War (1953–1962) The GIUK Gap . Type NATO …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”