2012-10-05 351 views
0

因此,直到最近,我們決定將它移動到Lubuntu 12.04系統時,此代碼運行良好。到timer_settime調用返回EINVAL,並在gdb下運行它,我已經證實,所有TS領域都在0到999999999,在它被調用的時候:即使設置了itimerspec的所有成員,timer_settime也會失敗

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; 
} 

回答

1

timer_settime記錄爲取timer_t作爲第一個參數,而不是像timer_t *timer_create。如果timerid無效,它將失敗,並返回EINVAL

因此,您應該通過*tid作爲第一個參數。

請注意,您的編譯器應該爲此發出警告。

+0

奇怪的是,gcc -Wall並沒有透露這一點,它在我們嘗試過的四種環境中的三種情況下完美運行。奇怪的。那麼,現在它不再失敗,但計時器不會失敗,所以你回答了我問的問題。 –

相關問題