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. time1.c<br />

}<br />

// set up the kind of event that we want to deliver -- a pulse<br />

SIGEV_PULSE_INIT (&event, coid, SIGEV_PULSE_PRIO_INHERIT, CODE_TIMER, 0);<br />

// create the timer, binding it to the event<br />

if (timer_create (CLOCK_REALTIME, &event, &timerid) == -1) {<br />

fprintf (stderr, "%s: couldn’t create a timer, errno %d\n",<br />

progname, errno);<br />

perror (NULL);<br />

exit (EXIT_FAILURE);<br />

}<br />

// setup the timer (1s delay, 1s reload)<br />

timer.it_value.tv_sec = 1;<br />

timer.it_value.tv_nsec = 0;<br />

timer.it_interval.tv_sec = 1;<br />

timer.it_interval.tv_nsec = 0;<br />

}<br />

// and start it!<br />

timer_settime (timerid, 0, &timer, NULL);<br />

/*<br />

* gotAPulse<br />

*<br />

* This routine is responsible for handling the fact that a timeout<br />

* has occurred. It runs through the list of clients to see<br />

* which client has timed-out, and replies to it <strong>with</strong> a timed-out<br />

* response.<br />

*/<br />

void<br />

gotAPulse (void)<br />

{<br />

ClientMessageT msg;<br />

int i;<br />

if (debug) {<br />

time_t now;<br />

}<br />

time (&now);<br />

printf ("Got a Pulse at %s", ctime (&now));<br />

// prepare a response message<br />

msg.messageType = MT_TIMEDOUT;<br />

// walk down list of clients<br />

for (i = 0; i < MAX_CLIENT; i++) {<br />

// is this entry in use?<br />

if (clients [i].in_use) {<br />

// is it about to time out?<br />

if (--clients [i].timeout == 0) {<br />

// send a reply<br />

MsgReply (clients [i].rcvid, EOK, &msg, sizeof (msg));<br />

}<br />

// entry no longer used<br />

clients [i].in_use = 0;<br />

April 30, 2009 Appendix: C • Sample Programs 319

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

Saved successfully!

Ooh no, something went wrong!