3

i am making binary semaphore shared between multiple processes(not threads , Process Only) using POSIX in C language. if i create binary semaphore using mutex,

 typedef struct BIN_SEMA
  { 
     pthread_cond_t  cv;    /* cond. variable 
                                   - used to block threads */
     pthread_mutex_t mutex; /* mutex variable
                             - used to prevents concurrent
                                    access to the variable "flag" */
     int     flag;          /* Semaphore state: 
                                    0 = down, 1 = up */
   } bin_sema;

i will be able to use it amongst the threads only , but i want to share between processes. so my question is, how to make binary semaphore using posix counting semaphores ?

KrunalParmar
  • 1,062
  • 2
  • 18
  • 31
  • Why would you *ever* make a binary semaphore like that? The `mutex` *itself* is already a binary semaphore. Also, if you're synchronizing separate *processes*, why don't you use the appropriate mechanisms for that: `sem_open()`, `sem_init()`, `sem_wait()`, `sem_post()`, `sem_unlink()`? – EOF Aug 22 '15 at 13:38
  • but your suggested methods are for counting semaphore. if i am taking sem_t *sem_id; then it is creating a counting semaphore. and i need binary semaphore. so asked this question ! – KrunalParmar Aug 22 '15 at 13:52
  • Gee, I wonder what kind of semaphore I'd get if I initiated a counting-semaphore with an initial value of 1. – EOF Aug 22 '15 at 13:53
  • It is answered by many people in stackoverflow. But , even if i am initiating with 1 , i am not getting binary semaphore.. Its still counting semaphore ! – KrunalParmar Aug 22 '15 at 14:16

1 Answers1

2

It's not clear what you mean by binary semaphore. If you mean something that can have two states, then a mutex or a semaphore initialized with one would be functionally equivalent.

If you want to share the semaphore across processes, you can use a named semaphore...

sem_t* sem = sem_open("/APP/SEMAPHORE", O_CREAT, (S_IRUSR | S_IWUSR), 1);

sem_wait(sem);

// do stuff

sem_post(sem);

// do more stuff

sem_unlink("/APP/SEMAPHORE");

To enforce a mutex across a processes, you can use a file...

const char* lock_file = ".lock";

const int fd_lock = open(lock_file, O_CREAT);

flock(fd_lock, LOCK_EX);

// do stuff

flock(fd_lock, LOCK_UN);

// do more stuff

close(fd_lock);    
unlink(lock_file);
Jason
  • 3,777
  • 14
  • 27
  • By binary semaphore I mean that if we do more than one post then the value of the semaphore should remain one as this is theoretical concept of strict binary semaphores. But if I initialize the value of semaphore to one and call post operation on it,then it's value increases from one which leads to the execution of more than one wait operation same as counting semaphore.Which should not be the case with binary semaphore. – KrunalParmar Aug 22 '15 at 16:10
  • Why would you post to an unlocked semaphore? What you're describing is functionally a mutex. – Jason Aug 22 '15 at 16:27
  • but , mutex can only be used with in one process and multiple threads. But i want synchronization between processes. hence i am looking for strict binary semaphore. – KrunalParmar Aug 22 '15 at 16:27
  • You can do one of two things to get mutex semantics. You can restrict posting to the semaphore when unlocked, or you can use a file lock. I've added code for the file lock above. – Jason Aug 22 '15 at 16:44