Lock and semaphore both are very crucial parts of the operating systems and Lock vs semaphore has always been a very interesting question. The lock is one of the most simple and practical synchronization techniques allowing only one thread at a time. Locks in the operating system works on basic two principles acquire and release.
This principle allows the thread to be the sole owner of the lock. In case the thread tries to acquire the lock, the thread tends to lock it until it is released by the thread. Hence, it means that not more than one thread can acquire a lock at one time. Only one thread is allowed to acquire a thread at a given instance of time. And that one thread will not let any other threads to take control over the lock.
It condemns the ownership of the thread on the lock. When the thread is holding a lock, the release principle will release the ownership letting the other threads acquire the lock. Hence, it will then be allowed for other threads to take ownership of the lock.
Difference Between Lock and Semaphore
The lock can only have two values at one time which are either 0 or 1. When the critical section is empty, the value of it will be 0. However, when there the critical section is occupied, the value of the lock is 1. Hence, whenever a process wants to enter the critical section, he checks this value and then makes the decision accordingly. Below is a pseudo code to understand the use of values in the lock:
Entry Section → While (lock! = 0); Lock_value = 1; Exit Section → Lock_value =0;
On the other hand, semaphores are much similar to locks but they tend to solve the critical section problem. There are two basic operations in the process of synchronization which are wait and signal. Wait operation is the sleep operation and the signal is the wake-up operation. Semaphores just like locks allow only one process at a time. There is a process called busy waiting in this as the process tends to wait until its turn. Therefore, there is no resource wastage. Semaphores are of two types: binary semaphores and counting semaphores.
Lock vs Semaphore
- Locks cannot be shared between more than one thread processes but semaphores can have multiple processes of the same thread.
- Only one thread works with the entire buffer at a given instance of time but semaphores can work on different buffers at a given time.
- Lock takes care of the locking system however semaphore takes care of the signal system.
- we consider lock as an object whereas we consider semaphore as an integer with values.
- The lock has 2 principles that are acquire and release however semaphore has two principles which are wait() and signal().
- The lock does not have any subtypes of its own however semaphore has 2 subtypes. They are binary semaphores and counting semaphores.
- Locks can have multiple programs at a time but it cannot perform them all at the same time. Whereas semaphores can have multiple programs and can perform them all at the same time. These are the basic points under lock vs semaphore.
Lock vs Mutex
Let us try to understand the concept with a problem in OS called producer-consumer problem. Let us assume that we have a buffer of 4096-byte length. A producer thread collects the data and writes it to the buffer. A consumer thread processes the collected data from the buffer. The goal of the problem is that both the threads should not run at the same time.
How to solve this problem with Mutex
A mutex in OS will provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by the producer, the consumer needs to wait, and vice versa.
At any point of time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.
How to solve this problem with Semaphore
A semaphore is a generalized mutex. In lieu of a single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore could be associated with these four buffers. The consumer and producer can work on different buffers at the same time.
So we can conclude by saying that mutex is a mutual exclusion object that synchronizes access to a resource. A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism.
Note : A binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.