QNX RTOS v4 Knowledge Base
QNX RTOS v4 Knowledge Base
Title |
Shared Memory file entry disappearing |
Ref. No. |
QNX.000003977 |
Category(ies) |
Development |
Issue |
We want to use shared memory to store/pass data between processes. We have a test program that demonstrates the functionality of using "shm_open", "ltrunc", "mmap" and "shm_unlink" .
The problem is: Referencing the POSIX.4 book, following paragraph (pg 118)states that: "Unlinking shared memory has the same semantics as unlinking a file or a message queue. If any processes are using the shared memory when it is unlinked, then those instances of the shared memory object remain viable and stable until each individual process ceases to use the shared memory. In the case of shared memory, of course, that means that each process must close, and also munmap, their shared memory before it truely ceases to exist. exit and exec implicity close shared memory instances, just like they close files and message queues."
My method of testing: we open three pterms in Phindows, one to create the shared memory area (and read/write from it), the second to watch it, and the third to read from it (using shm_open and mmap).
We write to memory in the first window and read it back the in the third window. Everything works great.
Now the problem: we exit the read program and check for the shared memory "file" in the second window, its gone, OR is it gone? The ls command doesn't show it. We start up the Read program and check to see if it reads the same characters it read before, it doesn't. We assume the shared memory is now gone. Looking at the quote from POSIX, it should still be there, since the read/write process is still running.
|
Solution |
When you do the shm_unlink() the name is removed from the file system. If other processes are still linked, the memory still exists and they can use it but no one else can open it. When they close there fd's the memory will be released.
The shm_unlink() should be called by the process that opens it to prevent any problems.
shm_unlink() is the same as the unlink command with regards to files. |
|