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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

obvious method of cleaning up these files, except perhaps to have a “grim reaper” task<br />

that runs around seeing if these things are still valid.<br />

There’s another related problem. Since the process that created the file can die <strong>with</strong>out<br />

removing the file, there’s no way of knowing whether or not the process is still alive<br />

until you try to send a message to it. Worse yet, the ND/PID/CHID specified in the file<br />

may be so stale that it would have been reused by another program! The message that<br />

you send to that program will at best be rejected, and at worst may cause damage. So<br />

that approach is out.<br />

The second approach, where we use global variables to advertise the ND/PID/CHID<br />

values, is not a general solution, as it relies on the client’s being able to access the<br />

global variables. And since this requires shared memory, it certainly won’t work<br />

across a network! This generally gets used in either tiny test case programs or in very<br />

special cases, but always in the context of a multithreaded program. Effectively, all<br />

that happens is that one thread in the program is the client, and another thread is the<br />

server. The server thread creates the channel and then places the channel ID into a<br />

global variable (the node ID and process ID are the same for all threads in the process,<br />

so they don’t need to be advertised.) The client thread then picks up the global channel<br />

ID and performs the ConnectAttach() to it.<br />

The third approach, where we use the name_attach() and name_detach() functions,<br />

works well for simple client/server situations.<br />

The last approach, where the server becomes a resource manager, is definitely the<br />

cleanest and is the recommended general-purpose solution. The mechanics of “how”<br />

will become clear in the Resource Managers chapter, but for now, all you need to<br />

know is that the server registers a particular pathname as its “domain of authority,” and<br />

a client performs a simple open() of that pathname.<br />

What about priorities?<br />

I can’t emphasize this enough:<br />

POSIX file descriptors are implemented using connection IDs; that is, a file descriptor<br />

is a connection ID! The beauty of this scheme is that since the file descriptor that’s<br />

returned from the open() is the connection ID, no further work is required on the<br />

client’s end to be able to use that particular connection. For example, when the client<br />

calls read() later, passing it the file descriptor, this translates <strong>with</strong> very little overhead<br />

into a MsgSend() function.<br />

What if a low-priority process and a high-priority process send a message to a server<br />

at the same time?<br />

April 30, 2009 Chapter 2 • Message Passing 103

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

Saved successfully!

Ooh no, something went wrong!