LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-16-2005, 04:48 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
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

Last edited by Thinking; 11-16-2005 at 04:50 AM.
 
Old 11-16-2005, 05:26 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 65
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.

Last edited by jtshaw; 11-16-2005 at 05:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphore tzzdvd Programming 1 05-24-2005 09:23 PM
Difference between condition variable and mutex linux_ujjwal Linux - General 4 05-10-2005 03:17 AM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
Whats the difference between a Mutex and a Semaphore? The_Nerd Programming 3 07-07-2004 09:28 AM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration