Delete shared memory block
(PHP 4 >= 4.0.4, PHP 5)
Example 2232. Deleting shared memory block
Code Examples / Notes » shmop_delete
There is an important factor to keep in mind here, the shmop_delete function doesn't actually "delete" the memory segment per say, it MARKS the segment for deletion. An shm segment can not be deleted while there are processes attached to it, so a call to shm_delete will two things, first no other processes will be allowed to attach to the segment after this call, and also, it will mark the segment for deletion, which means that when all current processes mapping the segment detach (shmop_close) the system will automatically delete the segment.
So, again, the proper way to close and delete an shm segement is:
$shmid = shmop_open(...);
(do something with the block here: read, write, whatever)
you must call the shmop_delete before you call shmop_close for reasons outlined above.
Also to mark a segment for deletion you must be root or the owner.
A few notes from the linux manpage on shmctl:
IPC_RMID (the flag used in shmop_delete):
is used to mark the segment as destroyed. It will actually be destroyed after the last detach. (I.e., when the shm_nattch member of the associated structure shmid_ds is zero.) The user must be the owner, creator, or the super-user.
The user must ensure that a segment is eventually destroyed; otherwise its pages that were faulted in will remain in memory or swap.
A helpful hint, although when using shmop on windows (yes you can use shmop on windows in IIS or Apache when used as isapi/module) you can delete a segment and later open a new segment with the SAME KEY in the same script no problem, YOU CANNOT IN LINUX/UNIX - the segment will still exist - it's not immediately deleted - but will be marked for deletion and you'll get errors when trying to create the new one - just a reminder.