I am trying to solve readers-writers problem. The following functions is supposed to create named semaphores.
void createSems(void){
if((sem_counter = sem_open("counter", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("sem_open_counter");
if((sem_reader = sem_open("reader", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("sem_open_reader");
if((sem_writer = sem_open("writer", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("sem_open_writer");
if((sem_writer = sem_open("mutex1", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("mutex1");
if((sem_writer = sem_open("mutex2", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("mutex2");
if((sem_writer = sem_open("mutex3", O_CREAT, 0660, 1)) == SEM_FAILED)
printErrorAndKill("mutex3");
}
void printErrorAndKill (const char *functionName){
perror(functionName);
printf("%s: %s\n",functionName, strerror(errno));
exit(1);
}
Everything is working as expected on my MBP 10.7.3. But when I test it on school server I get the following output.
sem_open_reader: Permission denied
sem_open_reader: Illegal seek
The first semaphore is created successfully every time. I tried to google the error but with no succes or any connection to sem_open.
My question is am I doing something wrong when I am creating semaphores or the problem is somewhere else?
UPDATE
I did more testing and I eventually found out that it has nothing to do with illegal seek. I got rid of the line “perror(functionName);” just to found out the problem is just with permissions. Some names were reserved on our school OS so I just had to come with some more relevant names.
The “Illegal seek” message is probably caused by perror(). The man page for perror on my system says: “Note that errno is undefined after a successful library call: this call may well change this variable, even though it succeeds, for example because it internally used some other library function that failed.”