How to lock a critical section of code shared between multiple processes? Use a semaphore?

I'm writing 2 different C++ programs that will run simultaneous and both need to access the same files, but cannot do so simultaneously. I need to have code that locks this section of code in both programs so that once one program starts processing the files, the other program will not be able to start until the other program is finished. I am running Linux kernel 2.6.9. Looking around, it seems like an option may be Posix named semaphores (like sem_open). Can someone confirm if they will work and/or is there a better way? I don't really need a full-blown semaphore but I didn't see any way to do a mutex that works between different processes. Also, right now my app isn't multi-threaded, but I'd like to have a solution that continues working if I multi-thread my app. Does anyone have any examples? Thanks!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
When you say you don't need a full blown semaphore, you should realize that mutexes are really just binary semaphores with slightly different semantics, so even when using a mutex you are normally manipulating a semaphore. I can confirm that a posix named semaphore should provide you with the ability to fulfill your stated requirements.

DarkNovaAuthor Commented:
I realize that mutexes are generally binary semaphores. Can you give an example for a posix named semaphore? BTW, your 2nd wiki link is missing the closing parenthesis in the URL.
evilrixSenior Software Engineer (Avast)Commented:
>> Can you give an example for a posix named semaphore?
A simple example can be found here:
See below.

>> your 2nd wiki link is missing the closing parenthesis in the URL.
It's there but for some reason it's not being rendered as a link... look a it again.

// The following example creates a semaphore named /tmp/mysem with a value of 3: 
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <sys/stat.h>
sem_t   *mysemp;
int     oflag = O_CREAT;
mode_t  mode = 0644;
const char semname[] = "/tmp/mysem"
unsigned int value = 3;
int     sts;
mysemp = sem_open(semname, oflag, mode, value);
if (mysemp == (void *)-1) {
  perror(sem_open() failed ");

Open in new window

DarkNovaAuthor Commented:
It appears that a POSIX semaphore is not automatically decremented if a program crashes while in the critical section -- a big disadvantage to doing it this way. How can I work around this? Thanks.
DarkNovaAuthor Commented:
Like I posted yesterday, it looks like POSIX semaphores are not automatically decremented if a program crashes while in the critical section, which is a big problem as this case would either cause a manual intervention or some extra complexity while the program has to guess if the program is just taking a long time or if it has crashed.

Instead, I found a better solution: Use fcntl to create a file lock:

The advantage to doing it this way is that if the process that has the lock crashes, the OS automatically releases the lock it has and the other process is able to proceed. The disadvantage is that this can only do a form of mutex, not a full-blown semaphore as it just acts in a binary fashion. For mine, and for other's, applications this is fine however.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.