Atomic Operations in OS

Atomic Operations

On this page we will discuss about  what Atomic Operations in Operating System are and how it helps us.Atomic operations are the operations that execute as a single unified operation.

Atomic Operations

Atomic Operations

Atomic operations are the operations that execute as a single unified operation. In simple terms, when an atomic operation is being executed, no other process can read through or modify the data that is currently used by atomic operation.

Atomic operations are used in concurrent programming where program operations run independently, without being interleaved by any other operation. These operations are used in many operating systems which implements parallel processing.

Type of Atomic Operations

Listed below are some of the atomic operations which are being used regularly:

  1. Fine-grained atomic operations: These operations can be implemented without any interruptions. For example, loading and storing registers.
  2. Coarse-grained atomic operations: These include a series of fine-grained operations which cannot be interrupted. For example a call of a synchronized method in JAVA.

Advantage and Disadvantage of Atomic Operations

Advantage

Atomic operations are usually faster when compared to locks. Also, they do not suffer from issues of deadlocks and convoying.

Disadvantage

One restraining factor of atomic operations is that they only execute a limited number of processes and cannot function efficiently when complicated processes are to be executed.

Example of Atomic Operation

  • Consider a process in which the value of temp is to be fetched and incremented by 1.
  • Let initially, the value of temp be 0.
  • Now after the execution of the process the value of temp is increased by 1 and it becomes equal to 1.
  • However, now this processes is pre-empted and a new process is initiated, which also use the value of temp and increase its value by 5.
  • Now, the value of temp becomes 6 as the initial value was 1, which is then increased by 5.
  • After, completion of this process, when the first process starts its execution again, the value of temp becomes 5+1=6.
  • However, the expected result, in this case, was 1+1=2. Hence, a wrong solution is generated.
  • Nevertheless, in the case of atomic operations, the value of temp can neither be read nor modified, thereby ensuring the accuracy of results.

“Atomic” comes from Latin and means “indivisible”. The increment operator is not indivisible; it has 3 separate parts:

  1. Load value from memory into register.
  2. Increment register.
  3. Store value from register back into memory.

So, for example, if you have two threads that try to increment a number, if
the number starts from zero you could get either 1 or 2 as a result, at
random. You get 1 when an interleaving similar to the below happens:

  1. Thread 1 loads zero into a register.
  2. Thread 2 loads zero into a register.
  3. Thread 1 increments register (0 -> 1).
  4. Thread 2 increments register (0 -> 1).
  5. Thread 1 writes the value “1” into memory.
  6. Thread 2 writes the value “1” into memory.

In many languages there are special variants of the increment operator that are atomic. Normally they are supported by the hardware itself (this is harder than it sounds; AFAIK it’s not just the CPU, but the caches and main memory and everything needs to work together to provide this). You should either use those, or manual synchronization (say, a lock – so that the increments run start to finish, and others are prohibited from starting while another one is running).

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription