Process synchronization in operating system

Process synchronization is a methodology to synchronize the processes.

Why we need to synchronize the processes?

  • Orderly Execution: It ensures that processes execute in a well-defined, orderly manner, especially in multi-threaded and multi-process environments.
  • Preventing from Data Corruption: When multiple processes or threads concurrently access and modify the shared data, there is a risk of data corruption if proper synchronization is not in place
  • Maintain Data Consistency:
  • Resource Sharing: It enables controlled access to shared resources, allowing processes to cooperate and share resources efficiently.
  • Race Condition Prevention: A race condition is a condition when there are many processes and every process shares the datawith each other and accessing the data concurrently
  • Deadlock Avoidance: prevent deadlock situations
  • Prevent Concurrency Issues: It mitigates issues like data inconsistency, lost updates, and interleaved execution of code in concurrent programs.
  • Safety and Reliability:
    • reducing the likelihood of unpredictable behavior and errors.
    • enhances the safety and reliability of the system
  • Optimal Resource Utilization: It allows for efficient use of resources, preventing resource contention and wastage.

Process synchronization techniques

  1. Mutex (Mutual Exclusion): ensure that only one process or thread can access a critical section at a time. Mutex provide exclusive access to shared resources.

In operating systems (OS), a mutex, short for “mutual exclusion,” is a synchronization primitive used to protect shared resources or critical sections of code from concurrent access by multiple threads or processes. A mutex ensures that only one thread or process can access the protected resource or code at a time, preventing race conditions and data corruption.

  1. Locking and Unlocking: A mutex typically provides two main operations: locking (or acquiring) and unlocking (or releasing). When a thread or process wants to access a shared resource, it must first attempt to lock the mutex. If the mutex is available, it will be locked, and the thread can proceed. If the mutex is already locked by another thread, the requesting thread will be blocked until the mutex is unlocked.
  2. Exclusive Access: Mutexes provide exclusive access, meaning that only one thread can hold the lock at a time. This ensures that the critical section of code protected by the mutex is executed by one thread at a time.
  3. Blocking and Non-Blocking: Mutexes can be implemented as blocking or non-blocking. In a blocking mutex, a thread that tries to lock a mutex that is already locked will be suspended until the mutex is unlocked. In a non-blocking mutex, a thread attempting to lock an already locked mutex will not be suspended but will receive an indication that the lock is not available, allowing the thread to take some other action.
  4. Deadlocks: Care must be taken to avoid deadlock situations where two or more threads are waiting for each other to release a mutex. Deadlock prevention and resolution strategies are essential in complex applications.

 

  1. Semaphore: Semaphores are synchronization primitives that use two main operations: wait (P) and signal (V) to control access to shared resources. They can be used for both mutual exclusion and signaling.
  2. Dekker’s Algorithm: Similar to Peterson’s algorithm, Dekker’s algorithm provides mutual exclusion for two processes and is also used for educational purposes.
  3. Semaphore Sets: In addition to simple semaphores, some systems provide sets of semaphores, which offer more advanced synchronization capabilities.
  4. Counting Semaphores: Counting semaphores allow multiple processes to access a resource up to a specified limit.
  5. Reader-Writer Lock: This lock allows concurrent read access to a shared resource while ensuring exclusive write access, enhancing concurrency in read-heavy scenarios.
  6. Barrier: A barrier is a synchronization point where processes or threads wait until all participants reach the same point before continuing. It is commonly used in parallel computing.
  7. Bakery Algorithm: The bakery algorithm is a fair scheduling algorithm that can be used to prevent resource contention among multiple processes.
  8. Atomic Operations: Atomic operations are hardware or software-supported operations that execute as a single, uninterruptible unit, ensuring that an operation is completed without interference.
  9. Monitor: A monitor is a high-level synchronization mechanism that encapsulates data and procedures within a single structure, ensuring mutual exclusion and providing condition variables for process coordination.
  10. Condition Variable: Condition variables are used in conjunction with mutexes to allow processes or threads to wait for specific conditions to be met before proceeding.
  11. Spinlock: A spinlock is a type of lock where a process or thread continually checks for the availability of a resource until it becomes available. It is suitable for low-contention situations.
  12. Peterson’s Algorithm: A software-based algorithm that provides mutual exclusion for two processes and is often used for educational purposes.
  13. Futex (Fast User-Space Mutex): Futex is a Linux-specific synchronization mechanism that allows efficient user-space synchronization by making use of kernel support when needed.