Date: Wed, 19 Jan 2000 17:41:43 -0800 (PST) From: Sabrina Minshall <sabrina@accesscom.com> To: hackers@freebsd.org Cc: sabrina@accesscom.com Subject: PR kern/14034: gettimeofday() returns negative value? Message-ID: <200001200141.RAA30336@shell.accesscom.com>
next in thread | raw e-mail | index | archive | help
What's going one here? Successive calls to gettimeofday
yields negative elapsed time?
Any fixes?
~~~~negtime.c~~~~
#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 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001200141.RAA30336>
