Try changing it as follows, it should be quite a lot so that it never misses waking up, but be careful with it, since running a priority in real time can lock your machine if it is not sleeping, you may also need to set your user to have the ability to run material in priority in real time (see /etc/security/limits.conf )
#include <sys/timerfd.h> #include <time.h> #include <string.h> #include <stdint.h> #include <stdio.h> #include <sched.h> int main(int argc, char *argv[]) { int timerfd = timerfd_create(CLOCK_MONOTONIC,0); int milliseconds = atoi(argv[1]); struct itimerspec timspec; struct sched_param schedparm; memset(&schedparm, 0, sizeof(schedparm)); schedparm.sched_priority = 1; // lowest rt priority sched_setscheduler(0, SCHED_FIFO, &schedparm); bzero(&timspec, sizeof(timspec)); timspec.it_interval.tv_sec = 0; timspec.it_interval.tv_nsec = milliseconds * 1000000; timspec.it_value.tv_sec = 0; timspec.it_value.tv_nsec = 1; int res = timerfd_settime(timerfd, 0, &timspec, 0); if(res < 0){ perror("timerfd_settime:"); } uint64_t expirations = 0; int iterations = 0; while( res = read(timerfd, &expirations, sizeof(expirations))){ if(res < 0){ perror("read:"); continue; } if(expirations > 1){ printf("%ld expirations, %d iterations\n", expirations, iterations); break; } iterations++; } }
If you use threads, you should use pthread_setschedparam instead of sched_setscheduler .
In real time, it’s also not about low latency, about guarantees, RT means that if you want to wake up exactly once a second on the second, you WILL, normal planning does not give you this, it may decide to wake you up to 100 ms later, because in anyway, at that time he had a different job. If you want to wake up every 10 ms, and you really need to, then you must configure yourself as a task in real time, then the kernel will wake you up every 10 ms without crashing. If a task with a higher priority in real time is busy.
If you need to ensure that your wake-up interval is exactly some time, it does not matter if it is 1 ms or 1 second, you will not receive it if you do not perform the task in real time. There are good reasons why the kernel will do this with you (energy saving is one of them, higher throughput is the other, there are others), but it has rights to it, since you never talked about it You need the best warranty. Most things do not really have to be so precise or you don’t need to miss, so you should consider whether you really need it.
quote from http://www.ganssle.com/articles/realtime.htm
A tough task or real-time system where an activity simply needs to be completed - always - by a specified deadline. The deadline may be a specific time or time interval, or may be the arrival of an event. Hard real-time tasks fail, by definition, if they miss such a deadline.
Note that this definition does not make an assumption about the frequency or duration of tasks. A microsecond or a week - if there is not enough time, it induces a failure, then the task is strict requirements for real time.
Soft real time is almost the same, except that a missed deadline, while undesirable, is not the end of the world (for example, playing video and audio are real-time tasks in real time, you do not want to skip the frame display, or the buffer ends, but if you do it just an instant hiccup, and you just continue). If what you are trying to do is soft in real time, I would not work with real-time priority, since you usually need to get your awakenings on time (or at least close to it).
EDIT:
If you do not work in real time, the kernel will by default give you some timers that you will make "weak" so that it can combine your request in order to wake up with other events that happen sometimes close to the one you asked (if this other event is within your "weak" time, it will not wake you up at the time you asked, but a little earlier or later, at the same time he is already going to do something else, it saves energy) .
For more information, see High (but not too high) permission timeouts and Timer slack (note that I'm not sure that either of these things is exactly what is really in the kernel, since both of these articles are related to discussions on the lkml mailing list, but something like the first is really in the kernel.