Today I spent a couple of hours getting semaphores to work.

I was opening a semaphore like this:

{% highlight c %} if (SEM_FAILED == (CACHE_LOCK = sem_open("/cache_lock", O_CREAT, 0660, 1))) { printf(“error opening semphore: %i\n”, errno); return -1; /* error creating semaphore */ } {% endhighlight %}

This runs fine on my Mac, but fails on an Ubuntu machine. On failure it sets errno to EACCESS (13). Whatever you might expect, this fact alone did not allow me to figure out the cause of the problem.

It turns out the name I’d chosen isn’t allowed. I couldn’t find the rules that expressly state this, or even hinted that it might be the case, but I found when I changed the name from ‘/cache_lock’ to ‘/lock’ on a whim, it worked. Apparantly, underscores are not cool with Linux kernel developers.

During my Googling I found the following question which put me on the right track.