Timer in Linux

This is a discussion on Timer in Linux within the Linux Programming forums, part of the Platform Specific Boards category; Hi I need to find the time between two events in my code. Mainly I'm receiving packets over Bluetooth and ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    Timer in Linux

    Hi I need to find the time between two events in my code. Mainly I'm receiving packets over Bluetooth and as soon as I receive a certain amount of packets I launch a video player using fork() and exec().

    On every packet I receive I increment a packet counter and on the nth packet I execute the video player.

    Basically what I need is a timer from: the time the first packet arrives till the time the nth packet arrives

    I used the standard time_t function but I need something more accurate. Ideally something up to millisecond accuracy would be perfect.

    thanks

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, the C standard library offers the clock function, who's more accurate by a couple orders of magnitude than the time function usually (it's implementation specific if i remember well).

    Otherwise, i couldn't help you much. You could try searching in the section 2 of man pages.

    EDIT: Just by the way, on Visual Studio 2005, the clock function is accurate to the millisecond.
    Last edited by foxman; 04-18-2008 at 06:51 PM.
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    look up gettimeofday(). The data structure allows for microsecond accuracy, but the actual accuracy is implementation defined (on the Linux IIRC it's somewhere around 1x10^-5s)

    clock() is not a good idea IMHO, because it does different things on different platforms. On some, it measures the wall time, and it measures CPU time in others, which can be quite significantly different from the wall time, especially if you do a lot of IO (networking for example).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    There's a Linux forum on this site...just sayin'.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    There's a Linux forum on this site...just sayin'.
    Indeed. Moved to Linux Programming.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    thanks for the help ppl

    working gr8 with gettimeofday() function and sorry for posting in the wrong forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux timer functions
    By onebrother in forum C Programming
    Replies: 3
    Last Post: 06-18-2007, 10:53 PM
  2. Timer in Linux
    By mc99044 in forum C Programming
    Replies: 1
    Last Post: 07-20-2004, 08:42 AM
  3. timer qustion again, on linux
    By crystalike in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 12:43 PM

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