Timing (doing something x times per second)

This is a discussion on Timing (doing something x times per second) within the C Programming forums, part of the General Programming Boards category; Sorry for the broad thread title. Something I've always wondered is how programs like sound drivers do something exactly ____ ...

  1. #1
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    958

    Timing (doing something x times per second)

    Sorry for the broad thread title.

    Something I've always wondered is how programs like sound drivers do something exactly ____ times a second. In the sound driver's case, probably 44100 times a second. I know there are platform specific sleep functions, but whatever operation is done is going to take time as well, so it's not all as simple as that.

    How is this accomplished? One thread for timing and one thread to do the task?

    In my case, I'm going to be using an FTDI chip to interface with an older chip and will need to update the registers with microsecond accuracy (1 MHz).

    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The answer to that it *totally* platform dependent...

    On windows you have Timers... SetTimer Function (Windows) ... provided by the OS...

    Other systems may have events or counters for the purpose.

    Many uC (Micro-Controller) setups have built in timer interrups.

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    958
    So that's all there is to it? Somehow I thought it'd be more elaborate. Thanks for the help.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    There probably will be a lot more to it to meet your requirement of microsecond accuracy, depending on what you mean by that phrase. The most common OS options - Windows, Mac, Linux - won't be able to guarantee this. Even if they can set a timer to go off every microsecond it won't be exactly a microsecond, and there's no guarantee if it will be shorter or longer.

    But then again if you're using this chip under an OS there's probably driver support to abstract away the need to write specific values into certain registers on a given clock pulse.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Even if they can set a timer to go off every microsecond it won't be exactly a microsecond, and there's no guarantee if it will be shorter or longer.
    Isn't setting a process to realtime supposed to do that?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    I think it's just the highest priority level for NT-based systems. It doesn't turn a normal Windows box into an RTOS but it might make it more likely that you'll get a better response time. That could make your program work, or it could just make the occasional timing problem happen less frequently and therefore make it harder to debug.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by KCfromNC View Post
    I think it's just the highest priority level for NT-based systems. It doesn't turn a normal Windows box into an RTOS but it might make it more likely that you'll get a better response time. That could make your program work, or it could just make the occasional timing problem happen less frequently and therefore make it harder to debug.
    What about Linux ?
    I heard of some projects making real time OS `s based on the Linux kernel.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KCfromNC View Post
    There probably will be a lot more to it to meet your requirement of microsecond accuracy, depending on what you mean by that phrase. The most common OS options - Windows, Mac, Linux - won't be able to guarantee this. Even if they can set a timer to go off every microsecond it won't be exactly a microsecond, and there's no guarantee if it will be shorter or longer.
    This is more true of userspace than kernel space, and the OP did say s/he's working on a hardware interface, which on any OS should provide access to kernel timers with fairly dependable microsecond accuracy as they run directly off the frequency of the CPU.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Isn't setting a process to realtime supposed to do that?
    Naaa... that might bring you down to +-.5 ms but it will never get you to microseconds in a multitasking system.

    Usually timing that precise is the stuff of crystal clocks and data latches... hardware driven, not software.

  10. #10
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    958
    Well it turns out that I was misguided and probably only need millisecond resolution. Either way, your answer still helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Timing
    By BKW++ in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2009, 05:37 PM
  2. timing
    By trent in forum C Programming
    Replies: 3
    Last Post: 03-24-2009, 09:05 AM
  3. Timing in C++?
    By gijake in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2005, 08:12 PM
  4. Timing in C
    By Konrad in forum C Programming
    Replies: 4
    Last Post: 06-24-2003, 01:51 AM
  5. Timing
    By Korn1699 in forum C++ Programming
    Replies: 10
    Last Post: 06-06-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21