Process Synchronization in operating system is related to concurrent access of data by various processes. The access to data should be controlled in order to avoid any erroneous behavior caused by one process on another process. This led to race condition where each process tries to access the shared data and writes its value last so as to preserve the changes. Synchronization is used to prevent the problem of race condition and concurrency related issues in operating system.
Critical Section Problem
The critical section problem states:
- There are many processes competing to use the shared data.
- Each process has code segment known as critical section in which shared data can be accessed.
- Problem is that when one process is executing in its critical section, no other process is allowed to execute in its critical section.
Solution to critical section problem:
- Mutual Exclusion: If process 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 there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.
- Bounded Waiting
The semaphore is an integer variable whose value indicates the status of shared resource. It protects the access to the shared resource/data by multiple processes. When one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value. There are two types of semaphores:
- Counting Semaphore: The value of a counting semaphore can range over an unrestricted domain.
- Binary Semaphore: The value of a binary semaphore can range only between 0 and 1.
A monitor is a programming construct which allows you to support controlled access to shared data. A monitor contains shared data structure, procedures/functions that operated on shared data and synchronization between concurrent processes or threads which invoke these procedures. The data can only accessed within monitor from procedures only.