BCA / B.Tech 12 min read

Semaphores in Operating System

Semaphores in Operating System:


A semaphore is a synchronization technique used to establish coordination between multiple processes or threads. Its main purpose is to ensure that more than one process or thread does not use a shared resource at the same time. Semaphores are primarily used to solve problems of Mutual Exclusion and Critical Sections. Although misuse can lead to issues like deadlock and starvation, they are a powerful tool when used carefully.

What is a Semaphore?

A semaphore is a variable or counter used to track how many processes or threads can access a shared resource. Its main goal is to provide resources to processes in a proper order and to avoid problems like deadlocks or race conditions.

There are two main types of semaphores:

  • Binary Semaphore: This type of semaphore can have a value of either 0 or 1. It is also known as a mutex. It is mainly used to enforce mutual exclusion, ensuring that only one process can enter a critical section at a time.
  • Counting Semaphore: A counting semaphore tracks the number of available instances of a resource. It stores a non-negative integer value, indicating how many processes or threads can access the resource. It is used when multiple instances of a resource can be used by several processes.

Operations on Semaphores:

A semaphore supports two main types of operations:

Wait Operation (often called P()):

This operation is used when a process or thread wants to enter a critical section. It works as follows:
  • If the semaphore's value is positive, its value is decremented by one, and the process enters the critical section.
  • If the semaphore's value is 0 or negative, the process waits until the semaphore's value is incremented and it is allowed to use the resource.

Signal Operation (often called V()):

This operation is used when a process exits the critical section and wants to inform other processes that the resource is now available. In this process, the semaphore's value is incremented by one, giving waiting processes a chance to use the resource.

Advantages of Semaphores:

  • Mutual Exclusion: Helps enforce mutual exclusion.
  • Efficient Resource Management: Ensures proper use of resources.
  • Flexibility in System: Can be used in any system to control any type of resource.
  • Prevention of Deadlock and Starvation: Can be used to avoid such problems through proper management.

Limitations of Semaphores:

  • Complexity: Implementation and use can be complex.
  • Risk of Deadlock: Improper use can lead to deadlock.
  • Starvation Issue: A process might wait indefinitely if not managed correctly.
  • Difficulty in Debugging: Errors can be hard to debug, especially in complex systems.

In this Chapter

Semaphores in Operating System
Distributed System in Operating System
Real-Time System in Operating System
System Calls in Operating System
System Programs in Operating System
Structure of an Operating System
Layered Design of an Operating System Structure
UNIX in Operating System
Virtual Machine in Operating System
Kernel-Based Operating System
Process Concept in Operating System
Interacting Processes in Operating System
Threads in Operating System
Fundamentals of Scheduling in Operating System
Scheduling Criteria in Operating System
Long, Medium, and Short-Term Scheduling
Scheduling Algorithms in Operating System
Structure of a Concurrent System
Critical Region in Operating System
Critical Section in Operating System
Inter-process Communication (IPC) in Operating System
Monitors in Operating System
Semaphore Implementation & Uses in Operating System
Logical and Physical Address in Operating System
Swapping in Operating System
Contiguous Allocation in Operating System
Segmentation in Operating System
Paging in Operating System
Virtual Memory using Segmentation in Operating System
Interprocess Communication Protocol in Operating System
Network Operating System
Design Issues in Distributed File System
Network Structure in Operating System
Structure of a Distributed System
File System and Coordination in Operating System
History of Linux in Operating System
Linux Commands
Programmer Interface & File Manipulation in Linux
Process Control in Linux
Kernel & Signals in Linux
File System in Linux
Blocks and Inodes in Linux
System Editors in Linux
Character Transliteration in Linux
The `ed` Line Editor in Linux
The `vi` Editor and Its Commands
Shell Scripting in Linux
Looping & Decision Making in Linux Shell Scripting
Variables & File Name Expansion in Linux
Arrays in Linux Shell Scripting
Subprograms (Functions) in Linux Shell Scripting
C Interface with Linux
Simple Shell Programs in Linux
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is an Operating System (OS)
Operating System (OS) All Important Questions and Answers in English (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Types of Operating Systems
Goals of an Operating System
Operations of an Operating System
Resource Allocation & Functions in an Operating System
Classes of Operating System
Batch Processing in Operating System
Multiprocessing in Operating System
Time-Sharing in Operating System