Microsecond timer and printf issues in C++


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming Microsecond timer and printf issues in C++
# 1  
Microsecond timer and printf issues in C++

Hi guys,

First post here - hope it's in the right place.

I'm writing a timer for a client. It can send data every 100ms and receive every 150. It's an AI project so I *don't* want to fork() and pipe or go multi threaded, use interupts etc.

Using linux, gcc 4.3.2

The code I have:

Code:
initUsecs=usecs();
for ( ; ; ) {
    curUsecs=usecs();
    elapsed = curUsecs-initUsecs;
    printf("curUsecs: %d elapsed: %d\n", curUsecs, elapsed);
    
    if (elapsed > 20000) {
        printf("2 seconds, resetting\n");
        initUsecs=curUsecs;
    }
}

Code:
int Agent::usecs() 
{
    struct timeval tv;
         struct timezone tz;
    struct tm *tm;
    int ms;

    gettimeofday(&tv, &tz);
    ms = tv.tv_sec -1263300000;
    ms *= 1000;
    ms += tv.tv_usec;
    return(ms);
}


It's fairly ugly I know, but when I run it normally, the output is fairly unexpected. 'elapsed' turns to negative numbers after about 2 seconds and if I take out the first printf ('printf("curUsecs: %d....') then it dumps 7 or 8 lines, then nothing for a while and dumps some more or occasionally doesn't dump anything if I've mess arround a bit more. If I pipe it out to less or redirect output to a file insead of stdout then the results seem to be fine but I can't test it to make sure it is actually kicking in about every two seconds.

Is this a printf thing? Have I done something stupid? Is there an easier way to time things? (I was using clock() orriginally but I can't simulate more than one agent per cpu that way). How do I know it won't do this in the middle of execution?

Many thanks
Sam

Last edited by scazz; 01-13-2010 at 01:13 PM.. Reason: Missed line of code above for loop
# 2  
This is probably why - don't use %d, use %ld at least. check your sytem's definition of the timeval datatypes.

Does sys/types.h have usec_t typedef? Then don't return int, return usec_t from usecs(). sys/time.h usually has something like this:

Code:
typedef long    suseconds_t;    /* signed # of microseconds */

struct timeval {
        time_t          tv_sec;         /* seconds */
        suseconds_t     tv_usec;        /* and microseconds */


edit: one other note - usually struct timezone tz is passed as a NULL. Read your system's manpage for gettimeofday

IMO you have inadvertantly hashed up the datatypes.

---------- Post updated at 09:44 ---------- Previous update was at 09:23 ----------

tv_usec is one-millionth of a second, microseconds not one-thousandth, milliseconds.
# 3  
Fantastic - good call. All fixed - cheers Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #442
Difficulty: Easy
84% of the women surveyed by Cosmopolitan Magazine in 2018 indicated they would prefer to date a computer programmer than a rock star.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Timer as output

How do you set timer as output to the command line so that you get an output like Has been waiting for 5 seconds Has been waiting for 6 seconds ... Where only the number changes. (2 Replies)
Discussion started by: locoroco
2 Replies

2. UNIX for Dummies Questions & Answers

Timer

is there a timer function in unix without using C? for example i want to display a message after 5 seconds how do i do that? (2 Replies)
Discussion started by: khestoi
2 Replies

3. Shell Programming and Scripting

Timer

Is there a way to make a timer? E.g Please give the seconds... ... (6 Replies)
Discussion started by: aekaramg20
6 Replies

4. UNIX for Dummies Questions & Answers

timer interrupt

hello all since a process running in kernel mode cannnot be preempted by any other process what would be the status of Timer interrupt that occurs when the time quantum of a process is elapsed? thanks (2 Replies)
Discussion started by: compbug
2 Replies

5. UNIX for Advanced & Expert Users

Timer for VNC

Hello fellows, I am new in this forum, i would appreciate your assistance. I need a timming system for my vnc desktops (Cybercafe timer stuff). Each unix user login to my server only with vnc, and i want to write a program that can generate timer tickets and have control on the time used for... (1 Reply)
Discussion started by: foweja
1 Replies

6. Shell Programming and Scripting

VNC Timer

Hello fellows, I am new in this forum, i would appreciate your assistance. I need a timming system for my vnc desktops (Cybercafe timer stuff). Each unix user login to my server only with vnc, and i want to write a program that can generate timer tickets and have control on the time used for... (0 Replies)
Discussion started by: foweja
0 Replies

7. Shell Programming and Scripting

timer

Hi all, Wanted to a create a shell script ----------------------------------------------------------------------- 1) which when called will start a timer and wait for 48 hours. after 48 hours it will call some function(say XYZ) 2) Whenever this shell script is called (can be called... (3 Replies)
Discussion started by: k_oops9
3 Replies

8. Shell Programming and Scripting

writing a timer

Hi!, My shell script takes a quite a long time to execute.. Nothing appears on the screen during this period.. User are left guessing... whats going on???????????? Any ideas on how to create a small timer script which print a word on screen say " wait.. Program running" after every 10 seconds... (3 Replies)
Discussion started by: jyotipg
3 Replies

9. AIX

how to implement timer

anyone can help me how to implement the timer on AIX? I tried with 'setitimer' and its related functions, but it does not work correctly,the program exited each time. thanks (2 Replies)
Discussion started by: Frank2004
2 Replies

10. Programming

generating timer

I'm trying generate an interrupt every 1 seconds using itimer and My clock is not running. This is what i did : printf("about to sleep for 1 second \n"); signal(SIGALRM, wakeup); //myTimer.it_interval.tv_sec=0; //myTimer.it_interval.tv_usec =0; ... (5 Replies)
Discussion started by: Confuse
5 Replies

Featured Tech Videos