Reputation: 793
i'm trying to write some code in c dealing with sheared memory in linux,
and I'm not sure when to use the system call shmdt
,
and when to use shmctl (segment_id, IPC_RMID, 0);
I looked at the man page of shmdt, and read this sentence: "shm_nattch is decremented by one. If it becomes 0 and the segment is marked for deletion, the segment is deleted."
what is that marking that the man page mentions? is it shmctl (segment_id, IPC_RMID, 0);
?
and dose that mean that shmctl (segment_id, IPC_RMID, 0);
will not detach the segment if there is a process that is still connected?
if someone can explain what dose each call do, I'll be grateful. Thanks
Upvotes: 3
Views: 577
Reputation: 115
to unmap the shared memory mapping from process address space use system call
shmdt(shared memory start virtual address)
but to delete the shared memory segment use shmctl()
with IPC_RMID
or ipcs
command.
Shared memory segment data structures are maintained inside Linux kernel so deleting shared memory segment means removing or freeing the data structures from kernel.
Upvotes: 0
Reputation: 18410
shmdt()
reverses the shmat()
-operation.
shmat: Maps the shared memory segment in a processes address space
shmdt: Unmaps it again
shmctl (segment_id, IPC_RMID, 0);
marks the segment for deletion, this means, it is the counteroperation to creating the shared memory segment with shmget(..., IPC_CREAT)
. If the reference counter is 0 when deleting, the segment is deleted immediately. Otherwise, deletion is deferred until the last process unmaps it (either explicitly with shmdt()
or implicitly by terminating).
Upvotes: 4