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 />

Process and thread fundamentals<br />

A process as a house<br />

The occupants as threads<br />

Single threaded<br />

Multi threaded<br />

Back to processes and threads<br />

Before we start talking about threads, processes, time slices, and all the other<br />

wonderful “scheduling concepts,” let’s establish an analogy.<br />

What I want to do first is illustrate how threads and processes work. The best way I<br />

can think of (short of digging into the design of a realtime system) is to imagine our<br />

threads and processes in some kind of situation.<br />

Let’s base our analogy for processes and threads using a regular, everyday object — a<br />

house.<br />

A house is really a container, <strong>with</strong> certain attributes (such as the amount of floor space,<br />

the number of bedrooms, and so on).<br />

If you look at it that way, the house really doesn’t actively do anything on its own —<br />

it’s a passive object. This is effectively what a process is. We’ll explore this shortly.<br />

The people living in the house are the active objects — they’re the ones using the<br />

various rooms, watching TV, cooking, taking showers, and so on. We’ll soon see that’s<br />

how threads behave.<br />

If you’ve ever lived on your own, then you know what this is like — you know that<br />

you can do anything you want in the house at any time, because there’s nobody else in<br />

the house. If you want to turn on the stereo, use the washroom, have dinner —<br />

whatever — you just go ahead and do it.<br />

Things change dramatically when you add another person into the house. Let’s say<br />

you get married, so now you have a spouse living there too. You can’t just march into<br />

the washroom at any given point; you need to check first to make sure your spouse<br />

isn’t in there!<br />

If you have two responsible adults living in a house, generally you can be reasonably<br />

lax about “security” — you know that the other adult will respect your space, won’t<br />

try to set the kitchen on fire (deliberately!), and so on.<br />

Now, throw a few kids into the mix and suddenly things get a lot more interesting.<br />

Just as a house occupies an area of real estate, a process occupies memory. And just as<br />

a house’s occupants are free to go into any room they want, a processes’ threads all<br />

have common access to that memory. If a thread allocates something (mom goes out<br />

and buys a game), all the other threads immediately have access to it (because it’s<br />

present in the common address space — it’s in the house). Likewise, if the process<br />

April 30, 2009 Chapter 1 • Processes and Threads 15

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

Saved successfully!

Ooh no, something went wrong!