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. The resource manager library<br />

• POSIX library helper functions<br />

While you certainly could write resource managers “from scratch” (as was done in the<br />

<strong>QNX</strong> 4 world), that’s far more hassle than it’s worth.<br />

Just to show you the utility of the library approach, here’s the source for a<br />

single-threaded version of “/dev/null”:<br />

/*<br />

* resmgr1.c<br />

*<br />

* /dev/null using the resource manager library<br />

*/<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

int<br />

main (int argc, char **argv)<br />

{<br />

dispatch_t<br />

*dpp;<br />

resmgr_attr_t<br />

resmgr_attr;<br />

dispatch_context_t *ctp;<br />

resmgr_connect_funcs_t connect_func;<br />

resmgr_io_funcs_t io_func;<br />

iofunc_attr_t<br />

attr;<br />

// create the dispatch structure<br />

if ((dpp = dispatch_create ()) == NULL) {<br />

perror ("Unable to dispatch_create\n");<br />

exit (EXIT_FAILURE);<br />

}<br />

// initialize the various data structures<br />

memset (&resmgr_attr, 0, sizeof (resmgr_attr));<br />

resmgr_attr.nparts_max = 1;<br />

resmgr_attr.msg_max_size = 2048;<br />

// bind default functions into the outcall tables<br />

iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_func,<br />

_RESMGR_IO_NFUNCS, &io_func);<br />

iofunc_attr_init (&attr, S_IFNAM | 0666, 0, 0);<br />

// establish a name in the pathname space<br />

if (resmgr_attach (dpp, &resmgr_attr, "/dev/mynull",<br />

_FTYPE_ANY, 0, &connect_func, &io_func,<br />

&attr) == -1) {<br />

perror ("Unable to resmgr_attach\n");<br />

exit (EXIT_FAILURE);<br />

}<br />

ctp = dispatch_context_alloc (dpp);<br />

// wait here forever, handling messages<br />

while (1) {<br />

if ((ctp = dispatch_block (ctp)) == NULL) {<br />

perror ("Unable to dispatch_block\n");<br />

exit (EXIT_FAILURE);<br />

}<br />

April 30, 2009 Chapter 5 • Resource Managers 201

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

Saved successfully!

Ooh no, something went wrong!