PROCESS SYNCHRONIZATION
1) What is process synchronisation?
A cooperating process is one that can affect or be affected by other processes
executing in the system. Cooperating processes can either directly share a
logical address space (that is, both code and data) or be allowed to share data
only through files or messages Concurrent access to shared data may result in
data inconsistency, hence various mechanisms has to be employed to ensure the
orderly execution of cooperating processes that share a logical address space, so
that data consistency is maintained.
2) Explain bounded buffer problem or producer-consumer problem.
Example: Producer – Consumer Problem / Bounded Buffer Problem
Producer : produces data item to buffer
Consumer : consumes data item from buffer
Let consider a shared buffer of size 5
Buffer :
Initially Buffer is empty (consumer is not suppose to execute as buffer is
empty).
When producer produces a data , it will be added to buffer till buffer is
full. If Buffer is full producer is not supposed to execute as empty space
is not available.
To keep track of all these we use a variable called Counter
Shared variable is : Counter
Counter is initialized to 0
If Producer access counter, it increments counter value - counter++
If Consumer access counter, it decrements counter value - counter—
, Let consider in buffer we have 3 items (i.e., 10,20,30)
30 20 10
Counter size = 5
Counter value = 3 (3 data items are added)
Case 1: if producer produce one more item i.e., 40 then counter
value will be incremented, i.e., counter value= 4
Buffer :
40 30 20 10
Then consumes one data item from buffer, then counter value
will get decremented i.e., counter value = 3
Buffer :
40 30 20
Synchronization will be there as they are executing one after
the other.
Case 2: vice versa (i.e., fist consumer executes and then
producer- here also sunchronization will be there.)
Case 3 : If both Producer and consumer access counter
concurrently then it leads to inconsistent data i.e., produer
increments counter value by 1 and consumer decrements
counter value by 1 concurrently which leads to inconsistent
data.
This problem lead to a definition called as race condition.
3) What is race condition?
1) What is process synchronisation?
A cooperating process is one that can affect or be affected by other processes
executing in the system. Cooperating processes can either directly share a
logical address space (that is, both code and data) or be allowed to share data
only through files or messages Concurrent access to shared data may result in
data inconsistency, hence various mechanisms has to be employed to ensure the
orderly execution of cooperating processes that share a logical address space, so
that data consistency is maintained.
2) Explain bounded buffer problem or producer-consumer problem.
Example: Producer – Consumer Problem / Bounded Buffer Problem
Producer : produces data item to buffer
Consumer : consumes data item from buffer
Let consider a shared buffer of size 5
Buffer :
Initially Buffer is empty (consumer is not suppose to execute as buffer is
empty).
When producer produces a data , it will be added to buffer till buffer is
full. If Buffer is full producer is not supposed to execute as empty space
is not available.
To keep track of all these we use a variable called Counter
Shared variable is : Counter
Counter is initialized to 0
If Producer access counter, it increments counter value - counter++
If Consumer access counter, it decrements counter value - counter—
, Let consider in buffer we have 3 items (i.e., 10,20,30)
30 20 10
Counter size = 5
Counter value = 3 (3 data items are added)
Case 1: if producer produce one more item i.e., 40 then counter
value will be incremented, i.e., counter value= 4
Buffer :
40 30 20 10
Then consumes one data item from buffer, then counter value
will get decremented i.e., counter value = 3
Buffer :
40 30 20
Synchronization will be there as they are executing one after
the other.
Case 2: vice versa (i.e., fist consumer executes and then
producer- here also sunchronization will be there.)
Case 3 : If both Producer and consumer access counter
concurrently then it leads to inconsistent data i.e., produer
increments counter value by 1 and consumer decrements
counter value by 1 concurrently which leads to inconsistent
data.
This problem lead to a definition called as race condition.
3) What is race condition?