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. Threads and processes<br />

they’re in the house, and when they’re not. A thread has a very good idea — if it’s<br />

accessing memory <strong>with</strong>in the process, it can live. If it steps out of the bounds of the<br />

process’s address space, it gets killed. This means that two threads, running in<br />

different processes, are effectively isolated from each other.<br />

Memory protection<br />

barrier<br />

Process 1<br />

Process 2<br />

Process 1's<br />

address space<br />

Process 2's<br />

address space<br />

Memory protection.<br />

Starting a process<br />

The process address space is maintained and enforced by <strong>Neutrino</strong>’s process manager<br />

module. When a process is started, the process manager allocates some memory to it<br />

and starts a thread running. The memory is marked as being owned by that process.<br />

This means that if there are multiple threads in that process, and the kernel needs to<br />

context-switch between them, it’s a very efficient operation — we don’t have to<br />

change the address space, just which thread is running. If, however, we have to change<br />

to another thread in another process, then the process manager gets involved and<br />

causes an address space switch as well. Don’t worry — while there’s a bit more<br />

overhead in this additional step, under <strong>Neutrino</strong> this is still very fast.<br />

Let’s now turn our attention to the function calls available to deal <strong>with</strong> threads and<br />

processes. Any thread can start a process; the only restrictions imposed are those that<br />

stem from basic security (file access, privilege restrictions, etc.). In all probability,<br />

you’ve already started other processes; either from the system startup script, the shell,<br />

or by having a program start another program on your behalf.<br />

Starting a process from the command line<br />

For example, from the shell you can type:<br />

$ program1<br />

This instructs the shell to start a program called program1 and to wait for it to finish.<br />

Or, you could type:<br />

$ program2 &<br />

This instructs the shell to start program2 <strong>with</strong>out waiting for it to finish. We say that<br />

program2 is running “in the background.”<br />

April 30, 2009 Chapter 1 • Processes and Threads 27

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

Saved successfully!

Ooh no, something went wrong!