QNX 例程,使用MsgReceive和MsgReplay自收自發消息。使用定時器週期發送消息。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <sys/siginfo.h>
#include <sys/neutrino.h>unsigned int flag=0;
int chid; // channel ID (global)
int rcvid; // process ID of the senderstruct _pulse pulse;
#define CODE_TIMER 1 // pulse from timer
void HelloTask(void)
{
timer_t timerid; // timer ID for timer
struct sigevent event; // event to deliver
struct itimerspec timer; // the timer data structure
int coid; // connection back to ourselves
// create a connection back to ourselvesif ((chid = ChannelCreate (0)) == -1) {
fprintf (stderr, "%s: couldn't create channel!\n", "Hello");
perror (NULL);
exit (EXIT_FAILURE);
}
coid = ConnectAttach (0, 0, chid, 0, 0);
//SIGEV_SIGNAL_INIT(&event,SIGUSR1);
SIGEV_PULSE_INIT (&event, coid, SIGEV_PULSE_PRIO_INHERIT, CODE_TIMER, 0);
if (timer_create (CLOCK_REALTIME, &event, &timerid) == -1) {
fprintf (stderr, "%s: couldn't create a timer, errno %d\n", "Hello", errno);
perror (NULL);
exit (EXIT_FAILURE);
}// setup the timer (1s delay, 1s reload)
timer.it_value.tv_sec = 1;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_nsec = 0;
// and start it!
timer_settime (timerid, 0, &timer, NULL);
for(;;)
{
rcvid = MsgReceive (chid, &pulse, sizeof (pulse), NULL);
if (rcvid == 0)
{
time_t now;
time (&now);
printf ("Got a Pulse at %s", ctime (&now));
MsgReply (rcvid, EOK, &pulse, sizeof (pulse));
}}
}int main(int argc, char *argv[]) {
pthread_t tid;
if ( pthread_create(&tid, NULL, HelloTask, NULL) < 0)
{
printf("taskSpawn HelloTask failed!\n");
}pthread_join(tid,NULL);
return EXIT_SUCCESS;
}
以上代碼在x86平臺使用SDP6.5 運行通過