From owner-freebsd-hackers Fri Jan 21 8: 3:46 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from mail.cs.umn.edu (mail.cs.umn.edu [128.101.33.100]) by hub.freebsd.org (Postfix) with ESMTP id 1D66C1540A for ; Fri, 21 Jan 2000 08:03:35 -0800 (PST) (envelope-from beyer@cs.umn.edu) Received: from kepler.cs.umn.edu (beyer@kepler.cs.umn.edu [128.101.34.78]) by mail.cs.umn.edu (8.9.3/8.9.3) with ESMTP id KAA16364; Fri, 21 Jan 2000 10:03:30 -0600 (CST) Received: from localhost (beyer@localhost) by kepler.cs.umn.edu (8.9.1/8.9.0) with ESMTP id KAA24722; Fri, 21 Jan 2000 10:03:30 -0600 (CST) X-Authentication-Warning: kepler.cs.umn.edu: beyer owned process doing -bs Date: Fri, 21 Jan 2000 10:03:30 -0600 (CST) From: "James C. Beyer" To: Sabrina Minshall Cc: hackers@FreeBSD.ORG Subject: Re: PR kern/14034: gettimeofday() returns negative value? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 #include #include #include #include #include #include #include #include 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