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

On the server side, (let’s say it’s the filesystem, fs-qnx4), we have a number of 4 KB<br />

cache blocks, and we’d like to efficiently receive the message directly into the cache<br />

blocks. Ideally, we’d like to write some code like this:<br />

// set up the IOV structure to receive into:<br />

SETIOV (iov + 0, &header, sizeof (header.io_write));<br />

SETIOV (iov + 1, &cache_buffer [37], 4096);<br />

SETIOV (iov + 2, &cache_buffer [16], 4096);<br />

SETIOV (iov + 3, &cache_buffer [22], 4096);<br />

rcvid = MsgReceivev (chid, iov, 4, NULL);<br />

This code does pretty much what you’d expect: it sets up a 4-part IOV structure, sets<br />

the first part of the structure to point to the header, and the next three parts to point to<br />

cache blocks 37, 16, and 22. (These numbers represent cache blocks that just<br />

happened to be available at that particular time.) Here’s a graphical representation:<br />

header (12)<br />

header (12)<br />

cache_buffer [37]<br />

data chunk<br />

(12000)<br />

header and<br />

data chunk<br />

cache_buffer [16]<br />

cache_buffer [22]<br />

Client’s address space<br />

Kernel copying data<br />

Driver’s address space<br />

Converting contiguous data to separate buffers.<br />

Then the MsgReceivev() function is called, indicating that we’ll receive a message<br />

from the specified channel (the chid parameter) and that we’re supplying a 4-part IOV<br />

structure. This also shows the IOV structure itself.<br />

(Apart from its IOV functionality, MsgReceivev() operates just like MsgReceive().)<br />

Oops! We made the same mistake as we did before, when we introduced the<br />

MsgReceive() function.<br />

How do we know what kind of message we’re receiving, and how much data is<br />

associated <strong>with</strong> it, until we actually receive the message?<br />

We can solve this the same way as before:<br />

rcvid = MsgReceive (chid, &header, sizeof (header), NULL);<br />

switch (header.message_type) {<br />

...<br />

case _IO_WRITE:<br />

number_of_bytes = header.io_write.nbytes;<br />

April 30, 2009 Chapter 2 • Message Passing 111

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

Saved successfully!

Ooh no, something went wrong!