pikpik 0 Newbie Poster

Hi,

As part of porting a (mostly C++) library[1] to a "new" operating system[2], I'm trying to link the library's semaphore class to the platform's semaphore implementation. The problem is that the library assumes that the semaphore implementation will be a modern POSIX one[3] but the operating system's semaphores are modeled after System V[4].

My question is primarily if anyone knows of a wrapper of C (or C++) code that will act as an emulation layer around the System V semaphores in a way that will present "modern" POSIX semaphores to the library I'm porting. A caveat is that any used code must be compatible with the BSD license.

Thank you,
pikpik


1. The library, semaphore example


2. The operating system

http://www.minix3.org/


3. POSIX Semaphores

http://www.opengroup.org/onlinepubs/009695399/basedefs/semaphore.h.html


4. The operating system's System V semaphore implementation

Dani AI

Generated

Short answer: a thin, BSD-compatible shim that exposes the POSIX sem_* API on top of System V semaphores is practical and commonly used. For 's case the simplest pattern is: map a POSIX name to a stable System V key, create/lookup a single-element SysV semaphore set with that key, use semop for wait/post, and keep a small on-disk registry (or a tiny shared-memory counter) to implement sem_unlink and the “remove after last close” semantics.

Key implementation notes (concise)

  • Name → key: create a dedicated directory (eg. /tmp/posixsem/) and atomically create a file derived from the POSIX name; use ftok on that file (or hash the filename) to produce a stable key for semget. Creation should use open(O_CREAT|O_EXCL) to detect creator vs opener.
  • Create/open: call semget(key, 1, perms | IPC_CREAT) and when created set the initial value with semctl(SETVAL). Honor O_EXCL by checking for existence races (IPC_EXCL / EEXIST).
  • sem_wait/sem_post/trywait: implement with semop operations; trywait uses IPC_NOWAIT, post increments by 1.
  • sem_close/sem_unlink: maintain a small metadata file per name containing a reference count and an “unlinked” flag. Protect updates with an advisory file lock (flock). sem_unlink sets the flag; the creator/last closer removes the SysV semaphore (semctl IPC_RMID) when refcount reaches zero.

Small skeleton (concept)

typedef struct { int semid; key_t key; int is_named; } sem_t;

/* sem_open pseudo-steps:
   1) turn name -> path, open/create the path file atomically
   2) ftok(path, proj) -> key
   3) semget(key,1,mode|IPC_CREAT|(oflag&O_EXCL?IPC_EXCL:0))
   4) on create: semctl(semid,0,SETVAL,value)
   5) increment refcount in metadata file (flock)
*/

Caveats and pitfalls

  • sem_timedwait is not available in all SysV kernels; prefer semtimedop if present or document the limitation.
  • SEM_UNDO behavior differs; POSIX semaphores lack SEM_UNDO semantics—don’t rely on process-exit undo unless explicitly emulated.
  • Robust cleanup is crucial: stray IPC objects can persist; the registry approach limits namespace pollution and makes cleanup deterministic.
  • License: placing the wrapper directly in the project under a BSD-compatible header avoids compatibility hassles.

The above gives a practical, testable blueprint that matches POSIX semantics closely while remaining implementable with SysV primitives and a small metadata registry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.