message queues timeout pb

Hi!
I have a timeout problem with message queue function mq_timedreceive: timout
doesn’t seem to work in the following code (try to change the tsTimeout
value ! infinite value (-1) seems to work).

  • errno is set to 260 (‘timed out’) at the end of execution, which is not in
    the doc about mq_timedreceive,
  • are the flags ok ?

thanks

#include <mqueue.h>
#include <errno.h>
#include <string.h>

#define QUEUE “/MYQUEUE”
#define BUFLEN 512

int main(int argc,char* argv[])
{
char szBuf[BUFLEN];
mqd_t mqDescr;
struct mq_attr mqAttr;
struct timespec tsTimeout;
int nPrio;

/attributs/
mqAttr.mq_maxmsg=100;
mqAttr.mq_msgsize=BUFLEN;
mqAttr.mq_flags=0;//blocking state

/creation/
mqDescr=mq_open(QUEUE,O_CREAT|O_RDWR,S_IRWXO|S_IRWXG|S_IRWXU,&mqAttr);
if(mqDescr==(mqd_t)-1)
{
printf(“mq_open: error %d, %s”,errno,strerror(errno));
return 0;
}

/reception/
nsec2timespec(&tsTimeout,5000000000ULL);//timeout 5s
/nsec2timespec(&tsTimeout,(_uint64)-1);/ //infinite: ok

if(mq_timedreceive(mqDescr,szBuf,BUFLEN,&nPrio,&tsTimeout)==-1)
printf(“mq_timedreceive: error %d, %s”,errno,strerror(errno));

//
(void)mq_unlink(QUEUE);

return 0;
}

RUBET Louis <lrubet@centralp.fr> wrote:

Hi!
I have a timeout problem with message queue function mq_timedreceive: timout
doesn’t seem to work in the following code (try to change the tsTimeout
value ! infinite value (-1) seems to work).

  • errno is set to 260 (‘timed out’) at the end of execution, which is not in
    the doc about mq_timedreceive,
  • are the flags ok ?

The problem is that the time that you need to pass in to the structure
is the absolute time (hence why the doc uses abs_timeout as the
variable name). So what you should be doing here instead of:

nsec2timespec(&tsTimeout,5000000000ULL);//timeout 5s

(Is there a reason you didn’t just do tsTimeout->tv_sec = 5; ?)

is

/* Get the current time */
clock_gettime(CLOCK_REALTIME, &tsTimeout);
tsTimeout.tv_sec += 5; //5 seconds from now
//tsTimeout.tv_nsec //If you wanted to fiddle other parts

Thomas

thanks

#include <mqueue.h
#include <errno.h
#include <string.h

#define QUEUE “/MYQUEUE”
#define BUFLEN 512

int main(int argc,char* argv[])
{
char szBuf[BUFLEN];
mqd_t mqDescr;
struct mq_attr mqAttr;
struct timespec tsTimeout;
int nPrio;

/attributs/
mqAttr.mq_maxmsg=100;
mqAttr.mq_msgsize=BUFLEN;
mqAttr.mq_flags=0;//blocking state

/creation/
mqDescr=mq_open(QUEUE,O_CREAT|O_RDWR,S_IRWXO|S_IRWXG|S_IRWXU,&mqAttr);
if(mqDescr==(mqd_t)-1)
{
printf(“mq_open: error %d, %s”,errno,strerror(errno));
return 0;
}

/reception/
nsec2timespec(&tsTimeout,5000000000ULL);//timeout 5s
/nsec2timespec(&tsTimeout,(_uint64)-1);/ //infinite: ok

if(mq_timedreceive(mqDescr,szBuf,BUFLEN,&nPrio,&tsTimeout)==-1)
printf(“mq_timedreceive: error %d, %s”,errno,strerror(errno));

//
(void)mq_unlink(QUEUE);

return 0;
}

thanx a lot
bye