1 of 11

Operating System (CS2701)

semaphore

Dr. Rourab Paul

Computer Science Department, SNU University, Chennai

Operating System

2 of 11

Semaphore

Operating System

2

A semaphore is a synchronization mechanism used in operating systems and concurrent programming to control access to a shared resource by multiple threads or processes using simple integer value.

  • A counter (integer) stored by the OS.
  • A semaphore is an integer whose value is never allowed to fall below zero.
  • This semaphore value can be access only by two atomic operations:
    • wait() / P() / sem_wait() → decrements the counter; if counter < 0, the calling thread blocks until it becomes ≥0 again. P for ‘proberen’, means ‘test’
    • signal() / V() / sem_post() → increments the counter; if any threads are waiting, one is unblocked. V for verhogen, means ‘increment’

3 of 11

Semaphore

Operating System

3

Mutex = “I’m using this resource — wait until I’m done.”

Signal/Semaphore = “Hey, I’ve finished something — what you are saying

line is

busy

4 of 11

Semaphore

Operating System

4

Mutex = “I’m using this resource — wait until I’m done.”

Signal/Semaphore = “Hey, I’ve finished something — you can sta

bye

5 of 11

Definitions of wait and signal(post)

Operating System

5

wait(S) {

while (S <= 0)

; // busy

wait S--;

}

signal(S) {

S++;

}

All the modifications to the integer value of the semaphore in the wait() and signal() operation must be executed indivisibly. That is when one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value

6 of 11

Counting vs Binary Semaphores

Operating System

6

Counting Semaphore

  • Value ranges over an unrestricted domain
  • Used to control access to a resource with multiple instances
  • Initialized to the number of available resources�

Binary Semaphore

  • Value can only be 0 or 1
  • Behaves like a mutex lock
  • Can be used for mutual exclusion if no mutex locks exist

7 of 11

named & unmanned semaphore

Operating System

7

Unnamed semaphore = a physical ticket box inside your office — only people already in your office can see and use it.�

Named semaphore = a public locker with a name/number in the building lobby — any process with the name can use it.

8 of 11

unnamed semaphore

Operating System

8

In the process’s memory (shared memory or global variable).

  • sem_init() is used
  • Scope:
    • By default for threads within one process.
    • 2nd argument ‘0’ = semaphore is shared between threads in the same process.
    • 2nd argument ‘1’ (nonzero) = semaphore is shared between processes, and must be placed in shared memory
  • No name in the system: It’s just a variable you allocated.
  • ✅ Fast, simple for intra-process synchronization.
  • ❌ No persistence; you can’t open it by name later.

sem_t sem;

sem_init(&sem, 0, 1); // 0 = shared between threads, 1 = initial value

sem_wait(&sem); // acquire

sem_post(&sem); // release

sem_destroy(&sem);

9 of 11

named semaphore

Operating System

9

In the kernel, identified by a string name (like a file).

  • sem_open("/mysem", O_CREAT, 0644, 1) is used
  • Any unrelated processes can open the same name and synchronize.
  • It appears under /dev/shm/sem.mysem on Linux.
  • Needs explicit unlink: sem_unlink("/mysem");
  • ✅ Works across unrelated processes easily.
  • ✅ Survives even if one process dies (until sem_unlink() is called).
  • ❌ Slightly more overhead, needs cleanup.

sem_t *sem = sem_open("/mysem", O_CREAT, 0644, 1);

sem_wait(sem); // acquire

sem_post(sem); // release

sem_close(sem);

sem_unlink("/mysem"); // remove name

10 of 11

Sample Programs

Operating System

10

11 of 11

Thank You

Operating System

11