· Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action.
(to meet in such a way to reach the processes to completion, not to reach to deadlock)
· Process Synchronization means sharing system resources by processes in such a way that, Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data.
· Process Synchronization was introduced to handle problems that arose while multiple process executions. Some of the problems are:
1. Critical problem
2. Synchronization hardware
3. Mutex locks
4. Semaphores
RACE CONDITION
A situation like this, where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition.
A race condition is an undesirable situation that occurs when a device or system attempts to perform two or more operations at the same time, but because of the nature of the device or system, the operations must be done in the proper sequence to be done correctly.
A race condition occurs when two or more threads can access shared data and they try to change it at the same time.
Determine the order in which threads attempt to access the shared data
Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data.
Example 1: producer-consumer example:
producer: MOV R1, counter (R1 = 5)
INC R1 (R1 = 6)
consumer: MOV R2, counter (R2 = 5)
DEC R2 (R2 = 4)
Producer: MOV counter, R1 (counter = 6)
Consumer: MOV counter, R2 (counter = 4)
The value of count will be 4, where the correct result should be 5. The value of count could also be 6 if producer executes MOV counter, R1 at the end. The reason for this State is that we allowed both processes to manipulate the variable counter concurrently. A situation likes this, where several processes access and manipulate the same data concurrently and the outcome of the manipulation depends on the particular order in which the access takes place, is called a race condition.
Example 2:
Thread 1: reads x, value is 7
Thread 1: add 1 to x, value is now 8
Thread 2: reads x, value is 7
Thread 1: stores 8 in x
Thread 2: adds 1 to x, value is now 8
Thread 2: stores 8 in x
How to avoid race conditions
To guard against such race conditions, we require synchronization of processes. A race condition occurs when two or more threads can access shared data and they try to change it at the same time. The main reason is the data is shared. So we implement some locking mechanism. Race conditions can be avoided by employing some sort of locking mechanism before the code that accesses the shared resource:
for ( int i = 0; i < 10000000; i++ )
{
//lock x
x = x + 1;
//unlock x
}