answersLogoWhite

0


Best Answer

From wikipedia:

"A mutex is a binary semaphore, usually including extra features like ownership or priority inversion protection. The differences between mutexes and semaphores are operating system dependent. Mutexes are meant to be used for mutual exclusion only and binary semaphores are meant to be used for event notification and mutual exclusion."

They also have a good example as to the use of a semaphore:

"A thread named A needs information from two databases before it can proceed. Access to these databases is controlled by two separate threads B, C. These two threads have a message-processing loop; anybody needing to use one of the databases posts a message into the corresponding thread's message queue. Thread A initializes a semaphore S with init(S,-1). A then posts a data request, including a pointer to the semaphore S, to both B and C. Then A calls P(S), which blocks. The other two threads meanwhile take their time obtaining the information; when each thread finishes obtaining the information, it calls V(S) on the passed semaphore. Only after both threads have completed will the semaphore's value be positive and A be able to continue. A semaphore used in this way is called a 'counting semaphore.'"

Basically think of a semaphore as a lock that allows multiple threads to wait in line for the resource to be free. Usually they will block and the semaphore will wake them up when it is their turn.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between MUTEX and Semaphore?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How to write c programme of Sleeping-barber problem in opertating system?

#define CHAIRS 5 /* # chairs for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0; /* # of customers waiting for service */ semaphore barbers = 0; /* # of barbers waiting for customers */ semaphore mutex = 1; /* for mutual exclusion */ int waiting = 0; /* customer are waiting (not being cut) */ void barber(void) { while (TRUE) { down(&customers); /* go to sleep if # of customers is 0 */ down(&mutex); /* acquire access to "waiting' */ waiting = waiting - 1; /* decrement count of waiting customers */ up(&barbers); /* one barber is now ready to cut hair */ up(&mutex); /* release 'waiting' */ cut_hair(); /* cut hair (outside critical region */ } } void customer(void) { down(&mutex); /* enter critical region */ if (waiting < CHAIRS) { /* if there are no free chairs, leave */ waiting = waiting + 1; /* increment count of waiting customers */ up(&customers); /* wake up barber if necessary */ up(&mutex); /* release access to 'waiting' */ down(&barbers); /* go to sleep if # of free barbers is 0 */ get_haircut(); /* be seated and be served */ } else { up(&mutex); /* shop is full; do not wait */ } }



What is sem post and sem wait functions?

The sem_wait() function locks the semaphore referenced by sem by performing a semaphore lock operation on that semaphore. If the semaphore value is currently zero, then the calling thread will not return from the call to sem_wait()until it either locks the semaphore or the call is interrupted by a signal.Upon successful return, the state of the semaphore is locked and remains locked until the sem_post() function is executed and returns successfully. The sem_wait() function is interruptible by the delivery of a signal.The sem_post() function unlocks the semaphore referenced by sem by performing a semaphore unlock operation on that semaphore.If the semaphore value resulting from this operation is positive, then no threads were blocked waiting for the semaphore to become unlocked; the semaphore value is simply incremented.If the value of the semaphore resulting from this operation is zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait(). If the symbol _POSIX_PRIORITY_SCHEDULING is defined, the thread to be unblocked will be chosen in a manner appropriate to the scheduling policies and parameters in effect for the blocked threads. In the case of the schedulers SCHED_FIFO and SCHED_RR, the highest priority waiting thread will be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest will be unblocked. If the symbol _POSIX_PRIORITY_SCHEDULING is not defined, the choice of a thread to unblock is unspecified.The sem_post() interface is reentrant with respect to signals and may be invoked from a signal-catching function.


Which is a high level abstraction over semaphore?

Monitor


How do you write Multithreaded applications using C plus plus?

Use std::packaged_task (preferably) or std::thread to start a new thread. Use std::future (preferred), std::mutex or std::atomic to share information between threads.


What is the difference between technology and process?

what is the difference between license and patent

Related questions

How to write c programme of Sleeping-barber problem in opertating system?

