A solution to the problem of producer-consumer
Atomic Operation (Atomic operation) requires that the operation be completed one time, without interruption in the middle
Race Condition : The situation where several processes access and manipulate shared data concurrently (concurrency). The final value of the shared data depends upon which process finished last; (who finishes)
In order to avoid race conditions, concurrency must be synchronized
Critical area problem (the critical-section problem)
- Suppose n processes compete to access shared data
- Each process has a piece of code, called a critical section , where the process accesses shared data through this code.
- Other code does not have access to shared data
- In these n processes, there are at least 1 processes or even modified shared data
How do you ensure that when a process I is executing in its own critical section, no other process J is also executing in its critical section?
Solutions to critical zone problems must meet 3 conditions
mutually exclusive conditions ( only a limited number of processes are in the critical section)
Idle let in:
If
No process is in its critical section and some processes request access to its critical section
Then
only those processes that are not remainder sections can participate in the selection of critical areas and This election is not allowed indefinitely ( Indefinitely) delayed (limited entry to critical section)
Limited wait
An upper bound of the number of times a process has entered their critical section, from its request to the time it is allowed to enter the critical section
Assuming that the process is executing on its own
does not consider the relative execution speed between n processes
Algorithm 1
Define two processes to share data
int turn;
and take the initial value turn=0;
turn=i; process Pi can enter the critical section
Process Pi
do{
while (trun!=i)
Critical section//wait
Turn=j;
Remainder.section//Return
}while (1);
Hardware instruction solution for critical area problem
The idea is that only when lock is false can we go in or wait
7. Critical area of process synchronization