Location>code7788 >text

System V semaphore vs. POSIX semaphore: Core difference and selection guide

Popularity:289 ℃/2025-04-05 17:44:49

System V semaphore vs. POSIX semaphore: Core difference and selection guide

Recently, I have been exposed to two semaphores in the chapter on learning Linux system programming, so I have studied the differences between the two and recorded the comparison of the two here.
In Linux multithreaded/process development, semaphores are one of the core tools to solve synchronization problems. System V and POSIX are two mainstream implementation methods, and their differences directly affect development efficiency and performance. Here is a summary of the key differences between the two:


1. The underlying implementation mechanism

characteristic System V semaphore POSIX semaphore
Kernel dependencies Maintained by the kernel, life cycle has nothing to do with the process It is divided into two types:
• Famous semaphores (kernel maintenance, file association)
• Nameless semaphore (process memory maintenance)
Persistence Permanent before explicit deletion (requiressemctl(IPC_RMID) The nameless semaphores die with the process; the nameless semaphores need to be deleted manually

2. API design comparison

Operation Type System V function POSIX functions
initialization semget() + semctl(SETVAL)Two-step operation nameless:sem_init()
famous:sem_open()
PV operation semop()(Supports atomic batch operation) sem_wait()(block)
sem_post()(release)
Error handling Through global variableserrnojudge The function returns the error code directly (such asEAGAIN

3. Differences in applicable scenarios

Scene Recommended plan reason
Complex synchronization across processes System V semaphore set (supports multi-semaphore atomic operation) Can operate multiple semaphores at the same time to avoid deadlocks
Lightweight synchronization between threads POSIX nameless semaphore (sem_init Memory-based, no kernel overhead, higher performance
Simple inter-process synchronization POSIX famous semaphore (sem_open The API is simpler and compatible with modern programming specifications

4. Functional characteristics comparison

characteristic System V POSIX
Semaphore set ✔️ Supports multi-semaphore sets (such assemget( ,5) ❌ Only single semaphore operation is supported
Timeout mechanism ❌ Custom implementation required ✔️ sem_timedwait()Support timeout waiting
Semaphore value range No explicit limits (kernel parameter constraints) Nameless semaphores are usually limited to 32-bit integers

5. Performance measurement data (reference)

  • 100,000 PV operation time (i7-12700H, Ubuntu 22.04):
    • System V semaphore: ~85ms (requires frequent kernel switching)
    • POSIX nameless semaphore: ~12ms (user state atomic operation)
    • POSIX famous semaphore: ~45ms (involving file system path resolution)

6. Selection suggestions

  1. Priority POSIX scenarios:

    • Need to be compatible with C++11 or above standards (such asstd::counting_semaphore
    • Inter-thread synchronization or simple process synchronization (such as shared memory counters)
    • Strict performance requirements (such as high-frequency trading systems)
  2. Scenarios that stick to System V:

    • Multiple semaphores need to be operated simultaneously (such as bank transfers require atomic locking of the account and balance)
    • Legacy system compatibility requirements (such as CentOS 6 legacy systems)

Attachment: Typical code snippets
System V semaphore set initialization

key_t key = ftok("/tmp", 'S');
 int semid = semget(key, 3, 0666 | IPC_CREAT); // Create 3 semaphores
 union semun arg;
  = (unsigned short[]){1, 1, 1};
 semctl(semid, 0, SETALL, arg); // All are initialized to 1

POSIX famous semaphore

sem_t *sem = sem_open("/mysem", O_CREAT, 0666, 1);
 sem_wait(sem); // P operation
 // Critical area operation
 sem_post(sem); // V operation
 sem_close(sem);
 sem_unlink("/mysem"); // Delete the kernel object

Summary: POSIX semaphores are the first choice for modern development, but System V is still irreplaceable in complex process synchronization. When choosing, you need to weigh performance, functional requirements and system compatibility.