Link to home
Start Free TrialLog in
Avatar of DarkNova
DarkNova

asked on

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!
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

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.

http://en.wikipedia.org/wiki/Mutual_exclusion
http://en.wikipedia.org/wiki/Semaphore_(programming)

Avatar of DarkNova
DarkNova

ASKER

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.
>> Can you give an example for a posix named semaphore?
A simple example can be found here: http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/APS33DTE_html/DOCU_010.HTM
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

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.
ASKER CERTIFIED SOLUTION
Avatar of DarkNova
DarkNova

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial