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.

Using message passing<br />

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

Finding the server’s ND/PID/CHID<br />

client’s MsgSend() function to return -1, and will cause the client to have errno set to<br />

whatever was passed as the second argument to MsgError().<br />

On the other hand, MsgReply() could transfer data (as indicated by the third and fourth<br />

arguments), and will cause the client’s MsgSend() function to return whatever was<br />

passed as the second argument to MsgReply(). MsgReply() has no effect on the client’s<br />

errno.<br />

Generally, if you’re returning only a pass/fail indication (and no data), you’d use<br />

MsgError(), whereas if you’re returning data, you’d use MsgReply(). Traditionally,<br />

when you do return data, the second argument to MsgReply() will be a positive integer<br />

indicating the number of bytes being returned.<br />

You’ve noticed that in the ConnectAttach() function, we require a Node Descriptor<br />

(ND), a process ID (PID), and a channel ID (CHID) in order to be able to attach to a<br />

server. So far we haven’t talked about how the client finds this ND/PID/CHID<br />

information.<br />

If one process creates the other, then it’s easy — the process creation call returns <strong>with</strong><br />

the process ID of the newly created process. Either the creating process can pass its<br />

own PID and CHID on the command line to the newly created process or the newly<br />

created process can issue the getppid() function call to get the PID of its parent and<br />

assume a “well-known” CHID.<br />

What if we have two perfect strangers? This would be the case if, for example, a third<br />

party created a server and an application that you wrote wanted to talk to that server.<br />

The real issue is, “How does a server advertise its location?”<br />

There are many ways of doing this; we’ll look at four of them, in increasing order of<br />

programming “elegance”:<br />

1 Open a well-known filename and store the ND/PID/CHID there. This is the<br />

traditional approach taken by UNIX-style servers, where they open a file (for<br />

example, /etc/httpd.pid), write their process ID there as an ASCII string,<br />

and expect that clients will open the file and fetch the process ID.<br />

2 Use global variables to advertise the ND/PID/CHID information. This is<br />

typically used in multi-threaded servers that need to send themselves messages,<br />

and is, by its nature, a very limited case.<br />

3 Use the name-location functions (name_attach() and name_detach(), and then<br />

the name_open() and name_close() functions on the client side).<br />

4 Take over a portion of the pathname space and become a resource manager.<br />

We’ll talk about this when we look at resource managers in the Resource<br />

Managers chapter.<br />

The first approach is very simple, but can suffer from “pathname pollution,” where the<br />

/etc directory has all kinds of *.pid files in it. Since files are persistent (meaning<br />

they survive after the creating process dies and the machine reboots), there’s no<br />

102 Chapter 2 • Message Passing April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!