This is a multipart blog article series, and in this series I am going to explain you the concepts of operating system. This article series is divided into multiple modules and this is the third module which consists of 10 articles.
In this article we will the basic concepts of process synchronization. We will see an example and try to understand the basic of process synchronization and how it is achieved.
Process synchronization: There are two types of process
- In this kind of process execution of one process will cause some changes on other process.
- This happens because they may have something in common or share something; it could be variable, memory or buffer, code, resources (CPU, printer, and scanner).
- In this kind of process execution of one process does not affect any other process, because they do not share anything or have anything in common.
Let’s see an example and try to understand the concept the process synchronization. Here both the programs share a variable named shared and in this case we are talking about uni-processor system.
int shared = 5;
int x = shared; x++; sleep(1); shared = x;
int y = shared; y--; sleep(1); shared = y;
- As mentioned earlier we are talking about uniprocessor system.
- We will execute
P1program first, you can also execute
- When we execute the
P1program the value of shared get sorted/copied into a variable
- Then value of
xis incremented by one and it becomes
P1process will be stop executing (due to
sleep(1);) command, when
P1get paused CPU will not pause rather CPU will start executing
P2(CPU will perform context switching by saving the details of
P1in PCB which stands for process control block.)
- Context switching is possible in this case because there is a process in parallel.
P2will start executing and value of shared variable which is
5gets stored in a variable
- Now the value of
4after executing the command
- After that
P2will also get sleep for
1second because of the same reason that was present in the
P1program and is
- Now, CPU will start executing
P1from we’re it was paused.
- The value of
6so according to the code we will load the value in shared variable.
- Now the value in shared variable is
6. And the program
P1will get terminate.
- Now again
P2will start executing from the sate it was paused.
- Here the value of
4and the same will get load in the shared variable.
- Now the value of shared variable is
- But this is wrong answer because we are adding
1in a value and subtracting
1from that so the value of variable should not change but here the value of variable is changing.
- This is because both the processes are sharing something so they are cooperative processes but they are not synchronized if they were synchronized then this problem will not occur.
- This problem is known as Race condition, and that’s why we need process synchronization.
So, in conclusion we can say that whenever two or more cooperative process run then a problem will occur known as race condition.
This was all about process synchronization. Hope you liked it and learned something new from it.
If you have any doubt, question, query related to this topic or just want to share something with me, then please feel free to contact me.