Management of Unix Shared Memory Management of Unix Shared Memory unix unix

Management of Unix Shared Memory


It not pure reference counting. According to shmctl(2):

IPC_RMID: Mark the segment to be destroyed. The segment will only actually be destroyed after the last process detaches it (i.e., when the shm_nattch member of the associated structure shmid_ds is zero). The caller must be the owner or creator,

This means: IPC_RMID will not delete immediately but only after the reference count drops to zero the next time.

This allows you to achieve several goals with the same tool:

  • Either a server/client approach where the server creates, attaches and immediately sets RMID. Then clients can connect as long as the server is here. If the server goes down, the clients should disconect and the resource is cleaned up by the OS.

  • Or the "message" approach: Someone writes a message, pins it to a known location. After that someone else can come, look for a message and act accordingly. This is what you have done. This approach is more open to resource garbage of course. But there are usecases for this.