咨询一个POSIX消息队列和定时器 一起使用的错误

米车阿里 2012-06-29 09:08:07
咨询一个POSIX消息队列和定时器 一起使用的错误

在使用POSIX定时器的时候,在timer_set后面用POSIX消息队列接受函数 mq_receive进行阻塞,然后在超时处理函数中发送一个消息到这个消息队列,然后这里mq_receive 就会报错, 通过查一些资料,感觉是 因为在定时器超时的时候 会向定时器创建的 地方发送一份信号量,而这个信号量使 mq_receive 出错,不知道是不是这个问题,还有其他解决方法吗?


#include <taskLib.h>
#include "sysLib.h"
#include "semLib.h"
#include "msgQLib.h"
#include "time.h"
#include <stdio.h>
#include "strings.h"
#include "stdlib.h"
#include "rtpLib.h"
#include "sdLib.h"
#include "sdLibCommon.h"
#include <semaphore.h>
#include <ticklib.h>
#include "msgQLib.h"
#include "mqueue.h"
#include "ioLib.h"
#include "sys/stat.h"
#include "signal.h"
timer_t synTimerID;
sem_t *sem_SynEveComp;
mqd_t mq_syn;
STATUS deadlineHandler(timer_t ti, int para)
{
int eventID;

int *a;
a = (int *)para;
eventID = *(a+0);
printf("a[1]=%d\n",*(a+1));
*(a+1) = 1;
printf("04_C:\t Start Outtime Handler!\n");
if (mq_send(mq_syn, (char *)&eventID, sizeof (int), NULL) == ERROR)
{
perror("04_A:\t ERROR_outtime msg send!\n");
return (ERROR);
}
printf("04_C:\t Over Outtime Handler!\n");
return(OK);
}


int timer()
{
struct itimerspec time_value;
/* 启动定时器 */
printf("timeID:%d \n",synTimerID);
struct sigevent evp;
evp.sigev_value.sival_ptr = &synTimerID;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGUSR1;
timer_create(CLOCK_REALTIME, &evp, &synTimerID);
// timer_create(CLOCK_REALTIME, NULL, &synTimerID);
printf("timeID:%d \n",synTimerID);
int para[2];
int eventID = 20;
para[0] = eventID;
para[1] = 0;
timer_connect(synTimerID, (VOIDFUNCPTR)deadlineHandler, (int)¶);

bzero((char *)&time_value, sizeof(struct itimerspec));
time_value.it_interval.tv_sec = 0;//2秒间隔
time_value.it_interval.tv_nsec = 0;
time_value.it_value.tv_sec = 5;
time_value.it_value.tv_nsec = 0;//0.5秒后开始
if (ERROR == timer_settime(synTimerID, 0, &time_value, NULL))
{
printf("04_A:\t ERROR_set timer for synevent!\n");
}
printf("04_A:\t Timer set!!\n");
char buffer[20];
while(1)
{
printf("04_A:\t waitting!\n");
//sem_wait(sem_SynEveComp);
if ( ERROR == mq_receive(mq_syn,buffer,sizeof(buffer),NULL))
{
perror("error mq_receive aaaaaa\n");
}
if (ERROR == timer_settime(synTimerID, 0, &time_value, NULL))
{
printf("04_A:\t ERROR_set timer for synevent!\n");
}
}
return 1;
}

int main()
{
printf("waiting sem!!\n");
struct mq_attr attrStartSchTimer;
attrStartSchTimer.mq_maxmsg = 10;
attrStartSchTimer.mq_msgsize = 20;
attrStartSchTimer.mq_flags = 0;

mq_syn = mq_open("/mq_startSchTimer", O_RDWR|O_CREAT|O_EXCL,
S_IRUSR|S_IWUSR, &attrStartSchTimer);
if (NULL == mq_syn)
{
perror("mq_syn - mq_open");
return (ERROR);
}

if (ERROR == mq_setattr(mq_syn, &attrStartSchTimer, NULL))
{
return (ERROR);
}
timer();
printf("over\n");
return 1;
}

...全文
775 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
普雅 2014-09-29
  • 打赏
  • 举报
回复
我也碰到了 ,是因为信号的干扰,在消息队列接收到错误消息的时候判断如果是某一种错误的话,让消息队列继续接收就行了
yuxianxiancos 2013-04-25
  • 打赏
  • 举报
回复
请问你解决没?我也碰到过?要解决了,教教我啊,拜托了。

2,179

社区成员

发帖
与我相关
我的任务
社区描述
xworks是美国 Wind River System 公司( 以下简称风河公司 ,即 WRS 公司)推出的一个实时操作系统。
社区管理员
  • VxWorks开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