cposixsemaphorereaderwriterlock

Reader-Writer using semaphores and shared memory in C


I'm trying to make a simple reader/writer program using POSIX named semaphores, its working, but on some systems, it halts immediately on the first semaphore and thats it ... I'm really desperate by now. Can anyone help please? Its working fine on my system, so i can't track the problem by ltrace. (sorry for the comments, I'm from czech republic)

https://www.dropbox.com/s/hfcp44u2r0jd7fy/readerWriter.c


Solution

  • POSIX semaphores are not well suited for application code since they are interruptible. Basically any sort of IO to your processes will mess up your signalling. Please have a look at this post.

    So you'd have to be really careful to interpret all error returns from the sem_ functions properly. In the code that you posted there is no such thing.

    If your implementation of POSIX supports them, just use rwlocks, they are made for this, are much higher level and don't encounter that difficulty.