Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
08-29-2004, 07:16 AM
|
#1
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Rep:
|
C - Linux - Timers - How?
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
Thank you.
|
|
|
08-29-2004, 08:41 AM
|
#2
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Check the man page of "setitimer".
|
|
|
08-29-2004, 01:32 PM
|
#3
|
Member
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35
Rep:
|
Related to timer i usually heard about "jiffis" .what is it? why it is used?
|
|
|
08-30-2004, 02:28 AM
|
#4
|
Member
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251
Rep:
|
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...
|
|
|
08-30-2004, 09:58 AM
|
#5
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Original Poster
Rep:
|
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:
Code:
struct timeval my_value={1,500};
struct timeval my_interval={1,500};
struct itimerval my_timer={my_interval,my_value};
Then run getitimer on it like:
Code:
getitimer(ITIMER_REAL, *my_timer);
And then my program would recieve SIGALRM every 1.5 seconds. How can I set my program up to do something when it gets SIGALRM?
Last edited by drigz; 08-30-2004 at 10:14 AM.
|
|
|
08-30-2004, 10:26 AM
|
#6
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Original Poster
Rep:
|
OK so far I have this:
Code:
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
typedef void (*sighandler_t)(int);
foo(int theint)
{
printf("I just got the SIGALRM signal\n");
}
main()
{
struct timeval my_value={1,500};
struct timeval my_interval={1,500};
struct itimerval my_timer={my_interval,my_value};
getitimer(ITIMER_REAL, &my_timer);
int i;
signal(SIGALRM, (sighandler_t) foo);
for(;;)
i=5;
}
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?
|
|
|
08-30-2004, 02:36 PM
|
#7
|
Member
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407
Original Poster
Rep:
|
Got it!
Code:
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
typedef void (*sighandler_t)(int);
foo(int theint)
{
printf("I just got the SIGALRM signal\n");
}
main()
{
struct timeval my_value={1,0};
struct timeval my_interval={1,0};
struct itimerval my_timer={my_interval,my_value};
setitimer(ITIMER_REAL, &my_timer, 0);
int i;
signal(SIGALRM, (sighandler_t) foo);
for( ; ; )
i=5;
}
|
|
|
All times are GMT -5. The time now is 12:25 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.
|
Latest Threads
LQ News
|
|