Global Sources
EE Times-India
Stay in touch with EE Times India
 
EE Times-India > Embedded
 
 
Embedded  

Clearing the RTOS confusion

Posted: 23 Dec 2008     Print Version  Bookmark and Share

Keywords:mutex  semaphore  RTOS 

The question "What's the difference between a mutex and a semaphore?" is short and easily phrased. Answering it is far more difficult. Even experienced RTOS users have trouble distinguishing the proper uses of mutexes and semaphores. This is unfortunate and dangerous, as misuse of either RTOS primitive can lead to unintended errors in embedded systems, including life-threatening products.

Barr: If you think of a mutex as a key owned by the OS, it's easy to see that an individual mutex is analogous to a bathroom key owned by an urban coffee shop, for one bathroom.

Bathroom key analogy
Nearly all engineers seem to properly understand that a mutex is a binary flag used to protect a shared resource by ensuring mutual exclusion inside critical sections of code. But when asked to expand on how to use a "counting semaphore," most engineers express some flavour of the textbook opinion that these are used to protect several equivalent resources.

If you think of a mutex as a key owned by the OS, it's easy to see that an individual mutex is analogous to a bathroom key owned by an urban coffee shop, for one bathroom. If you ask to use the bathroom when the key is not available, you are asked to wait in a queue for the key. Similarly, a mutex helps multiple tasks serialise their access to shared global resources and gives waiting tasks a place to wait for their turn.

This simple resource-protection protocol doesn't scale to the case of two equivalent bathrooms. If a semaphore were a generalisation of a mutex able to protect two or more identical shared resources, then in our analogy it would be a basket containing two identical keys, which would work for either bathroom door.

A semaphore can't solve a multiple identical resource problem on its own. Visitors only know that they have a key, but not which bathroom is free. If you try to use a semaphore like this, you'll find you always need other state information—itself typically a shared resource protected via a separate mutex.

The correct use of a semaphore is for signalling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects. By contrast, tasks that use semaphores either signal or wait—not both. For example, Task 1 may contain code to post (i.e. signal or increment) a particular semaphore when the "power" button is pressed and Task 2, which wakes the display, pends on that same semaphore. In this scenario, one task is the producer of the event signal; the other the consumer.

- Michael Barr
President
Netrino LLC





Comment on "Clearing the RTOS confusion"
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top