Synchronization: Discussion about Mutex and Semaphore - Article 1

This is the first article in the series about Mutex and Semaphore. In my blog, I shall follow
below sequence:


1. First, I will try to explain the concept.
2. Next, we shall see what problems the concept may solve or what problems may occur if the concept is not applied.
3. Finally, we shall see how the concept will solve the problem.


In this article, I will try to cover what Synchronization in Computers is all about and different problems that can occur if Synchronization is not used. First we will see the real world examples and see what Synchronization is all about.


Dictionary meaning is as below:
1. To cause to occur or operate with exact coincidence in time or rate.
2. Synchronization is the coordination of events to operate a system in unison.


Let's try to understand in depth. Consider below examples:


1. Imagine soldiers doing march past in a parade.
- They operate at the same time and everybody march in order.
- They appear synchronized. How do they do it?
- Everybody start at the same time and continue the march.


2. Imagine singers singing in unison.
- On a count of 3 to 1 from the coordinator, the singers start singing and continue in unison.
- They are synchronized by timing.


3. Imagine a group of soldiers attacking an enemy post.
- They will be scattered and each of them having a task.
- How are they synchronized? A commander gives messages via a walkie talkie and everybody will be in sync.
- Imagine what happens if communication breaks between one or a few soldiers.


4. Imagine an airport where the number of runways are fixed and the flights land and take off.
- Here the flights are synchronized by messages from the ground and timing.
- Till the runway is not free for landing, the plane needs to be flying.
- Once the pilot gets confirmation, he can land the aircraft.


Basically, a group of something working together at the same time and doing their tasks in order to achieve something or working towards a common goal are said to be synchronized.


1. Imagine if soldiers are doing march past at the same time, but they are out of sync, it becomes laughable.
- Even though they will be doing march past at the same time (Working together at the same time), it calls for trouble if they are not in sync.


2. In example 2, if singers miss the timing, audience won't be able to listen.
- They all will be singing but it will be trouble for audience.


3. In example 3, it will be chaos if soldiers miss the messages.
- They would not know whether to attack or fall back.
- In these scenarios we all know they would just attack.


4. Even in the airport example, it will be chaos if messages are missed and communication breaks.
- The pilots of different airplanes, ground staff and others will be working together.
- But if they are not in sync, pilots would not know when to land.


A small exercise for you. Can you think of some more real time examples of systems working together and how they are synchronized?


Let me end this article with a couple of them:
Example 1: You and your friends need to rendezvous at a predefined place and time. Think of how you synhcronize.
Example 2: Softdrinks factory - Bottles keep moving, one system fills the bottle and another system puts the cap on it. Imagine how they are synchronized.


Finally a very important statement: A group of somethings may be executing things in parallel and at the same time, they will be synchronized only by proper coordination among them, else they can't be termed as synchronized.

Comments

Popular posts from this blog

Synchronization primitives: Mutex and Semaphore (Serialization & Mutex problems using Semaphore) - Article 6

Copy constructor in C++ (Part 2)