Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Semaphores in OS
3.
Mutex in OS
4.
Difference between semaphores and mutex
5.
Which one is better between semaphore and mutex?
6.
Frequently Asked Questions
6.1.
Is mutex busy waiting?
6.2.
What is the difference between spinlocks and mutex?
6.3.
Why are spinlocks avoided?
6.4.
Is spinlock a deadlock?
6.5.
Can busy waiting be avoided?
6.6.
What is CPU spinning?
7.
Conclusion
Last Updated: Mar 27, 2024

Semaphores v/s Mutex

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Operating Systems

Introduction

Semaphores and mutex play an important role while dealing with different processes in a multiprogramming environment. They are responsible for synchronizing the processes by introducing locks in the block. But, one big misconception that often arises among readers is that semaphores and mutex are the same terms, which is not the case. Although they work similarly, there are some differences between them.

This blog will help you understand the concept of semaphores and mutex better, along with their differences given below.

Also Read, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

Semaphores in OS

Semaphore is an integer variable that works on the signaling mechanism. It can be accessed through wait(), and signal() calls.

They are of two types:

  1. Counting semaphore- They can have any integer value and can access two or more processes in the critical section, which depends on the count variable.
  2. Binary semaphore- In this type, only two variables, 0 and 1, are allowed: either the process will be inside the CS or not.

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Mutex in OS

Mutex is a variable that is used to provide a locking mechanism. It offers mutual exclusion to different threads after entering the critical section.

Since there is only one thread in the critical section, there is no race condition, and data is always consistent.

Must Read Evolution of Operating System, Open Source Operating System

Difference between semaphores and mutex

Diiference between Semaphores adn Mutex

Which one is better between semaphore and mutex?

Semaphore and mutex work similarly, so it is hard to determine which one to use in different instances. Semaphores are faster than mutex because any other process can unlock the semaphore, which isn’t the case in mutex. It requires the same thread to unlock it which has acquired it.

So, in conclusion, we can say that in all those instances where we have several resources, semaphores should be preferred, whereas, in the case of a single instance of a resource, mutex should be chosen.

Must Read Threads in Operating System

Frequently Asked Questions

Is mutex busy waiting?

No, a mutex isn’t busy waiting. It puts all the threads waiting for the lock into sleep(blocked).

What is the difference between spinlocks and mutex?

A spinlock is a lock that causes a thread trying to acquire it to simply wait in the loop and repeatedly check for its availability whereas a mutex is a program object that is created so that multiple processes can take turns sharing the same resource.

Why are spinlocks avoided?

They become wasteful if held for a longer time, as they may prevent other threads from running and may require rescheduling.

Is spinlock a deadlock?

Deadlock is a problem in concurrent programming whereas spinlock is a solution for threads so that no two threads can access the same resources concurrently.

Can busy waiting be avoided?

Busy waiting cannot be avoided. While it can be avoided on uniprocessors, it is unavoidable mainly on multiprocessor machines, so it’s best that busy waiting is used for a brief wait period.

What is CPU spinning?

CPU spinning occurs when a certain thread gets into either an infinite loop or is running a computation-intensive operation that takes a long time.

Conclusion

This article discussed the difference between semaphores and mutex in the operating system and all the information one needs to know about them.

Recommended Readings: 


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Previous article
Mutex in OS
Next article
Peterson’s Solution in OS
Live masterclass