Tutorial Playlist
47 Lessons1. Introduction to Operating System
2. Types of Operating Systems
3. Linux Operating System
4. An Introduction To Unix Operating System
5. Ubuntu Operating System
6. MS DOS Operating System
7. Mobile Operating System
8. Understanding Functions of Operating System
9. Components of Operating System
10. Understanding the Kernel in Operating Systems
11. Structure of Operating System
12. Process in Operating System
13. What is Bios
14. What is Booting in Computer
15. What is Interrupt in Operating System?
16. Process Control Block in Operating Systems
17. Threads in Operating System
18. Process Synchronization in OS
19. Critical Section in OS
20. Semaphore in Operating System
Now Reading
21. Deadlock in Operating System
22. Deadlock Prevention in OS
23. Paging in Operating System
24. Segmentation in Operating System
25. Virtual Memory in Operating System
26. File System in Operating Systems
27. Page Table in OS
28. Round Robin Scheduling in Operating System
29. Shortest Job First Scheduling Algorithm
30. Priority Scheduling in OS
31. Page Replacement Algorithms in Operating System
32. Race Condition in OS
33. Distributed Operating System
34. Navigating Contiguous Memory Allocation in Operating Systems
35. Fragmentation in Operating System
36. Banker’s Algorithm in OS
37. Context Switching in OS
38. First Come First Serve (FCFS) Scheduling Algorithm in Operating System
39. Understanding Inter Process Communication in OS
40. Multiprogramming Operating System
41. Python OS Module
42. Preemptive Priority Scheduling Algorithm
43. Resource Allocation Graph in OS
44. Scheduling Algorithms in OS
45. System Calls In Operating System
46. Thrashing in Operating Systems: A Deep Dive
47. Time Sharing Operating System
Semaphore is an important concept to learn if you want to know how an Operating System manages shared system resources. It is a very crucial topic upon which questions appear in all sorts of computer science-based competitive exams. Having years of experience with OS (Operating Systems), let me walk you through the fundamental concepts of how a semaphore in OS works.
To start this tutorial, let me first define semaphore in OS.
In its most basic form, a semaphore in OS is a variable that manages resource access. It might contain a number that represents how many slots or resources are available for use by processes. A process looks up the semaphore when it wants to use the resource.
To understand a semaphore in OS example compared to real life, let us take the role of semaphore in OS and compare it with the role of a cop in real life managing traffic. The semaphore is like a police officer of the operating system. It distributes shared resources among various processes.
Imagine you have several processes trying to get the same resource. Much like a police officer maintaining traffic, if the police officer does not make sure the traffic lights are coordinated and every car is unorganized, it would wreak havoc. This would result in crashes left and right.
Similarly, semaphore needs to coordinate which processes get access to shared resources at a particular time to avoid a lot of problems. It can lead to deadlocks, race conditions, and more if not implemented correctly.
Before I explain semaphore in OS, let's take a quick look at their backstory. Knowing where they came from can help us understand their purpose better and might even come in handy during interviews!
The story starts with a Dutch computer scientist named Edsger Dijkstra, the same person we know from Dijkstra's algorithm. He proposed the use of semaphores to tackle the issue of sharing resources between simultaneously running processes. The name semaphores is taken from the maritime industry where it means to signal something using flags. He introduced two actions related to semaphores namely wait(also known as "P" operation) and signal(also known as "V" operation).
Semaphores have evolved and improved over time to meet a variety of simultaneous processing difficulties and requirements in operating systems. Different types of semaphores, such as binary and counting semaphores, have been developed to meet various synchronization scenarios and resource management duties.
There are mainly two types of semaphore in OS. Let me explain them in detail to you.
In this section, let me explain binary semaphore in OS with some key pointers:
Now, let me explain counting semaphores. They are both more complex to implement and difficult to debug.
Operating systems use these two kinds of semaphores as basic synchronization techniques to help simultaneous processes or threads coordinate and communicate with one another for shared resources. Developers can use binary or counting semaphores to design efficient synchronization techniques, depending on the particular requirements of a given application or system.
We already know semaphores in OS work as a resource coordinator. But ever wondered how it functions? Let me explain semaphore in OS step by step here.
A certain value is initialized for semaphores. While counting semaphores can have any non-negative integer as their initialization, binary semaphores typically have one of two possible values.
A thread or process tries to get the semaphore linked to a shared resource in order to gain access to it. How this operation is performed defines if a semaphore is binary or counting in nature. In the case of binary semaphores, the counter value keeping track of resources only has two states. In the case of counting semaphores, the value of the counter variable is not limited between two states of only 0 and 1.
A process or thread releases the semaphore linked to it once it has completed using the shared resource.
The working of a semaphore in OS also uses two processes called blocking and wakeup. Let us understand what are they.
In this section, let me explain the concept of mutual exclusion and atomicity:
Semaphores are very useful in the realm of OS. Let me tell you about all the advantages provided by semaphores in the context of OS.
While semaphores are very important for synchronizing processes and resources in OS, they also have their fair share of problems. Let me discuss some with you.
To sum up, a semaphore in OS is extremely important for the optimal functioning of an operating system. We have learned different types of semaphores, and their use cases to build robust and efficient operating systems.
To pursue a career in this field, you will need more insight into complex theories just like semaphores. Looking into courses from upGrad might be a good place to start. Their courses are affiliated with some of the best colleges around the world and are curated with the best professors in the field.
A semaphore in OS is a coordination tool that controls access to shared resources used by different simultaneously working processes or threads.
Incorrect implementation of semaphores can lead to a lot of problems. Semaphores wrongly implemented can lead to deadlocks, resource hogging, and overhead issues.
There are mainly two types of semaphores. They are binary semaphores and counting semaphores.
The term ‘Semaphore’ comes from the maritime industry meaning “signaling”.
There are a lot of advantages to using semaphores. Some of them are better efficiency, deadlock prevention, and bug prevention.
You should use semaphores to control the distribution of shared resources between processes
Yes. semaphores can be used across different processes and threads.
No, other synchronization mechanisms like mutexes, condition variables, monitors, read-write locks, barriers, and atomic operations also exist in operating systems.
Mukesh Kumar
Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...