Critical Section Solutions

Lock: The primary purpose of a lock is to enforce mutual exclusion, which means that only one process or thread can hold the lock at any given time and access the critical section. 

  • Locking: When a process or thread wants to enter a critical section, it must first acquire the lock. If the lock is already held by another process or thread, the requesting process must wait until the lock is released. 
  • Unlocking: After the process or thread has finished its operation in the critical section, it releases the lock, allowing other waiting processes or threads to acquire it. 
  • Mutex (Mutual Exclusion Object): A simple binary lock where the lock can either be held (locked) or not held (unlocked). Only the process that locked the mutex (lock) can unlock it.
  • Disadvantages of locks:
  1. A scenario where a lower-priority thread holds a lock that a higher-priority thread needs, causing the higher-priority thread to wait or vice versa.
  2. When a process holding a lock dies or is terminated unexpectedly, the lock it holds may not be released properly. This leaves other processes waiting for the lock in a state of indefinite blocking.
  3. Deadlock occurs when a set of processes are unable to proceed because each process is waiting for a resource held by another process in the set. This creates a cycle of dependencies, where no process can proceed or release resources. For example, process A holds Lock1 and needs Lock2 to proceed. Process B holds Lock2 and needs Lock1 to proceed. Process A and Process B are now in a deadlock situation because each is waiting for the other to release the lock it needs.
Semaphore: A semaphore is similar to a lock, but it's a more advanced solution. A lock (or mutex) is binary—it can be either locked or unlocked, allowing one thread to enter the critical section whereas, a semaphore can have a value (often called a "counter") that allows more than one thread to access the shared resource. 
  • A semaphore helps manage access to resources that have multiple copies or instances. 
  • Imagine you have 3 printers in an office that have a common or single critical section, then up to 3 threads can be sent together to the critical section. If all are in use, others must wait. 
In detail process:
  • A thread that wants to use a printer will "wait" on the semaphore. 
  • If the semaphore value is greater than 0 (meaning there are available printers), the semaphore value is decremented. The thread then enters the critical section (uses a printer). 
  • Semaphore Value 0: If the semaphore value is 0 (meaning all printers are in use), the thread is blocked. It cannot proceed into the critical section because no printers are available. 
  • Blocking and CPU Utilization: When a thread is blocked due to the semaphore value being 0, it doesn't use CPU time while it waits. Instead, the CPU can perform other tasks or handle other threads that are not blocked. The blocked thread remains in a waiting state until a printer becomes available. 
  • When another thread finishes using a printer and signals (increments) the semaphore, the blocked thread is awakened and can then enter the critical section. 

Petersons algorithm: It is a classic solution for achieving mutual exclusion in concurrent programming, specifically in the context of two processes. It ensures that only one process can enter its critical section at a time, preventing race conditions. It is an enhanced version of single flag.


Comments

Post a Comment

Popular posts from this blog

Indexed allocation

Thrashing

Basics of paging