ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i am new to linux programming in C. Was looking for a periodic timer that repeats very fast like each 10 msec with overlap calling protection. Is the posix timer reliable to run on linux versus the linux specific timers?.
I would not rely on time delays much below 100ms. A desktop OS is not built to do that. It runs too many processes.
You need to design your program so that it can tolerate timing errors of hundreds of milliseconds. Or, you need to use an OS intended for real-time.
Ed
A while back, for fun, I wrote a simple animation program that ran on the linux console (writing directly to the VCS deviceš). I used a clock_nanosleep() to govern the display loop, adjusting the sleep duration each time to compensate for drift˛. Typically the latency on the wake was sub 1msec. Setting scheduling class and priority might help with reliability, but as Nev' points out, Linux is not a true RTOS and there are no guarantees.
I'd expect the results using a timer and SIGALRM to be similar.
___
1) I eventually shelved the project as the VCS device doesn't support mmap and writing out a full frame each time using write(2) was just too inefficient/slow.
2) using TIMER_ABSTIME on the nanosleep is also a good way to avoid latency induced drift.
Here's the best way to consider "timers." A timer that is set to go off every 10 msec will ensure that your process will wake up at least that often. But, having woken up, your process then needs to "look at the clock to find out what time it is." It should process its entire time-ordered work list, re-checking the clock every time, until it either runs out of work to do or finds that the next unit of work is due "more than 10 msec from now." At which time it can yield, knowing that the next timer-pop will happen "soon."
It is entirely possible that the timer might go off again while the process is doing its work, so it's entirely possible that it might "wait for the next alarm" and actually wind up "not waiting at all." The timer simply serves as a mechanism to allow the process to avoid monopolizing system resources ("busy waiting ...") when it has nothing to do. The process must constantly refer to the immediate time-of-day value as it processes its to-do list. The unit-of-work might be processed slightly later than the specified time – since "Linux is not an RTOS" – but it will not be processed sooner.
Last edited by sundialsvcs; 08-18-2022 at 04:36 PM.
Always remember that: in a "non-real time OS," such as Linux, the occurrence of a timer interrupt only causes the affected process(es) to "become runnable." It does not cause the OS to "move Hell and High Water To ..." cause said process to "immediately run." (i.e. with to strive to fulfill some promised maximum contractual degree of "latency," of which Linux by its design has none.)
And this, of course, is the fundamental difference between "real-time" and "non-real-time" OSes . . .
A "non-real-time" OS promises that "you will not wake up sooner." But it does not promise that "you will not wake up later," nor that you might not occasionally "wake up twice, finding nothing to do the second time."
Last edited by sundialsvcs; 09-12-2022 at 08:29 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.