operating system - Difference between binary semaphore and mutex -


is there difference between binary semaphore , mutex or same?

they not same thing. used different purposes!
while both types of semaphores have full/empty state , use same api, usage different.

mutual exclusion semaphores
mutual exclusion semaphores used protect shared resources (data structure, file, etc..).

a mutex semaphore "owned" task takes it. if task b attempts semgive mutex held task a, task b's call return error , fail.

mutexes use following sequence:

   - semtake   - critical section   - semgive

here simple example:

   thread                     thread b    take mutex      access data      ...                        take mutex  <== block      ...    give mutex                     access data  <== unblocks                                   ...                                 give mutex 

binary semaphore
binary semaphore address totally different question:

  • task b pended waiting happen (a sensor being tripped example).
  • sensor trips , interrupt service routine runs. needs notify task of trip.
  • task b should run , take appropriate actions sensor trip. go waiting.
    task                      task b    ...                         take binsemaphore   <== wait    noteworthy    give binsemaphore              <== unblocks 

note binary semaphore, ok b take semaphore , give it.
again, binary semaphore not protecting resource access. act of giving , taking semaphore fundamentally decoupled.
typically makes little sense same task give , take on same binary semaphore.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -