LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Releasing RWLocks from SHM when process dies (https://www.linuxquestions.org/questions/programming-9/releasing-rwlocks-from-shm-when-process-dies-688446/)

rajkiran.ravi 12-04-2008 11:50 PM

Releasing RWLocks from SHM when process dies
 
Hi

This is my first time on the forum. Need help with POSIX RWLocks in Shared Memory.

I have the following scenario:
A process creates an SHM area and initializes an RWLock in the SHM area. If it takes the lock and gets killed, is there any way to release this lock?

Raj

ta0kira 12-06-2008 03:59 PM

I would look at mmap as a replacement for shm; shm causes quite a few problems related to what you're saying, namely that the segment won't be freed unless one process flags it, even if no existing process holds reference to it. You really shouldn't have to ask that question; it should happen automatically. If it doesn't then that sounds like a majorly-flawed system to me.
ta0kira

PS As with anything that has external cleanup, though, you should have a signal handler for every possible signal that can cause termination that will unlock the memory. The exception is SIGKILL, but there's nothing you can do about that. That's why SIGKILL is highly discouraged.

rajkiran.ravi 12-08-2008 01:04 AM

Thanks for the reply.

Infact I am using Posix based shared memory also... for which I did an shm_open, ftruncate and mmap()

However, for some reason the locks in SHM are not released. Shouldnt this be the responsibility of the kernel to clean up the lock?

Thanks

ta0kira 12-08-2008 02:50 AM

So you're saying that the locks with shm only (i.e. not mmap) are persistent? That sounds accurate. If that's actually the case then that is the reason I recommend mmap because it should take care of all of that; locks should be associated with the file, and file locks should dissolve when the process exits. With shm, though, you don't have anything underlying except for actual memory.
ta0kira

sundialsvcs 12-08-2008 08:59 AM

:scratch: I'm not aware of any situation where a lock held by a process won't get cleaned-up when it dies...


All times are GMT -5. The time now is 03:49 PM.