LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   difference mutex vs. semaphore? (https://www.linuxquestions.org/questions/programming-9/difference-mutex-vs-semaphore-383606/)

Thinking 11-16-2005 04:48 AM

difference mutex vs. semaphore?
 
hiho@ll

i'm reading a bit about semaphores and here
i read that a mutex is simply a binary semaphore

Q1: is it correct that a mutex is a binary semaphore?
Q2: if yes, does it mean the following example is correct?
process 1 creates a mutex (pthread_mutex_init())
P1 forks 2 processes using fork();
now we have P2 and P3 forked by P1
P1,P2 and P3 have the initalized mutex
all these three process are waiting for some data on a TCP server socket and write this to a shared memory (that's why i need a mutex or a semaphore)
so P2 gets the connection, locks the mutex, writes the data to the shared memory and unlocks the mutex

if P2 has locked the mutex using pthread_mutex_lock();
what happens if P3 does pthread_mutex_lock(); on the same mutex included by the fork of P1? will P3 wait until it's unlocked OR does it get the lock because P3 and P2 are complete different processes?

Q3: if no, could anybody post the few lines used to lock and unlock a semaphore?
i have this, but i have absolutly no idea if this is correct:
Code:

int SemaphoreMutex::lock(){
  struct sembuf ops[2]={
  0,0,0,  // wait for the semaphore 0 to be 0
  0,1,SEM_UNDO  // set the semaphore to 1
  };
  if(this->semid!=-1)
  return semop(this->semid,ops,2);
 }

 int SemaphoreMutex::unlock(){
  struct sembuf ops[2]={
  0,1,0,  // wait for the semaphore 0 to be 1
  0,0,SEM_UNDO  // set the semaphore to 0
  };
  if(this->semid!=-1)
  return semop(this->semid,ops,2);
 }

Q3: what exactly does semop? does it only set the semaphore! or does it wait for a condition and then set the semaphore? how does semop work?

btw: the code above is from my own SemaphoreMutex class i wrote until i read that mutex is a binary semaphore, that's why i post the whole stuff here

thx@ll

jtshaw 11-16-2005 05:26 AM

Re: difference mutex vs. semaphore?
 
Quote:

Originally posted by Thinking
hiho@ll

i'm reading a bit about semaphores and here
i read that a mutex is simply a binary semaphore

Q1: is it correct that a mutex is a binary semaphore?

Absolutely. A Mutex is a Semaphore with a value of 1.

Quote:

Originally posted by Thinking
Q2: if yes, does it mean the following example is correct?
process 1 creates a mutex (pthread_mutex_init())
P1 forks 2 processes using fork();
now we have P2 and P3 forked by P1
P1,P2 and P3 have the initalized mutex
all these three process are waiting for some data on a TCP server socket and write this to a shared memory (that's why i need a mutex or a semaphore)
so P2 gets the connection, locks the mutex, writes the data to the shared memory and unlocks the mutex

if P2 has locked the mutex using pthread_mutex_lock();
what happens if P3 does pthread_mutex_lock(); on the same mutex included by the fork of P1? will P3 wait until it's unlocked OR does it get the lock because P3 and P2 are complete different processes?

In a correctly coded environment if P2 has the lock and P3 tries to lock it will get blocked until P2 does an unlock on the mutex. If your using pthread_mutex's it makes more sense to use multiple threads then to spawn multiple processes. If you want to use multiple processes you really need to use a semaphore that is only allowed to be 1 or 0. Check out the man page for semop, semctl, and semget for more information on how to control them. If you have a general interest for how interprocess communication (aka IPC) works then check out the man page for ipc.


Quote:

Originally posted by Thinking
Q3: if no, could anybody post the few lines used to lock and unlock a semaphore?
i have this, but i have absolutly no idea if this is correct:
Code:

int SemaphoreMutex::lock(){
  struct sembuf ops[2]={
  0,0,0,  // wait for the semaphore 0 to be 0
  0,1,SEM_UNDO  // set the semaphore to 1
  };
  if(this->semid!=-1)
  return semop(this->semid,ops,2);
 }

 int SemaphoreMutex::unlock(){
  struct sembuf ops[2]={
  0,1,0,  // wait for the semaphore 0 to be 1
  0,0,SEM_UNDO  // set the semaphore to 0
  };
  if(this->semid!=-1)
  return semop(this->semid,ops,2);
 }

Q3: what exactly does semop? does it only set the semaphore! or does it wait for a condition and then set the semaphore? how does semop work?

btw: the code above is from my own SemaphoreMutex class i wrote until i read that mutex is a binary semaphore, that's why i post the whole stuff here

thx@ll

I don't have time to actually write up a snippet of code for you.. so I'll leave this to somebody else.. or if I have time later I'll come back to it.


All times are GMT -5. The time now is 03:30 PM.