Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl alarm() equivalent in C?

Tags:

c

linux

perl

alarm

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).

like image 440
gunakkoc Avatar asked Nov 19 '25 21:11

gunakkoc


1 Answers

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

like image 87
dpkp Avatar answered Nov 21 '25 11:11

dpkp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!