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

“p” suffix<br />

The “p” suffix versions will search the directories in your PATH environment variable<br />

to find the executable. You’ve probably noticed that all the examples have a<br />

hard-coded location for the executable — /bin/ls and /usr/bin/spellcheck.<br />

What about other executables? Unless you want to first find out the exact path for that<br />

particular program, it would be best to have the user tell your program all the places to<br />

search for executables. The standard PATH environment variable does just that.<br />

Here’s the one from a minimal system:<br />

PATH=/proc/boot:/bin<br />

This tells the shell that when I type a command, it should first look in the directory<br />

/proc/boot, and if it can’t find the command there, it should look in the binaries<br />

directory /bin part. PATH is a colon-separated list of places to look for commands.<br />

You can add as many elements to the PATH as you want, but keep in mind that all<br />

pathname components will be searched (in order) for the executable.<br />

If you don’t know the path to the executable, then you can use the “p” variants. For<br />

example:<br />

// Using an explicit path:<br />

execl ("/bin/ls", "/bin/ls", "-l", "-t", "-r", NULL);<br />

// Search your PATH for the executable:<br />

execlp ("ls", "ls", "-l", "-t", "-r", NULL);<br />

If execl() can’t find ls in /bin, it returns an error. The execlp() function will search<br />

all the directories specified in the PATH for ls, and will return an error only if it can’t<br />

find ls in any of those directories. This is also great for multiplatform support — your<br />

program doesn’t have to be coded to know about the different CPU names, it just finds<br />

the executable.<br />

What if you do something like this?<br />

execlp ("/bin/ls", "ls", "-l", "-t", "-r", NULL);<br />

Does it search the environment? No. You told execlp() to use an explicit pathname,<br />

which overrides the normal PATH searching rule. If it doesn’t find ls in /bin that’s<br />

it, no other attempts are made (this is identical to the way execl() works in this case).<br />

Is it dangerous to mix an explicit path <strong>with</strong> a plain command name (e.g., the path<br />

argument /bin/ls, and the command name argument ls, instead of /bin/ls)? This<br />

is usually pretty safe, because:<br />

• a large number of programs ignore argv [0] anyway<br />

• those that do care usually call basename(), which strips off the directory portion of<br />

argv [0] and returns just the name.<br />

The only compelling reason for specifying the full pathname for the first argument is<br />

that the program can print out diagnostics including this first argument, which can<br />

instantly tell you where the program was invoked from. This may be important when<br />

the program can be found in multiple locations along the PATH.<br />

The spawn() functions all have an extra parameter; in all the above examples, I’ve<br />

always specified P_WAIT. There are four flags you can pass to spawn() to change its<br />

behavior:<br />

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