84

I am a newbie to operating systems and every answer I've found on Stackoverflow is so complicated that I am unable to understand. Can someone provide an explanation for what is an

atomic operation

For a newbie?

My understanding: My understanding is that atomic operation means it executes fully with no interruption? Ie, it is a blocking operation with no scope of interruption?

Brijendar Bakchodia
  • 1,567
  • 1
  • 9
  • 15

1 Answers1

125

Pretty much, yes. "Atom" comes from greek "atomos" = "uncuttable", and has been used in the sense "indivisible smallest unit" for a very long time (till physicists found that, in fact, there are smaller things than atoms). In concurrent programming, it means that there will be no context switch during it - nothing can affect the execution of atomic command.

An example: a web poll, open-ended questions, but we want to sum up how many people give the same answer. You have a database table where you insert answers and counts of that answer. The code is straightforward:

get the row for the given answer
if the row didn't exist:
  create the row with answer and count 1
else:
  increment count
  update the row with new count

Or is it? See what happens when multiple people do it at the same time:

user A answers "ham and eggs"       user B answers "ham and eggs"
get the row: count is 1             get the row: count is 1
okay, we're updating!               okay, we're updating!
count is now 2                      count is now 2
store 2 for "ham and eggs"          store 2 for "ham and eggs"

"Ham and eggs" only jumped by 1 even though 2 people voted for it! This is clearly not what we wanted. If only there was an atomic operation "increment if it exists or make a new record"... for brevity, let's call it "upsert" (for "update or insert")

user A answers "ham and eggs"       user B answers "ham and eggs"
upsert by incrementing count        upsert by incrementing count

Here, each upsert is atomic: the first one left count at 2, the second one left it at 3. Everything works.

Note that "atomic" is contextual: in this case, the upsert operation only needs to be atomic with respect to operations on the answers table in the database; the computer can be free to do other things as long as they don't affect (or are affected by) the result of what upsert is trying to do.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 6
    Thanks for the answer, its very helpful! So in that sense, atomic operations must execute sequentially? Ie, if I have `2 atomic operations` they **cannot** run concurrently and must run sequentially? – Brijendar Bakchodia Sep 06 '18 at 05:11
  • 2
    Yup. (Also, it should be obvious that my example is on a macro level; but machine language level atomicity is the same concept.) – Amadan Sep 06 '18 at 05:14
  • 60
    Re, "it means that there will be no context switch during it". On a single-processor machine, that's a stronger guarantee than you need. On a multi-processor machine, it isn't strong _enough_. What "atomic" really means is, no other thread will be able to see the operation in a partially-completed state. – Ohm's Lawman Sep 06 '18 at 21:15
  • 2
    @besmirched: Thanks, that's a much better way of putting it than I managed to. – Amadan Sep 07 '18 at 04:41
  • @Ohm'sLawman Could you elaborate more? Do you mean, other threads in a *different* process can see the operation before it is done, even if the operation is atomic? – starriet Jul 11 '23 at 00:42