gunakkoc
gunakkoc

Reputation: 1069

Perl alarm() equivalent in C?

What is the Perl's alarm() equivalent in C for Linux? AFAIK there is no native alarm function in Windows, but Perl made a workaround which I am not really curious about.

For the ones who don't know about alarm: Perl alarm

EDIT: I actually need alarm with milisecond precision. And the one that I can use in a thread(in a multithreaded app).

Upvotes: 3

Views: 203

Answers (1)

dpkp
dpkp

Reputation: 1459

something like:

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

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

Upvotes: 2

Related Questions