Use a named semaphore with a score of 1. At startup, check to see if this semaphore is accepted. If so, give it up. Otherwise, take it.
Proof of concept code: (put somewhere near the application entry point)
#include <semaphore.h> ... if (sem_open(<UUID string for my app>, O_CREAT, 600, 1) == SEM_FAILED) { exit(0); }
From the sem_open documentation ,
The returned semaphore descriptor is available to the calling process until it is closed with sem_close () or until the caller exits or executes the command.
source share