Actually, a line like  temp =t1->tv_usec + 1000000;
or using some other temp to save the t1 data before it is permuted will
make the program work perfectly.  t1->tv_usec += 1000000;  Kills the
correct value when t2 is updated.  The another option would be to stop
using pointers in the function call. ie:

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

int
time_elapsed(struct timeval t1, struct timeval t2)
{
        int s, ms;
     
        s = t1.tv_sec - t2.tv_sec;
        assert((s >= 0));
        if (s != 0) { 
                if (t1.tv_usec < t2.tv_usec) {
                        s--;
                        t1.tv_usec += 1000000;
                }
        } 
        ms = s * 1000000 + (t1.tv_usec - t2.tv_usec);
        return (ms);
}

int 
main()
{
        struct timeval tv1, tv2;
        int run = 0;
        int diff;

        if (gettimeofday(&tv1, NULL) < 0) {
                perror("gettimeofday");
        }
        tv2.tv_usec = tv1.tv_usec;
        tv2.tv_sec = tv1.tv_sec;
        

        for (;;) {
                usleep(9000);
                if (gettimeofday(&tv1, NULL) < 0) {
                        perror("gettimeofday");
                }

                /*
                 * diff in usec.
                 */
                diff = time_elapsed(tv1, tv2);
                if (diff < 0) {

                        printf("-ve tvdiff %d\n", diff);
                        printf("%ld %ld %ld %ld\n",
                                        tv1.tv_sec, tv1.tv_usec,
                                        tv2.tv_sec, tv2.tv_usec);
                        printf("run = %d\n", run);
                        assert(0);
                }
                tv2.tv_usec = tv1.tv_usec;
                tv2.tv_sec = tv1.tv_sec;
                run++;
        }
        return (0);
}
works fine been running for 5 minutes without a hitch.

Don't update a value if you don't really want it changed outside a
function.

jcb




To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to