LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-29-2004, 07:16 AM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
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.
 
Old 08-29-2004, 08:41 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Check the man page of "setitimer".
 
Old 08-29-2004, 01:32 PM   #3
vijeesh_ep
Member
 
Registered: Jul 2004
Location: India
Distribution: Redhat-8
Posts: 35

Rep: Reputation: 15
Related to timer i usually heard about "jiffis" .what is it? why it is used?
 
Old 08-30-2004, 02:28 AM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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...
 
Old 08-30-2004, 09:58 AM   #5
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
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.
 
Old 08-30-2004, 10:26 AM   #6
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
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?
 
Old 08-30-2004, 02:36 PM   #7
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
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;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about timers eshwar_ind Programming 1 12-27-2004 09:23 AM
Pls Help, Singals and Timers problem in RedHat Linux 7.3 kumarnine Programming 2 08-26-2004 09:16 AM
timers is user space in linux gooner Programming 0 06-07-2004 03:52 AM
Help with Linux timers samble Programming 8 02-07-2004 10:54 PM
Any old timers out there remember 486's? shmoo28 Linux - Software 4 11-07-2001 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration