I'm using a large number of semaphores to synchronize multi-process access to shared memory.
I initialize the semaphores via
int scount = 80000;
int semid = semget(IPC_PRIVATE, scount, 0666 | IPC_CREAT);
which works fine.
At the end of the program I remove the semaphore set via
semctl(semid, 0, IPC_RMID);
However, when I check the number of semaphores in the system via
ipcs -S
in the console, I see they are still there.
seminfo:
semmap: 30 (# of entries in semaphore map)
semmni: 8 (# of semaphore identifiers)
semmns: 80064 (# of semaphores in system) <------ ????
semmnu: 0 (# of undo structures in system)
semmsl: 87381 (max # of semaphores per id)
semopm: 5 (max # of operations per semop call)
semume: 10 (max # of undo entries per process)
semusz: 32 (size in bytes of undo structure)
semvmx: 32767 (semaphore maximum value)
semaem: 16384 (adjust on exit max value)
I also tried to remove them manually in the console via
ipcrm -s [semid]
which seems to remove the set fine (i.e. the semid is listed in ipcs -s before the command and gone after). Yet ipcs -S still shows the
semmns: 80064 (# of semaphores in system)
How do I properly cleanup the semaphores after usage, so they do not show up in "semmns: .... (# of semaphores in system)" anymore?
The reason I'm asking is that if the semaphores are not properly cleaned up, my next run of the program won't work because the number of open semaphores will exceed the limit. I need to restart to reset it to 0.
PS: I'm on Mac OS, but would also want it to work on Linux.
semget does not return an error. The error occurs at the next step when I initialize the semaphore set via:
semun_t semun = {.val = 1}; // initial semaphore value => 1 = released
for (int i=0; i<scount; i++){
if(semctl(semid, i, SETVAL, semun) == -1) {perror("semctl init");exit(1);}
}
The error I get there is:
semctl init: Invalid argument