I am trying to learn how to use sigtimedwait (), but I believe that it does not wait for the timeout to complete. Below, it seems EAGAIN 4s returns faster than necessary (1 s faster in 1 minute timeout):
#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stddef.h>
#include <errno.h>
int main(int argc, char* argv[]) {
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog ("SIG_TIMED_WAITER", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog (LOG_NOTICE, "Started");
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
struct timespec to;
to.tv_sec = 240;
to.tv_nsec = 0;
int ret = sigtimedwait(&set, NULL, &to);
if(ret < 0) {
if (errno == EAGAIN) {
syslog (LOG_NOTICE, "EAGAIN: TimedWait complete...");
} else {
syslog (LOG_NOTICE, "ERROR!");
}
} else {
syslog (LOG_NOTICE, "Interrupted by signum: %d.", ret);
}
syslog (LOG_NOTICE, "Terminated.");
closelog();
}
And here is the conclusion:
$ tail -f /var/log/syslog|grep "SIG_TIMED_WAITER"
Jan 7 15:39:41 localhost SIG_TIMED_WAITER[13275]: Started
Jan 7 15:43:36 localhost SIG_TIMED_WAITER[13275]: EAGAIN: TimedWait complete...
Jan 7 15:43:36 localhost SIG_TIMED_WAITER[13275]: Terminated.
I expected to see "EAGAIN: TimedWait complete ..." recorded four seconds later.
Is something wrong with my code, or is it for some other reason? Note that I do not see this, for example, select (), which waits four minutes.
source
share