I need to port some applications from Windows to Linux, and they use shared memory and semaphores. I have to replace some Winddows functions like CreateFileMapping / OpenFileMapping and CreateSemaphore / OpenSemaphore. I will write a common interface for semaphore and shared memory later.
On Windows, when all descriptors are closed, the resource (semaphore or shared memory) is deleted by the system. However, Unix requires me to delete the resource, with shm_unlink (shared memory, open shm_open) and sem_unlink (semaphore, open sem_open). shm_open is good because it does not create a real file on disk.
I cannot use shmget because a string requires a number as an identifier. ftok is completely useless as it can generate collisions.
I cannot call * _unlink before all descriptors are closed, because it does not know when a new process is not needed, and the process may crash or it may be manually killed by the user.
If I do not disconnect them, they will leave garbage in the system, and if the process group is started again, they will not know if the resources are used by other processes or are old (this was not disconnected, the user killed the process or the process crashed).
source share