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.

Using message passing<br />

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

Reading and writing data<br />

Messages are always delivered in priority order.<br />

If two processes send a message “simultaneously,” the entire message from the<br />

higher-priority process is delivered to the server first.<br />

If both processes are at the same priority, then the messages will be delivered in time<br />

order (since there’s no such thing as absolutely simultaneous on a single-processor<br />

machine — even on an SMP box there will be some ordering as the CPUs arbitrate<br />

kernel access among themselves).<br />

We’ll come back to some of the other subtleties introduced by this question when we<br />

look at priority inversions later in this chapter.<br />

So far you’ve seen the basic message-passing primitives. As I mentioned earlier, these<br />

are all that you need. However, there are a few extra functions that make life much<br />

easier.<br />

Let’s consider an example using a client and server where we might need other<br />

functions.<br />

The client issues a MsgSend() to transfer some data to the server. After the client<br />

issues the MsgSend() it blocks; it’s now waiting for the server to reply.<br />

An interesting thing happens on the server side. The server has called MsgReceive() to<br />

receive the message from the client. Depending on the design that you choose for your<br />

messages, the server may or may not know how big the client’s message is. Why on<br />

earth would the server not know how big the message is? Consider the filesystem<br />

example that we’ve been using. Suppose the client does:<br />

write (fd, buf, 16);<br />

This works as expected if the server does a MsgReceive() and specifies a buffer size of,<br />

say, 1024 bytes. Since our client sent only a tiny message (28 bytes), we have no<br />

problems.<br />

However, what if the client sends something bigger than 1024 bytes, say 1 megabyte?<br />

write (fd, buf, 1000000);<br />

How is the server going to gracefully handle this? We could, arbitrarily, say that the<br />

client isn’t allowed to write more than n bytes. Then, in the client-side C library code<br />

for write(), we could look at this requirement and split up the write request into several<br />

requests of n bytes each. This is awkward.<br />

The other problem <strong>with</strong> this example would be, “How big should n be?”<br />

You can see that this approach has major disadvantages:<br />

• All functions that use message transfer <strong>with</strong> a limited size will have to be modified<br />

in the C library so that the function packetizes the requests. This in itself can be a<br />

fair amount of work. Also, it can have unexpected side effects for multi-threaded<br />

104 Chapter 2 • Message Passing April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!