From owner-freebsd-hackers Wed Jan 19 17:41:53 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from ns2.accesscom.com (ns2.accesscom.com [205.226.156.3]) by hub.freebsd.org (Postfix) with ESMTP id 3DA2F151B0 for ; Wed, 19 Jan 2000 17:41:47 -0800 (PST) (envelope-from sabrina@shell.accesscom.com) Received: from shell.accesscom.com (root@shell.accesscom.com [205.226.156.10]) by ns2.accesscom.com (8.9.3/8.9.3/Debian/GNU) with ESMTP id RAA13030; Wed, 19 Jan 2000 17:41:45 -0800 Received: (from sabrina@localhost) by shell.accesscom.com (8.9.3/8.9.3/Debian/GNU) id RAA30336; Wed, 19 Jan 2000 17:41:43 -0800 From: Sabrina Minshall Message-Id: <200001200141.RAA30336@shell.accesscom.com> Subject: PR kern/14034: gettimeofday() returns negative value? To: hackers@freebsd.org Date: Wed, 19 Jan 2000 17:41:43 -0800 (PST) Cc: sabrina@accesscom.com X-Mailer: ELM [version 2.4ME+ PL65 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG What's going one here? Successive calls to gettimeofday yields negative elapsed time? Any fixes? ~~~~negtime.c~~~~ #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 diff; if (gettimeofday(&tv1, NULL) < 0) { perror("gettimeofday"); } tv2 = tv1; for (;;) { usleep(2000); 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); assert(0); } tv2 = tv1; } return (0); } ~~~~negtime.c~~~~ hacker ~>./a.out -ve tvdiff -979993 948332324 35044 948332324 1015037 assertion "0" failed: file "negtime.c", line 56 Abort Thanks, sabrina To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message