Linux中posix消息队列系统函数mq_timedreceive()谨慎使用

背景:在工作中时不时出现设备掉心跳的情况,找了很久没发现原因,最终在众多日志和抓包中发现了原因,正是这个mq_timedreceive()函数


该函数原型:
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,
                   size_t msg_len, unsigned *msg_prio,
                   const struct timespec *abs_timeout);

该函数的描述:

mq_timedreceive() behaves just like mq_receive(), except that if the queue is empty and the O_NONBLOCK flag is not enabled for the message queue description, then abs_timeout points to a structure which specifies a ceiling on the time for which the call will block. This ceiling is an absolute timeout in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC), and it is specified in the following structure:

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};
If no message is available, and the timeout has already expired by the time of the call,  mq_timedreceive() returns immediately.
如上可知,这个阻塞函数的输入是个绝对时间,需要获取当前系统时间,再加上需要阻塞的时间最为输入;例如当前时间是09:10:00,需要阻塞10S等待接收消息,那么这个函数如果一直没有消息会在09:10:10立即返回。
但是有一个问题,就是当这个系统时间被修改后会怎么样?那会导致阻塞的时长加长,如上所说,如果在等待的时候系统时间被修改为09:09:30,那么如果这个消息队列没有消息过来,阻塞函数还必须等到09:10:10才会返回,那么最起码延长了30S的阻塞时长;所以建议设备会经常同步时钟源的慎用这个阻塞函数;
这里在唠叨一下,如果系统时间被修改为09:10:10之后会不会有异常,这里的回答是:如果这样,阻塞会被提前返回。
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值