Sponsored Content
Top Forums Programming Microsecond timer and printf issues in C++ Post 302386741 by scazz on Wednesday 13th of January 2010 11:12:49 AM
Old 01-13-2010
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 12:13 PM.. Reason: Missed line of code above for loop
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
install_int(3alleg4)						  Allegro manual					      install_int(3alleg4)

NAME
install_int - Installs a user timer handler. Allegro game programming library. SYNOPSIS
#include <allegro.h> int install_int(void (*proc)(), int speed); DESCRIPTION
Installs a user timer handler, with the speed given as the number of milliseconds between ticks. This is the same thing as install_int_ex(proc, MSEC_TO_TIMER(speed)). If you call this routine without having first installed the timer module, install_timer() will be called automatically. Calling again this routine with the same timer handler as parameter allows you to adjust its speed. RETURN VALUE
Returns zero on success, or a negative number if there is no room to add a new user timer. SEE ALSO
install_timer(3alleg4), remove_int(3alleg4), install_int_ex(3alleg4), install_param_int(3alleg4), exscn3d(3alleg4), exswitch(3alleg4), extimer(3alleg4), exzbuf(3alleg4) Allegro version 4.4.2 install_int(3alleg4)
All times are GMT -4. The time now is 02:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy