Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > POSIX semaphores in Darwin

POSIX semaphores in Darwin
Thread Tools
Forum Regular
Join Date: Sep 2000
Location: Chico, California
Status: Offline
Reply With Quote
Dec 6, 2004, 02:14 AM
 
I'm trying to do this programming project on my PowerBook but it's becoming a real headache because Darwin doesn't have all POSIX semaphore functions implemented, namely sem_init() and sem_destroy() which our teacher told to use in this assignment.

Instead of just doing the assignment on our school's servers, I decided to try coding this project on Mac OS X using alternative functions, sem_open() and sem_close(). This is a headache because sem_open() takes more arguments than sem_init(), and I think I'm having trouble figuring out how to use it based on man pages. I also can't find any good sem_open() examples on the web.

So, I was wondering if anyone could possibly point me in the right direction for what arguments I need to use in sem_open(). Currently, I have:

Code:
#include <iostream.h> #include <semaphore.h> #include <errno.h> /* found this example on the web somewhere */ #define FILE_MODE (S_IRWXU | S_IRWXG | S_IRWXO ) ... sem_t *print_mutex; print_mutex = sem_open("print_mutex", O_CREAT, FILE_MODE, 0); ... /* sem_close() returns 0 if everything went ok */ if(sem_close(print_mutex) != 0) perror("sem_close");
This will compile with no errors, however, every time I run it, I get:

sem_close: Bad file descriptor

And if I insert a perror("sem_open") after I call sem_open(), It says:

sem_open: Permission denied

I'm not sure what else to try in place of the arguments of sem_open(). Everything looks fine to me, but apparently there's a permission problem. I just want to get sem_open() working as a simple replacement for sem_init(), I don't need anything fancy. I'm about ready to give up writing this program on OS X and do it on my school's server. Can anyone shed some light on sem_open() and sem_close()?
     
Professional Poster
Join Date: Sep 1999
Location: Ottawa, ON, Canada
Status: Offline
Reply With Quote
Dec 6, 2004, 10:38 AM
 
MacOS X does not support unnamed semaphores.

Have a look here:
http://lists.cistron.nl/pipermail/fr...er/007620.html
     
Forum Regular
Join Date: Dec 2004
Status: Offline
Reply With Quote
Dec 8, 2004, 02:55 AM
 
Here's what I found:

I compiled code that is very similar to yours, and the first time I run it, it runs without errors, but subsequent runs return the same error you are getting.

If I run the same code as root, I get no errors, so it appears that the system lets user processes create semaphores but not delete them. I haven't investigated deeply enough to be able to tell you why. I have never used named semaphores before on any system, let alone os x. I think you'd be wise to run sem_unlink() after closing your semaphores, too, although doing so didn't affect my tests any.

Good luck.

If you write sem_init() and sem_destroy() functions that wrap the named semaphore sem_open(), sem_close(), sem_unlink() functions, you should at least be able to test your code as root. It should then compile and run fine on linux during your final testing. You'll have to do something about looking up semaphores by comparing pointer values if your test app must use multiple semaphores. A simple array of (sem_t *)'s that you can iterate over doing pointer comparisons should be sufficient.

--sam
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2