SIGALRM is an easy concept to implement, but becomes complex as soon as you have to juggle two or more balls in the air, i.e. support two or more timers with only one alarm clock. But it is doable with some simple math.I may have missed it, but has no one mentioned alarm(n) in this thread? It sends a SIGALRM interrupt in "n" seconds.
No need for another thread. Also I see there now the interval timers: setitimer() (and getitimer()) which look a bit more fiddly to use. Alarm() is very simple and has been around since the dawn of time.Code:
LIBRARY Standard C library (libc, -lc)SYNOPSIS #include <unistd.h> unsigned int alarm(unsigned int seconds);DESCRIPTION alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds. If seconds is zero, any pending alarm is canceled. In any event any previously set alarm() is canceled.RETURN VALUE alarm() returns the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.STANDARDS POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.NOTES alarm() and setitimer(2) share the same timer; calls to one will interfere with use of the other. Alarms created by alarm() are preserved across execve(2) and are not inherited by children created via fork(2).
Statistics: Posted by deepo — Sat Dec 21, 2024 7:17 pm