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.

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

Priorities<br />

Semaphores<br />

What if the bathroom is currently locked and a number of people are waiting to use it?<br />

Obviously, all the people are sitting around outside, waiting for whoever is in the<br />

bathroom to get out. The real question is, “What happens when the door unlocks?<br />

Who gets to go next?”<br />

You’d figure that it would be “fair” to allow whoever is waiting the longest to go next.<br />

Or it might be “fair” to let whoever is the oldest go next. Or tallest. Or most important.<br />

There are any number of ways to determine what’s “fair.”<br />

We solve this <strong>with</strong> threads via two factors: priority and length of wait.<br />

Suppose two people show up at the (locked) bathroom door at the same time. One of<br />

them has a pressing deadline (they’re already late for a meeting) whereas the other<br />

doesn’t. Wouldn’t it make sense to allow the person <strong>with</strong> the pressing deadline to go<br />

next? Well, of course it would. The only question is how you decide who’s more<br />

“important.” This can be done by assigning a priority (let’s just use a number like<br />

<strong>Neutrino</strong> does — one is the lowest usable priority, and 255 is the highest as of this<br />

version). The people in the house that have pressing deadlines would be given a higher<br />

priority, and those that don’t would be given a lower priority.<br />

Same thing <strong>with</strong> threads. A thread inherits its scheduling algorithm from its parent<br />

thread, but can call pthread_setschedparam() to change its scheduling policy and<br />

priority (if it has the authority to do so).<br />

If a number of threads are waiting, and the mutex becomes unlocked, we would give<br />

the mutex to the waiting thread <strong>with</strong> the highest priority. Suppose, however, that both<br />

people have the same priority. Now what do you do? Well, in that case, it would be<br />

“fair” to allow the person who’s been waiting the longest to go next. This is not only<br />

“fair,” but it’s also what the <strong>Neutrino</strong> kernel does. In the case of a bunch of threads<br />

waiting, we go primarily by priority, and secondarily by length of wait.<br />

The mutex is certainly not the only synchronization object that we’ll encounter. Let’s<br />

look at some others.<br />

Let’s move from the bathroom into the kitchen, since that’s a socially acceptable<br />

location to have more than one person at the same time. In the kitchen, you may not<br />

want to have everyone in there at once. In fact, you probably want to limit the number<br />

of people you can have in the kitchen (too many cooks, and all that).<br />

Let’s say you don’t ever want to have more than two people in there simultaneously.<br />

Could you do it <strong>with</strong> a mutex? Not as we’ve defined it. Why not? This is actually a<br />

very interesting problem for our analogy. Let’s break it down into a few steps.<br />

A semaphore <strong>with</strong> a count of 1<br />

The bathroom can have one of two situations, <strong>with</strong> two states that go hand-in-hand<br />

<strong>with</strong> each other:<br />

April 30, 2009 Chapter 1 • Processes and Threads 17

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

Saved successfully!

Ooh no, something went wrong!