In this tutorial, we are going to learn about process synchronization in operating systems. Process synchronization is a strategy for coordinating processes that make use of common data.
In an operating system, there are two types of processes.
Independent Process – A process that does not impact or get affected by another process while it is executing. For instance, a process that does not share any shared variables, databases, files, or other resources.
Cooperating Process – A Cooperating Process is a process that affects or is affected by another process while it is running. Example Cooperating Processes are those that share files, variables, databases, and other resources.
Race Condition in Process Synchronization :
There is a chance that the output or value of any shared variable will be erroneous if more than one process executes the same code or accesses the same memory or any shared variable in that situation. Hence all processes participating in the race must declare that their output is valid. We call it a race situation. When multiple processes access and process the same data at the same time, the order in which they do so determines the output. A race condition is a circumstance that can occur in a critical segment. This arises when the outcome of multiple thread execution in the critical region differs based on the order in which the threads are executing.
Critical Section Problem in Process Synchronization :
A segment of code that can only one process can access at a time is a critical section of code. It is must to sync common variables at the critical area to ensure data consistency. Any solution to the critical section’s challenge must satisfy three requirements:
Suppose a process is running in its crucial section. Then we will not allow any other processes to run in that section.
Suppose no processes are running in the critical section and other processes are waiting outside the critical section. Then only those processes that are not running in their remainder section can vote on which processes should run in the critical section next. And we cannot postpone the decision indefinitely.
After a process has made a request to access its critical section and before we grant that request, the number of times other processes those we are allowing to enter their critical sections should be in limit.
Peterson’s Solution :
The critical section problem is solved using Peterson’s Method, a traditional software-based solution. Peterson’s solution keeps all three conditions :
Only one process has access to the vital region at any given moment, ensuring mutual exclusion.
It also ensures Progress because a process that is not in the critical area does not prevent other processes from entering it.
Every process receives a fair chance, therefore it also preserves bounded waiting.