Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

C timers

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
3 posts • Page 1 of 1
Author
Message
christianlee
n00b
n00b
User avatar
Posts: 18
Joined: Thu Oct 21, 2004 2:21 am

C timers

  • Quote

Post by christianlee » Sat Aug 20, 2005 3:35 pm

Hi all,
Has anybody a samples how to write an application with timers in C, i need two timers which doing something with different ticks.
I've used this code below:

Code: Select all

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void timer_handler (int signum)
{
 static int count = 0;
 printf ("timer expired %d times\n", ++count);
}

int main ()
{
 struct sigaction sa;
 struct itimerval timer;

 /* Install timer_handler as the signal handler for SIGVTALRM. */
 memset (&sa, 0, sizeof (sa));
 sa.sa_handler = &timer_handler;
 sigaction (SIGVTALRM, &sa, NULL);

 /* Configure the timer to expire after 250 msec... */
 timer.it_value.tv_sec = 0;
 timer.it_value.tv_usec = 250000;
 /* ... and every 250 msec after that. */
 timer.it_interval.tv_sec = 0;
 timer.it_interval.tv_usec = 250000;
 /* Start a virtual timer. It counts down whenever this process is
   executing. */
 setitimer (ITIMER_VIRTUAL, &timer, NULL);

 /* Do busy work. */
 while (1);
}
but there're only one timer and I need two :?
Thanks, chris.
Top
iTux
Guru
Guru
Posts: 586
Joined: Tue Sep 07, 2004 11:50 pm
Location: Toronto

  • Quote

Post by iTux » Sat Aug 20, 2005 4:59 pm

I think you can have only one timer signal...

If you cannot find a way to get more, you can implement more timers in your program...


If your time are:
3 4 --> set it to 1 and when interrupted maintained the count: 1, 2, 3, 4
4 6 --> set it to 2: 2, 4, 6
12 18 --> set it to 6: 6, 12, 18

Basically greatest common divisor of your times. You might be interrupt for nothing, but anyway.


iTux
Top
Sleeper
l33t
l33t
User avatar
Posts: 667
Joined: Tue Nov 12, 2002 3:40 pm
Contact:
Contact Sleeper
Website

  • Quote

Post by Sleeper » Sun Aug 21, 2005 7:37 am

You can also build your own timer framework. Having a single base time marker (i.e for example a tick every 10ms), and a separate thread being wake up on these ticks, and handling an ordered list of timers that are expiring ..; Something along this line ...
Top
Post Reply

3 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic