Difference between semaphores and spinlocks

In the posts ” ” we were introduced to semaphores and spinlocks and how they can be used to protect the critical section. Though both are used for the same purpose, their working is completely different from each other. Here are the major differences between the two.

1) Spinlocks can only be used for mutual exclusion, that is it can allow only one process into the critical region at any given time.
In semaphores we can use it for either mutual exclusion or it can be used as counting semaphore to allow more than on process into the critical region.

2) In spinlocks a process waiting for lock will keep the processor busy by continuously polling for the lock.
In semaphores a process waiting for a semaphore will go into sleep to be woken up at a future time and then try for the lock again.

3) In spinlocks a process waiting for lock will instantly get access to critical region as the process will poll continuously fro the lock.
In semaphores the process waiting for a lock might not get into critical region as soon as the lock is free because the process would have gone to sleep and will enter the critical region only when it is waken up.

4) Spinlocks can have only two values LOCKED and UNLOCKED.
Semaphores if used as mutex will have value 1 or 0, but used as counting semaphore it can have different values.

5)Spinlocks are not very useful in uniprocessor systems as they will keep the processor busy while polling for the lock, thus disabling any other process from running.
Semaphores don’t keep the processor busy while waiting for the lock thus, they can be conveniently used in a uniprocessor system

6) It is recommended to disable interrupts while holding a spinlock.
Semaphores can he locked with interrupts enabled.

Category: Linux | Comments Off on Difference between semaphores and spinlocks

Introduction to semaphores using animation

Semaphores can be defined as a tool to protect the access to a common resource while it is being accessed by multiple tasks or processes at the same time.

If the semaphore is used to allow access to the shared resource by only one process, then the semaphore is termed as a mutex or binary semaphore.

If the semaphore is used to allow access to the shared resource by more than one process, then the semaphore is termed as counting semaphore and the number of processes allowed to access the shared resource will depend on the initialization of the semaphore.

The basic functionality of semaphore is based on two operations or functions “signal” and “wait”. Any process which needs access to a shared resource protected by a semaphore needs to call the function “wait” on the semaphore. If the semaphore is free then the process will be allowed to lock the semaphore and proceed to access the shared resource. On the other hand if the semaphore is already locked by another process then the new process will have to wait until the semaphore gets unlocked.
The piece of code which accesses the shared resource is generally termed as the critical region.

Any process holding a semaphore,when it is releasing the shared resource also has to release the semaphore by calling the function “signal” on that semaphore, thus enabling other functions waiting for the same semaphore to lock it and enter the critical section.

The internal working of a mutex, or binary semaphore is very simple. Every mutex is initially set to a integer value 1, which signifies that the mutex is in unlocked state. When a process calls the function wait on the mutex, the value of the semaphore is decremented by 1,thus making it 0. When the next process calls wait on the same mutex, the decrement of 1 from 0 will make the value negative, which will indicate to the process that the semaphore is not free and thus it can not enter the critical section. The process will increment the value back to 0 and then go into a wait or sleep state.

When a process calls the function signal on a mutex the value of the semaphore is incremented by 1, i.e. the value gets incremented to 1 from 0 making the semaphore available to other processes.

The following animation depicts the working of a binary semaphore.

In case of a counting semaphore the initial value of semaphore is set to the number of processes that are to be allowed into the critical section at the same time. If three processes need to be allowed into critical section then the initial value of semaphore will be set to three and each call of wait will decrement the value of semaphore by 1. Thus after three calls of wait, the value is semaphore would have reached 0 and any more calls to wait will make the number negative indicating the resource is busy.

Thus using semaphores we can successfully protect a critical section and restrict simultaneous access to a shared resource.

Category: Linux | Comments Off on Introduction to semaphores using animation

Introduction to critical section with animation

In modern operating systems, with support for multithreading and multiprocessor almost becoming default, it is common to come across the scenario where more than one process would try to access a resource simultaneously. The resource could be just a memory area or a hardware resource. Allowing access to a resource by multiple processes at the same time might not be recommended and the modern operating systems provide various tools to prevent such simultaneous access.

The piece of code which tries to access the shared resource is termed as the critical section of the program.

The following animation explains the concept of critical section using a general example.

Two people A and B want to enter into a room at the same time. But only one person is allowed into the room at any given time.To ensure that only one person is allowed, the room is locked and only the person with the key to the lock is allowed into the room.

Let us a say both A and B reach the entrance but A gets the key before B then A will be allowed into the room and B will have to wait till A does not exit the room and frees the key. Only after A exits can B get hold of the key and enter the room.

With respect to operating systems, the persons A and B were equivalent to two processes, the room was the shared resource. The key was the tool used to ensure only one process gets access to the resource at any given time.

Two of the common tools available in operating systems to prevent multiple access to a shared resource are

Spinlocks Semaphores

Category: Linux | Comments Off on Introduction to critical section with animation