System V Semaphores

 

System V Semaphores
TLPI Chapter 47 — Complete Tutorial Series | EmbeddedPathashala
3
Tutorial Parts
10+
Code Examples
20+
Interview Questions
Ch47
TLPI Reference

Why Learn System V Semaphores?

System V semaphores are a fundamental IPC (Inter-Process Communication) mechanism in Linux. They appear in embedded Linux systems, real-time applications, multi-process servers, and legacy codebases. Understanding them is essential for:

  • Interviews at companies like Texas Instruments, Qualcomm, and STMicroelectronics
  • Writing multi-process Linux applications with shared resource control
  • Understanding legacy code that uses SysV IPC
  • Linux kernel internals and POSIX IPC foundations

Unlike POSIX semaphores (sem_open/sem_wait), System V semaphores are organized in sets, managed entirely by the kernel, and persist until explicitly deleted — making them powerful but requiring careful resource management.

Key Terms in This Series

semget() semctl() semop() semun union semid_ds ipc_perm IPC_CREAT IPC_EXCL IPC_RMID IPC_STAT IPC_SET SETVAL GETVAL SETALL GETALL GETPID GETNCNT GETZCNT IPC_PRIVATE ftok() sem_otime sem_ctime sempid semncnt semzcnt

System V IPC — Big Picture

Semaphores are one of three System V IPC mechanisms. This series focuses on semaphores.

Semaphores
Synchronization & signaling between processes
← This Series
Message Queues
Passing messages between processes
Ch46 Series
Shared Memory
Fast data sharing between processes
Ch48 Series

All three share the same key infrastructure: ftok() for key generation, IPC_CREAT/IPC_EXCL flags, and ipc_perm for ownership. The ipcs command lists all SysV IPC objects on your system.

What Makes System V Semaphores Different — The Set Concept
One semget() call = One semaphore SET (not just one semaphore)
semget(key, 4, …)
1
sem[0]
0
sem[1]
3
sem[2]
5
sem[3]
Single semid,
4 semaphores,
indexed 0–3

POSIX semaphores are single-valued objects. System V semaphores are grouped in sets — one kernel identifier controls a whole array of semaphore counters. This is useful for atomically managing access to multiple resource types simultaneously (e.g., semop() can modify multiple semaphores in a single atomic operation).

Tutorial Parts

Part 1 — semget(): Creating and Opening Semaphore Sets
semget() signature key argument IPC_PRIVATE ftok() nsems rules semflg bits IPC_CREAT IPC_EXCL Error codes Parent-child sharing

Learn how to create a new semaphore set or open an existing one, understand the key mechanism, and handle all error conditions.

Part 2 — semctl(): All Control Operations + semun Union
semctl() signature semun union IPC_RMID IPC_STAT IPC_SET SETVAL/GETVAL SETALL/GETALL GETPID GETNCNT GETZCNT Cleanup patterns

Master all semctl() operations with complete working code examples and a reusable helper library.

Part 3 — semid_ds: The Associated Data Structure
semid_ds fields ipc_perm sem_otime sem_ctime sem_nsems Readiness detection Dynamic GETALL Portability issues Set inspector program

Understand the kernel metadata structure behind every semaphore set and how to read, interpret, and change it.

Shell Commands — Inspecting SysV Semaphores
# List all System V semaphore sets on the system
ipcs -s

# Detailed view with times and permissions
ipcs -s -t
ipcs -s -c
ipcs -s -l   # show system limits

# Remove a semaphore set by semid
ipcrm -s <semid>

# Remove by key (hex)
ipcrm -S 0x12345678

# Show kernel limits for semaphores
cat /proc/sys/kernel/sem
# Output format: SEMMSL SEMMNS SEMOPM SEMMNI
# SEMMSL = max semaphores per set
# SEMMNS = max total semaphores system-wide
# SEMOPM = max operations per semop() call
# SEMMNI = max number of semaphore sets

Top Interview Questions — Quick Reference

What are System V semaphores?

Kernel-maintained integer counters organized in sets, used to synchronize access to shared resources between multiple processes.
How do you create a semaphore set?

semget(key, nsems, IPC_CREAT | perms) — returns a semid (integer identifier).
How do you initialize semaphore values?

semctl(semid, semnum, SETVAL, arg) for one, or semctl(semid, 0, SETALL, arg) for all.
How do you remove a semaphore set?

semctl(semid, 0, IPC_RMID) — note it is NOT automatic on process exit.
What is the semun union?

A union you must define yourself containing val (int), buf (semid_ds*), and array (unsigned short*) — used as the 4th argument to semctl().
What is GETNCNT used for?

Returns the number of processes currently blocked waiting for a semaphore’s value to increase — useful for debugging deadlocks.

Free Embedded Systems Education

EmbeddedPathashala covers Linux System Programming, BLE/Bluetooth, and Embedded C — all free for engineering students and professionals.

Start Part 1: semget() → Visit EmbeddedPathashala

Leave a Reply

Your email address will not be published. Required fields are marked *