Re: Locking a shared memory object (crash safe )
Re: Locking a shared memory object (crash safe )
- Subject: Re: Locking a shared memory object (crash safe )
- From: Jonas Maebe <email@hidden>
- Date: Wed, 19 Mar 2008 14:38:30 +0100
On 19 Mar 2008, at 14:28, Nicholaz Beresford wrote:
I'm porting an application from WIN32 which does shared memory.
Under Windows we use a Named Mutex to lock the memory. This works
well even if the app crashes (Win32 releases the mutex when crashing).
Under darwin we're doing it via posix shared-mem and posix sems, but
posix sems retain their states when the app goes down, so from that
point every access to the shared mem will lock until the machine is
rebooted.
You can manually delete named semaphores using the ipcs/ipcrm command
line program, there's no need to reboot (or you can write a helper
program which does a sem_open() of the semaphore without O_CREAT|
O_EXCL, followed by a sem_close()).
Furthermore, you can ensure that the semaphore is freed when your
program ends (crashing or not) by immediately unlinking the semaphore
after you open it. So,
creation:
sem=sem_open(...)
sem_unlink(sem)
destruction (which also happens automatically when the process stops
due to the unlink above):
sem_close(sem)
Note that if you unlink the semaphore right after creating it, trying
to sem_open() the same semaphore (i.e., passing the same name to
sem_open) will get you a new semaphore rather than the previously
opened/unlinked one (and this may or may not be a problem).
Jonas
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden