Site icon DataFlair

Semaphores in Operating System

OS Semaphores

FREE Online Courses: Your Passport to Excellence - Start Now

Integer variables that help solve the critical section problem are known as semaphores. Wait and signal are the two atomic operations that help achieve process synchronization in a multiprocessing environment..

If an argument, let’s say argument A is positive, then the wait (sleep or down) operation decrements its value. And if argument A is negative or zero, then no operation occurs. Whereas, the signal (wake-up or up) operation increments the value of argument A.

P(A) is wait and V(A) is signal.

P(A): if A >= 1 then A := A - 1
      else <block and enqueue the process>;

V(A): if <some process is blocked on the queue>
        then <unblock a process>
      else A := A + 1;

Properties of Semaphores

Following are the properties of semaphore:

Types of Semaphore

There are two types of semaphores:

1. Counting Semaphore

Counting semaphores are integer value semaphores. They have an unrestricted value domain and are used to coordinate resource access. During this coordination, the count of semaphores is equal to the number of available resources and if a resource is added or removed, then the count of semaphores changes accordingly.

2. Binary Semaphore

Binary semaphores are similar to the above, just that their value is restricted to 0 and 1. The wait and signal operation only works when the semaphore is 1 and 0 respectively. Binary semaphores are easier to implement.

Counting Semaphore vs Binary Semaphore

COUNTING SEMAPHORE BINARY SEMAPHORE
No mutual exclusion Mutual exclusion is present
Any integer value possible Two integer values, 0 and 1
More than one slots Only one slot
Offers a set of processes Contains mutual exclusion mechanism

Semaphore vs Mutex

PARAMETER SEMAPHORE MUTEX
Data Type Integer variable Object
Mechanism Signaling mechanism Type of locking mechanism
Types Binary and Counting  No Type
Operation Wait and signal modifies the semaphore’s value  Locked and Unlocked operation
Thread Multiple program threads Multiple program threads that aren’t simultaneous

Advantages of OS Semaphores

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Following are the advantages of semaphore:

Disadvantages of Semaphore in Operating System

Following are the disadvantages of semaphores:

Summary

A semaphore is a variable that helps with critical section problems and process synchronisation. Wait and signal are the two operations that help modify the value of a semaphore. There are two types of semaphores, namely, counting and binary.

Exit mobile version