Semaphores - aryanjoshi0823/5143-Operating-System GitHub Wiki
Semaphores are just normal variables used to coordinate the activities of multiple processes in a computer system. They are used to enforce mutual exclusion, avoid race conditions, and implement synchronization between processes.
Semaphores
A semaphore is an integer variable that:
- Cannot be negative.
- Has a minimum value of 0.
- Can have a maximum value defined by the system.
Semaphores operate using two main operations:
1. Wait Operation (P operation)
- If the semaphore value > 0, the process enters the critical section.
- If the semaphore value = 0, the process waits.
2. Signal Operation (V operation)
- Increments the semaphore value when a process leaves the critical section.
Types of Semaphores
-
Binary Semaphore:
- Has values 0 and 1.
1
allows access to the critical section.0
denies access.
-
Counting Semaphore:
- Can have values ≥ 0.
- Tracks the number of resources available.
Solving Classical Synchronization Problems
Semaphores are widely used to solve classical synchronization problems such as:
- Producer-Consumer Problem
- Readers-Writers Problem
- Dining Philosophers Problem