26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Process and thread fundamentals<br />

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG.<br />

A semaphore <strong>with</strong> a count greater than 1<br />

A semaphore as a mutex<br />

• the door is unlocked and nobody is in the room<br />

• the door is locked and one person is in the room<br />

No other combination is possible — the door can’t be locked <strong>with</strong> nobody in the room<br />

(how would we unlock it?), and the door can’t be unlocked <strong>with</strong> someone in the room<br />

(how would they ensure their privacy?). This is an example of a semaphore <strong>with</strong> a<br />

count of one — there can be at most only one person in that room, or one thread using<br />

the semaphore.<br />

The key here (pardon the pun) is the way we characterize the lock. In your typical<br />

bathroom lock, you can lock and unlock it only from the inside — there’s no<br />

outside-accessible key. Effectively, this means that ownership of the mutex is an<br />

atomic operation — there’s no chance that while you’re in the process of getting the<br />

mutex some other thread will get it, <strong>with</strong> the result that you both own the mutex. In<br />

our house analogy this is less apparent, because humans are just so much smarter than<br />

ones and zeros.<br />

What we need for the kitchen is a different type of lock.<br />

Suppose we installed the traditional key-based lock in the kitchen. The way this lock<br />

works is that if you have a key, you can unlock the door and go in. Anyone who uses<br />

this lock agrees that when they get inside, they will immediately lock the door from<br />

the inside so that anyone on the outside will always require a key.<br />

Well, now it becomes a simple matter to control how many people we want in the<br />

kitchen — hang two keys outside the door! The kitchen is always locked. When<br />

someone wants to go into the kitchen, they see if there’s a key hanging outside the<br />

door. If so, they take it <strong>with</strong> them, unlock the kitchen door, go inside, and use the key<br />

to lock the door.<br />

Since the person going into the kitchen must have the key <strong>with</strong> them when they’re in<br />

the kitchen, we’re directly controlling the number of people allowed into the kitchen at<br />

any given point by limiting the number of keys available on the hook outside the door.<br />

With threads, this is accomplished via a semaphore. A “plain” semaphore works just<br />

like a mutex — you either own the mutex, in which case you have access to the<br />

resource, or you don’t, in which case you don’t have access. The semaphore we just<br />

described <strong>with</strong> the kitchen is a counting semaphore — it keeps track of the count (by<br />

the number of keys available to the threads).<br />

We just asked the question “Could you do it <strong>with</strong> a mutex?” in relation to<br />

implementing a lock <strong>with</strong> a count, and the answer was no. How about the other way<br />

around? Could we use a semaphore as a mutex?<br />

Yes. In fact, in some operating systems, that’s exactly what they do — they don’t have<br />

mutexes, only semaphores! So why bother <strong>with</strong> mutexes at all?<br />

18 Chapter 1 • Processes and Threads April 30, 2009

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!