Semaphores In Operating Systems IPC Problems Avoidance


The solutions of the critical section problem represented in the section is not easy to generalize to more complex problems. To overcome this difficulty, we can use a synchronization tool call a semaphore. A semaphore S is an integer variable that, a part from initialization, is an accessed two standard atomic operations: wait and signal. This operations were originally termed P



S = S + 1;


The integer value of the semaphore in the wait and signal operations must be executed indivisibly. That is, when one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value.

In addition, in the case of the wait(S), the testing of the integer value of S (S0), and its possible modification (S: = S – 1), must also be executed without interruption.

Semaphores are not provided by hardware. But they have several attractive properties:

  1. Semaphores are machine independent.
  2. Semaphores are simple to implement.
  3. Correctness is easy to determine.
  4. Can have many different critical sections with different semaphores.
  5. Semaphore acquire many resources simultaneously.
  • Counting semaphore – integer value can range over an unrestricted domain
  • Binary semaphore – integer value can range only between 0 and 1; can be simpler toimplement
    • Also known as mutex locks
    • Can implement a counting semaphore S as a binary semaphore • Provides mutual exclusionSemaphore S; // initialized to 1 wait (S);Critical Section signal (S);Semaphores (Properties)
  • Machine independent: no need to code at assembly level as in TSL
  • Works with any no of processes
  • Can have different semaphores for different critical section
  • Simply binary semaphores or more than one if desired using a counting semaphoreSemaphore Implementation
  • Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time
  • Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section
  • Could now have busy waiting in critical section implementation
  • But implementation code is short
  • Little busy waiting if critical section rarely occupied
  • Note that applications may spend lots of time in critical sections and therefore this is not a good solutionSemaphore Implementation with no busy waiting
  • With each semaphore there is an associated waiting queue

Each entry in a waiting queue has two data items

  • value (of type integer)
  • pointer to next record in the list • Two operations:
  • block – place the process invoking the operation on the appropriate waiting queue
  •  wakeup – remove one of processes in the waiting queue and place it in the ready queue

Implementation of wait:

wait (S) { value–;

if (value < 0) {
add this process to waiting queue

block(); } }

Implementation of signal:

signal (S)



if (value <= 0)

remove a process P from the waiting queue


Drawback of Semaphore



  1. They are essentially shared global variables.
  2. Access to semaphores can come from anywhere in a program.
  3. There is no control or guarantee of proper usage.
  4. There is no linguistic connection between the semaphore and the data to which thesemaphore controls access.
  5. They serve two purposes, mutual exclusion and scheduling constraints.


Suraj Chand

Suraj Chand is an Author in Surajchand99.com.np and is pursuing Bachelor's Degree In Computer Engineering From Kathmandu Engineering College (KEC) IOE TU Nepal.

Leave a Reply

Back to top button

Adblock Detected

Sorry ! Please Disable your adblock .