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.

Threads and processes<br />

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

If you want to adjust the priority of a program before you start it, you could use the<br />

nice command, just like in UNIX:<br />

$ nice program3<br />

This instructs the shell to start program3 at a reduced priority.<br />

Or does it?<br />

If you look at what really happens, we told the shell to run a program called nice at<br />

the regular priority. The nice command adjusted its own priority to be lower (this is<br />

where the name “nice” comes from), and then it ran program3 at that lower priority.<br />

Starting a process from <strong>with</strong>in a program<br />

You don’t usually care about the fact that the shell creates processes — this is a basic<br />

assumption about the shell. In some application designs, you’ll certainly be relying on<br />

shell scripts (batches of commands in a file) to do the work for you, but in other cases<br />

you’ll want to create the processes yourself.<br />

For example, in a large multi-process system, you may want to have one master<br />

program start all the other processes for your application based on some kind of<br />

configuration file. Another example would include starting up processes when certain<br />

operating conditions (events) have been detected.<br />

Let’s take a look at the functions that <strong>Neutrino</strong> provides for starting up other processes<br />

(or transforming into a different program):<br />

• system()<br />

• exec() family of functions<br />

• spawn() family of functions<br />

• fork()<br />

• vfork()<br />

Which function you use depends on two requirements: portability and functionality.<br />

As usual, there’s a trade-off between the two.<br />

The common thing that happens in all the calls that create a new process is the<br />

following. A thread in the original process calls one of the above functions.<br />

Eventually, the function gets the process manager to create an address space for a new<br />

process. Then, the kernel starts a thread in the new process. This thread executes a few<br />

instructions, and calls main(). (In the case of fork() and vfork(), of course, the new<br />

thread begins execution in the new process by returning from the fork() or vfork();<br />

we’ll see how to deal <strong>with</strong> this shortly.)<br />

Starting a process <strong>with</strong> the system() call<br />

The system() function is the simplest; it takes a command line, the same as you’d type<br />

it at a shell prompt, and executes it.<br />

28 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!