gpt4 book ai didi

c - 即使设置了 itimerspec 的所有成员,timer_settime 也会失败

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:30 26 4
gpt4 key购买 nike

因此,直到最近我们决定将其移至 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;
}

最佳答案

timer_settime 被记录为将 timer_t 作为第一个参数,而不是像 timer_create 这样的 timer_t *。如果 timerid 无效,它将失败并返回 EINVAL

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

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

关于c - 即使设置了 itimerspec 的所有成员,timer_settime 也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12753883/

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