Re: Locking a shared memory object (crash safe )
site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com On 19 Mar 2008, at 14:28, Nicholaz Beresford wrote: creation: sem=sem_open(...) sem_unlink(sem) Jonas _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... 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, 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). This email sent to site_archiver@lists.apple.com
participants (1)
-
Jonas Maebe