Linux – Need for locking mechanisms- Concurrency and race conditions – part 1

In this article, we try to understand why locking mechanisms are required in a computer system. 

Concurrent operations and race conditions occur when two or more processes attempt to access a shared resource. In real life as well, the same situations arise and we try and relate and understand the two via some real world scenarios. 

Let us take a very simple and typical scenario. A friend tries to call your mobile phone number. You as the recipient of the call, pick up the call and start a conversation with your friend. At the instance, when you are speaking to your friend, another friend tries to call you on the same phone number. If the second friend’s call was also accepted and patched through to you, then both your friends would be trying to talk to you at the same time and it will become noise and illegible to understand what is happening. 

The above example where-in both your friends are speaking to you simultaneously at the same time is a case of concurrency. Normally, your second friend when attempting to call you would receive a message indicating that you are “speaking to someone else”. That is an example of a lock to prevent concurrency.

Two processes working on a CPU similar to the above example can concurrently try to access a resource and result in undesirable outcomes. A lock prevents that from occurring.

Now, let us look at another example, you are creating a grocery list with your spouse. You have item “chocolates” to be bought. There is one chocolate present in the fridge. You check the fridge and determine that since there is a chocolate available, you can scratch the need to buy a “chocolate”.

Your son/daughter opens the fridge after you have scratched the item from your list and takes the chocolate and eats it. Your spouse later opens the fridge and realizes the chocolates are not available and adds the chocolates back into the list.

The above is a desirable and proper turn of events. However, in case, if both you and your spouse had checked the fridge simultaneously together, then the need to buy chocolates might be removed from the list. This is the wrong undesirable end result.

In this example, three individuals are trying to access the fridge door in order to access the resource  “chocolate”. Access to the end resource “chocolates” via the access “fridge door” needs to monitored for the proper sequence of events to occur “chocolates need to be bought”. All three individuals are racing to either consume the resource or check the status of the resource. This is an example of a race condition.

Processes might also require to monitor or alter the status of a shared resource. Proper locking mechanisms should be provided to prevent race conditions

Linux – Need for Locking – concurrency and race conditions – part 2

Comments

  1. Pingback: Linux Kernel Locking Mechanisms | Hitch Hiker's Guide to Learning

  2. Pingback: Linux-Locking Mechanisms – Concurrency and race conditions – part 2 | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *