LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-31-2008, 09:03 AM   #1
c_prog
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
sem_init and persistent storage in an mmaped file


I'm using sem_init() to create an un-named semaphore.

The semaphore data structure (sem_t *) is stored in memory that is mmaped from a disk file.

This leads me to ask three questions:

- how can a process that opens the file and does an mmap() determine if sem_init() has already been called for the semaphore and if the semaphore is valid?

- if all the processes using the semaphore terminate, and then a new process starts and mmap()s the same file, is the semaphore still valid or must sem_init() be called again?

- if the machine is rebooted, and the process is started again after the reboot, does it need to call sem_init() again, or can it just mmap() the file and start using the semaphore in calls to sem_wait(), etc?

I realise that named semaphores offer a more obvious solution (because sem_open() with O_CREAT will only create a semaphore if one doesn't already exist), but I would prefer to use un-named semaphores if at all possible.
 
Old 11-01-2008, 02:18 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 49
[original comment removed]

I just read the sem_init man page - apparently you can use unnamed semaphores across processes (via mmap).

Now the interesting bit - you can use another flag in the mmap to see if the semaphore has been initialized - except of course you need a lock of some sort to do that (which brings you back to a named semaphore).

How to determine if a semaphore has been initialized? Well, you can start out with an mmap() that's all zeroed out; that makes it a bit easier to check the semaphore. You can also look at the code to see how the semaphore is implemented and you should get an idea of how to test.

[yet another edit]

Blah ... back to IPC101 - according to the man pages:

Quote:
A semaphore is an integer whose value is never allowed to fall below zero.
So if the mmap area used by the semaphores is initialized with, say, -1, then you can easily test if the sempahore had been init'ed.

Last edited by pinniped; 11-01-2008 at 02:28 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cat: /etc/udev/rules.d/70-persistent-net.rules: No such file or directory rcg1984 Linux From Scratch 2 09-17-2008 07:02 AM
persistent map of file overpencil Programming 1 02-02-2008 05:37 PM
persistent static routes, not so persistent Hewson Linux - Networking 4 04-27-2007 05:09 PM
Firefox and persistent storage cold Linux - Software 2 02-04-2006 09:11 PM
Persistent File Permission s on Shared Directories jackiemcghee Linux - General 3 07-15-2004 02:47 AM


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

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration