2,179
社区成员
发帖
与我相关
我的任务
分享
#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;
}