1

因此,直到最近,当我们决定将其移至 Lubuntu 12.04 系统时,这段代码才能正常工作。对 timer_settime 的调用返回 EINVAL,并在 gdb 下运行它我已经确认 ts 的所有字段在调用时都在 0 和 999999999 之间:

1067        if(-1 ==timer_settime(tid,0,&ts,NULL))
(gdb) print ts
$1 = {it_interval = {tv_sec = 0, tv_nsec = 200000000}, it_value = {tv_sec = 0,
tv_nsec = 0}}

因为这应该是唯一可以导致它返回 EINVAL 的东西,所以我非常困惑。也许这里有一些明显的东西我错过了。

struct sigevent sev;
struct itimerspec ts;
timer_t *tid;
//actually point the pointer at something.
tid = calloc(1,sizeof(timer_t));
//make sure there's no garbage in the structures.
memset(&sev,0,sizeof(struct sigevent));
memset(&ts,0, sizeof(struct itimerspec));
//notify via thread
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = SwitchThreadHandler;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = tid;
ts.it_value.tv_sec =0;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 200000000;
if(-1 == timer_create(CLOCK_REALTIME,&sev,tid))
{
    retval = EX_SOFTWARE;
    fprintf(stderr,"Failed to create timer.");
    free(tid);
    return retval;
}

if(-1 ==timer_settime(tid,0,&ts,NULL))
{
    int errsv = errno;
    fprintf(stderr,"timer_settime FAILED!!!\n");
    if(errsv == EINVAL)
    {
        fprintf(stderr,"INVALID VALUE!\n");
    }
    else
    {
        fprintf(stderr,"UNKOWN ERROR: %d\n",errsv);
    }
    return EX_SOFTWARE;
}
4

1 回答 1

4

timer_settime记录为将 atimer_t作为第一个参数,而不是 a timer_t *like timer_createEINVAL如果timerid无效,它将失败。

*tid因此,您应该作为第一个参数传递。

请注意,您的编译器应该为此发出警告。

于 2012-10-05T21:51:49.410 回答