QNX消息傳遞的例程

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 sender

struct _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 ourselves

    if ((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 運行通過

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章