Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - mq_notify seems only notify once: (3 Items)
   
mq_notify seems only notify once  
#define QUEUE_NAME "/testqueue"

mqd_t queue;

int PulseHandler (message_context_t *ctp, int code, unsigned flags, void *handle)
{
    fprintf(stderr, "Plulse excuted ! \n");
    static char message[256];
    struct mq_attr attr;
   // mqd_t queue = mq_open (QUEUE_NAME, O_RDONLY , S_IRUSR | S_IWUSR, NULL);
    memset(message, 0 , 256);
    memset(&attr, 0, sizeof(attr));
    mq_getattr(queue,&attr);
    
    mq_receive( queue, message, attr.mq_msgsize, NULL );
    fprintf(stderr, "%s \n", message);
    fprintf(stderr, "Plulse excuted finished! \n");
    
    return 0;
}


int main(int argc, char *argv[]) {
    
    struct mq_attr attr;
    dispatch_context_t        *ctp;
    memset(&attr, 0, sizeof(attr));
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 255;
    queue = mq_open (QUEUE_NAME, O_CREAT |O_EXCL | O_RDONLY , S_IRUSR | S_IWUSR, &attr);
    struct      sigevent event;

    dispatch_t * dpp = dispatch_create();
   
    
    /* Attach a handler function to a pulse code */
    int TimerPulseCode = pulse_attach (dpp, MSG_FLAG_ALLOC_PULSE, 0, PulseHandler, NULL);

    /* Create a connection to a channel that our resource manager is receiving on */
    int SelfCoid = message_connect (dpp, MSG_FLAG_SIDE_CHANNEL);

    /* This macro fills in the event structure */
    SIGEV_PULSE_INIT(&event, SelfCoid, 24, TimerPulseCode, 0);
    mq_notify(  queue, &event );

    /* allocate a context structure */
    ctp = dispatch_context_alloc(dpp);
    while(1) {
        printf("sleep now 11111\n");
        if ( NULL != (ctp = dispatch_block(ctp)) )
        {
            printf("sleep now \n");
            dispatch_handler( ctp );
        }
        else
        {
            printf("errno \n");
            return -1;
        }
            
    }

    
	return EXIT_SUCCESS;
}

See the test code above, it is fine to notify when recive the first message, but after ward notify do not really work.

am i missing anything?
Re: mq_notify seems only notify once  
I think u need to call/setup mq_notify(), every time u read the messages. 

The QNX documentation on mq_notify() says the following

If notification isn't NULL, the mq_notify() function asks the server to notify the calling process when the queue makes 
the transition from empty to nonempty. The means by which the server is to notify the process is passed in the sigevent 
structure pointed to by notification. Once the message queue server has notified the process of the transition, the 
notification is removed. 

If u read the last line of the above para u will get notified only once of a transition from empty to non empty.
Re: mq_notify seems only notify once  
> I think u need to call/setup mq_notify(), every time u read the messages. 
> 
> The QNX documentation on mq_notify() says the following
> 
> If notification isn't NULL, the mq_notify() function asks the server to notify
>  the calling process when the queue makes the transition from empty to 
> nonempty. The means by which the server is to notify the process is passed in 
> the sigevent structure pointed to by notification. Once the message queue 
> server has notified the process of the transition, the notification is removed
> . 
> 
> If u read the last line of the above para u will get notified only once of a 
> transition from empty to non empty.


Thanks, yes mq_notify need to setup every time read the mesage