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()?