#define CHAIRS 5 /* # chairs for waiting customers */ typedef int semaphore; /* use your imagination */ semaphore customers = 0; /* # of customers waiting for service */ semaphore barbers = 0; /* # of barbers waiting for customers */ semaphore mutex = 1; /* for mutual exclusion */ int waiting = 0; /* customer are waiting (not being cut) */ void barber(void) { while (TRUE) { down(&customers); /* go to sleep if # of customers is 0 */ down(&mutex); /* acquire access to "waiting' */ waiting = waiting - 1; /* decrement count of waiting customers */ up(&barbers); /* one barber is now ready to cut hair */ up(&mutex); /* release 'waiting' */ cut_hair(); /* cut hair (outside critical region */ } } void customer(void) { down(&mutex); /* enter critical region */ if (waiting < CHAIRS) { /* if there are no free chairs, leave */ waiting = waiting + 1; /* increment count of waiting customers */ up(&customers); /* wake up barber if necessary */ up(&mutex); /* release access to 'waiting' */ down(&barbers); /* go to sleep if # of free barbers is 0 */ get_haircut(); /* be seated and be served */ } else { up(&mutex); /* shop is full; do not wait */ } }



What is a semaphore in RTOS?

A semiphore is a means of protecting a resource/data shared between threads. It is a token based mechanism for controlling when a thread can have access to the resource/data.Usually a semiphore handle will be able to be received from the system by name/id.See Also Mutex. which is a simplification of the semiphore concept.


What is difference between counting and binary semaphore in os?

Binary semaphore is a semaphore with the integer value ranges over 0 and 1 whereas the counting semaphore's integer value ranges over unrestricted domain. Binary semaphores are easier to implement comparing with the counting semaphore. Binary semaphore allows only one thread to access the resource at a time. But counting semaphore allows N accesses at a time. The 2 operations that are defined for binary semaphores are take and release. The 2 operations that are defined for counting semaphores are wait and signal


What are the types of semaphores?

Three types of semaphores: 1.General/Counting semaphores: (can take any non-negative value) These are used when you might have multiple devices (like 3 printers or multiple memory buffers). 2.Binary semaphores: (can either be 0 or 1) These are used to gain exclusive access to a single resource (like the serial port, a non-reentrant library routine, or a hard disk drive). A counting semaphore that has a maximum value of 1 is equivalent to a binary semaphore (because the semaphore's value can only be 0 or 1). 3.Mutex semaphores: These are optimized for use in controlling mutually exclusive access to a resource. There are several implementations of this type of semaphore.


What is the role of mutex in IPC?

contct to damit for this.


What is priority inversion?

Priority inversion is a situation where in lower priority tasks will run blocking higher priority tasks waiting for resource (mutex). For ex: consider 3 tasks. A, B and C, A being highest priority task and C is lowest. Look at sequence of context swaps A goes for I/O . unlocks mutex. C was ready to run. So C starts running. locks mutex B is ready to run. Swaps out C and takes mutex. A is ready to run. but A is blocked as mutex is locked by B. but B will never relinqishes the mutex as its higher priority than C. The solution to priority inversion is Priority inheritance.


What is the use of 'lock' statement?

The lock prefix on the 8086/8088 prevents any other bus master from accessing the bus during this instruction, even if this instruction is a multiple access instruction. It assures atomicity and data consistency in a multi-computer environment, but only for a single instruction. It is generally used to manipulate a mutex or semaphore.


Can you explain for what is mutex used?

The uses of a mutex can indeed be described, however some of the more notable uses are that of cleaning and disinfecting area so that germs can not be spread from area to area.


How many semaphore flag methods are there?

there are 28 semaphore flag methods.


What is the difference between vocal and nonvocal communication?

Fairly self-explanatory really. Vocal communication is using the voice, non-vocal is communicating by other means such as semaphore, Morse code, email, fax etc !


When was Semaphore - album - created?

Semaphore - album - was created on 1998-03-16.


When did Semaphore railway line end?

Semaphore railway line ended in 1978.