Discussion:
regarding posix shm_unlink
(too old to reply)
Anil
2005-01-06 23:33:45 UTC
Permalink
Hi All,
Posix shared memory implementation provides the shm_open and shm_unlink
methods to create and detact/delete the shared memory.
shm_unlink detacts from the shared memory segment and if the reference
count for that segment is zero it automatically deletes it.

But I kind of see this as a resistriction as compared to the system
defined shared memory, where you explicity detach from the shared
memory and delete the shared memory.

Posix is giving me resistriction in situation where one process creates
a shared memory and fills some data and goes out of scope. At a later
time, some other process comes up attaches to this shared memory and
reads the values. With the reference count resistriction in posix,
atleast one process should be attached to the shared memory for it be
existing.

Can anyone suggest a way where we can keep the shared memory in posix
even if there is no process attached to it.

Regards
ajay kumar
2005-01-08 12:52:44 UTC
Permalink
You have rightly got it that shm_open and shm_unlink methods are to
create and delete the shared memory objects, but I guess you have some
confusion as of how to use it. If you want to use it as communication
channel between two different process, why should you call shm_unlink
in the first process itself (if it is exiting before the other process
can use it). What you have to actually do is create the shared memory
object (using shm_open), map it in your process address space, fill it
with data you, unmap it and exit (without calling shm_unlink). The
shared memory object will continue to exist. Then, in the second
process, get the shared memory object (using shm_open with same shared
memory object name as which you created it), map it and use the way you
want it to. If you are sure that you (or any other process) wouldn't
require it once this process is over, call shm_unlink to delete this
shared memory object before exiting.

So, the thing is shared memory object doesn't vanish if the process
exits unless the process itself have called to delete the shared memory
object.
I hope it answered your problem.

- Ajay Kumar

Loading...