gpt4 book ai didi

c - Posix Timer 周期性地向前跳过其周期的一半

转载 作者:IT王子 更新时间:2023-10-29 00:42:22 24 4
gpt4 key购买 nike

我有一个任务链接到每 20 毫秒/50 赫兹执行一次的 Posix 定时器 (timer_create())。大多数情况下工作正常,除了每 334.5 秒(大约)定时器提前 10 毫秒执行一个周期。在接下来的 334.5 秒左右,间隔再次全部为 20 毫秒。

我已经包含了配置定时器的相关代码。该应用程序在 Gumstix 的默认 Linux 版本的 Gumstix Verdex Pro XL6P 上运行。我还使用 FIFO 调度算法对其进行了调度。

我的直觉告诉我这是一个整数溢出问题。也许还有其他东西使用相同的信号?我一直能够在董事会的执行和 session 中重现跳过。

这个问题不是问题,但我真的很想了解为什么会这样。

下面是配置定时器的代码:

//------------------------------------------------------------------------------
// Create a timer which fires at the specified time and calls a timer event
// handler.
//
// handler : function to be called when the timer expires
// us : number of microseconds to add to timer
// ms : number of milliseconds to add to timer
// sec : number of seconds to add to timer
//------------------------------------------------------------------------------
void createTimer(void (*handler)(void), uint32 us, uint32 ms, uint32 sec)
{
struct sigaction sigact;
struct sigevent sigev;
timer_t timerid;
struct itimerspec itval;
struct itimerspec oitval;
timer_info_t* newTimer = NULL;

// Initialize signalNum.
if (timers == NULL)
signalNum = SIGRTMAX;

if (signalNum < SIGRTMIN)
exitWithError("no avaiable signals, unable to create timers");

sigemptyset(&sigact.sa_mask);
sigact.sa_flags = SA_SIGINFO;
sigact.sa_sigaction = signalHandler;

// Set up sigaction to catch signal
if (sigaction(signalNum, &sigact, NULL) == -1)
exitWithError("sigaction() failed, unabled to creat timers");

// Create the POSIX timer to generate signo
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = signalNum;
sigev.sigev_value.sival_ptr = &timerid;

long ret = timer_create(CLOCK_REALTIME, &sigev, &timerid);

if (ret == 0)
{
// Prevent overflow in calculation of nsec below.
if (ms >= 1000)
{
sec += (ms / 1000);
ms = ms % 1000;
}

itval.it_value.tv_sec = sec;
itval.it_value.tv_nsec = (long)(us * 1000L) + (long)(ms * 1000L * 1000L);

// Configure it as a repeat timer.
itval.it_interval.tv_sec = itval.it_value.tv_sec;
itval.it_interval.tv_nsec = itval.it_value.tv_nsec;

if (timer_settime(timerid, 0, &itval, &oitval) != 0)
exitWithError("time_settime() error!");
}
else
exitWithError("timer_create() error!");

newTimer = (timer_info_t*)malloc(sizeof(timer_info_t));
newTimer->timer = timerid;
newTimer->handler = handler;
newTimer->sigNum = signalNum;
newTimer->next = NULL;

// Check to see if this is the first time through.
if (timers == NULL)
{
timers = newTimer;
atexit(deleteTimers);
}
else
lastTimer->next = newTimer;

lastTimer = newTimer;

signalNum--;
}

提前致谢。

最佳答案

我的猜测是您正在使用 ntp,并且它会以这些间隔调整时间。您可以尝试改用 CLOCK_MONOTONIC,但根据 http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html它也可能受到影响。

关于c - Posix Timer 周期性地向前跳过其周期的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7276251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com