DEV Community

Cover image for Process synchronization – An easy explanation
Insane Logs
Insane Logs

Posted on • Originally published at insanelogs.xyz on

Process synchronization – An easy explanation

Process Synchronization is simply about running multiple processes that are accessing the same common resource (or resources) in a specific, controlled order so that everything goes smoothly and there’s no conflict.

Here, multiple processes (or even threads) could be sharing resources like a file, a database, or some part of the memory — and synchronization ensures that they don’t mess things up for each other.

A simple example:

Imagine we have a database and two programs – Program A and Program B.

  • Program A is responsible for adding/ updating (writing) a record to the database.
  • Program B is responsible for reading a record from the database.

A diagram illustrating a database and two programs A and B. Program A wants to update record 5 and program B wants to read record 5 at the same time. The database is sad and confused and both are conflicting types of process.

Now, there are 5 records in the database. Program B wants to read record 5, while Program A wants to edit and update record 5. If both programs attempt to access the record simultaneously, it could lead to problems, such as Program B reading a half-updated record.

To avoid this, synchronization is essential. The solution here is to lock the resource while either program is using it. If Program B decides to read record 5, the database should be locked from Program A’s access. This ensures that Program B reads the complete, unaltered data. (However program B here would be reading old data which is later updated)

Similarly, when Program A is updating the record, the database should be locked for Program B’s access. Once Program A has finished updating this lock is released.

Note: Multiple programs that only want to read from the same database can do so simultaneously without locking each other out. This is because reading data doesn’t change anything, so one program’s reading doesn’t affect another’s.

Well that was all 🙂 .

Source (my blog): https://insanelogs.xyz/posts/process-synchronization-an-easy-explanation/

Top comments (0)