One way to synchronize multiple processes is to use shared memory segments:
/* get id of shared memory segment with key shmkey - if not existing, create one */
int shmid = shmget(shmkey, size, IPC_CREAT | S_IRUSR | S_IWUSR);
/* attach the shared memory segment */
void *mem = shmat(shmid, 0, 0);
/* do some read/write stuff on shared memory */
/* detach shared memory segment */
shmdt(mem);
/* 'remove' shared memory segment */
shmctl(shmid, IPC_RMID, NULL);
The last step will not remove the shared memory segment immediately. It will mark the shared memory segment as to be deleted. If all processes are detached, the shared memory segment is deleted.
So the question is, can we can use that behaviour as is for an automatic cleanup of a shared memory segment, if we think of multiple in parallel running processes with different starting times.
Continue reading Cleanup of shared memory segments used by multiple processes