QNX消息传递的例程

QNX 例程,使用MsgReceive和MsgReplay自收自发消息。使用定时器周期发送消息。

 

#include
#include
#include
#include
#include
#include
#include
#include

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 运行通过

 


 

你可能感兴趣的:(OS)