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.
I want to run a set of code at set intervals, but allow my program to continue running while this is happening. I thought of two ways to do this: with a timer, or by starting a for loop (with sleep) running in the background. The timer way seemed much simpler, and I can use timers in Windows, so I thought it would be easy in Linux...
Does anybody know how to set up a timer in Linux? I have spent some time trying to figure it out from google and the contents of linux/timer.h, which I thought would have what I needed. However, whenever I try to compile a program which includes linux/timer.h, I get the error:
/usr/include/linux/timer.h:17: error: field `list' has incomplete type
jiffies to the best of my knowledge is accessible only from kernel space..
it actually is not a timer but the timers are based on the concept of jiffies..the varialble "jiffies" is updated every 1/HZ (HZ is the frequency at which the timer interrupts are generated)..on the 2.4 series kernels, this value of 100, i.e. jiffies was updated ever 10 milliseconds, in the 2.5 series onwards, HZ was changed to 1000 and thus jiffies gets updated every 1 millisecond...
this forms the basis for the timers on linux....read understandin the linux kernel to understand how the timers work...
also there is a timestamp counter that helps get resolution of more than a millisecond...
these things are explained in the timers chapter...
I read through the man page, but still can't see how I should use the getitimer and setitimer functions to get my desired behaviour. I got the impression that I should:
Declare an itimerval, like:
This compiles and runs, and if I use kill to send it the SIGALRM signal, it does what it should. However, it doesnt do anything of it's own accord. Is there something I need to do to activate the timer? Something with setitimer maybe?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.