Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Jan 2000 10:03:30 -0600 (CST)
From:      "James C. Beyer" <beyer@cs.umn.edu>
To:        Sabrina Minshall <sabrina@accesscom.com>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: PR kern/14034: gettimeofday() returns negative value?
Message-ID:  <Pine.SOL.4.21.0001210952460.24651-100000@kepler.cs.umn.edu>
In-Reply-To: <Pine.BSF.4.21.0001210017590.51684-100000@resnet.uoregon.edu>

next in thread | previous in thread | raw e-mail | index | archive | help


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 majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SOL.4.21.0001210952460.24651-100000>