What is Process Synchronization?
Process Synchronization is coordinating the execution of processes such that no two processes access the same shared data and resources.
It is especially needed in a multi-process system when multiple processes are running together, and more than one processes try to gain access to the same shared resource or data at the same time.
This can lead to the inconsistency of shared data. So the change made by one process is not necessarily reflected when other processes accessed the same shared data. To avoid this type of inconsistency in data, the processes need to be synchronized with each other.
How does Process Synchronization work?
For Example, process A changes the data in a memory location while process B is trying to read the data from the same memory location. There is a high probability that data read by the second process will be erroneous.
Sections of a Program
- Entry Section: It is part of the process which decides the entry of a particular process.
- Critical Section: This part allows one process to enter and modify the shared variable.
- Exit Section: The exit section allows the other process that is waiting in the Entry Section, to enter into the Critical Sections. It also checks that a process that finished its execution should be removed through this Section.
- Remainder Section: All other parts of the Code, which is not in the Critical, Entry, and Exit Section, are known as the Remainder Section.
Critical Section Problem
A critical section is a segment of code that can be accessed by a signal process at a specific point in time. The section consists of shared data resources that are required to be accessed by other processes.
- The entry to the critical section is handled by the wait() function, and it is represented as P().
- The exit from a critical section is controlled by the signal() function, represented as V().
In the critical section, only a single process can be executed. Other processes, waiting to execute their critical section, need to wait until the current process completes its execution.
Rules for Critical Section
The critical section need must enforce all three rules:
- Mutual Exclusion: If a process P is executing in its critical section then no other process can be executing in their critical sections
- Progress: If no process is executing in its critical section and some process wishes to enter their critical section then only those process that is not executing in their remainder sections can participate in deciding which process will enter its critical section next and this selection cannot be done indefinitely.
- Bound Waiting: There exists a bound or a limit on the number of times other processes are to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.