2013-06-11 49 views
3

在C for Linux中,Perl的alarm()等效於什麼? AFAIK在Windows中沒有原生的alarm函數,但Perl提出了一個我並不十分好奇的解決方法。在C中相當於Perl報警()?

對於誰不知道alarm的那些:Perl alarm

編輯:我確實需要milisecond精度報警。還有我可以在線程中使用的一個(在多線程應用程序中)。

+2

是不是隻是['報警(3)'](http://linux.die.net/man/3/alarm) ? –

+1

實時定時器系統調用:timer_create(),setitimer(),timer_delete()是你想要的。 timer_create()手冊頁有一個例子。 –

+0

http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html,http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html,http://pubs.opengroup.org /onlinepubs/9699919799/functions/timer_delete.html – ysth

回答

2

類似:

unsigned int alarm (unsigned int secs, unsigned int usecs) { 
    struct itimerval old, new; 
    new.it_interval.tv_usec = 0; 
    new.it_interval.tv_sec = 0; 

    // usecs should always be < 1000000 
    secs += usecs/1000000; 
    usecs = usecs % 1000000; 

    // set the alarm timer 
    new.it_value.tv_usec = (long int) usecs; 
    new.it_value.tv_sec = (long int) secs; 

    // type ITIMER_REAL for wallclock timer 
    if (setitimer (ITIMER_REAL, &new, &old) < 0) 
    return 0; 
    else 
    return old.it_value.tv_sec; 
} 

見:http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html