[lkml]   [2002]   [Jun]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: gettimeofday problem
On Mon, 24 Jun 2002, Salvatore D'Angelo wrote:

> In this piece of code I convert seconds and microseconds in
> milliseconds. I think the problem is not in my code, in fact I wrote the
> following piece of code in Java, and it does not work too. In the for
> loop the 90% of times b > a while for 10% of times not.
> class Prova {
> public static void main(....) {
> for (;;) {
> long a = System.currentTimeMillis();
> long b = System.currentTimeMillis();
> if (a > b) {
> System.out.println("Wrong!!!!!!!!!!!!!");
> }
> }
> }
> }

This has been running since I first read your mail about 10:00 this
morning. The kernel is 2.4.18

#include <stdio.h>
#include <sys/time.h>
#define MICRO 1000000
#define ULL unsigned long long
int main(void);
static ULL tim(void);

static ULL tim()
struct timeval t;
(void)gettimeofday(&t, NULL);
return (ULL)t.tv_sec * MICRO + (ULL)t.tv_usec;
int main()
ULL a, b, cnt;
a = tim();
b = tim();
if(b < a)
printf("Failed after %llu\n", cnt);
printf("a = %llu, b = %llu\n", a, b);
return 0;

It seems to work fine. That said, I didn't use your code or check
for the possibility of a sign-change miscompare. I just made sure
I don't have any by using unsigned stuff. You may want to try
this code to see if you have a compiler (or coding) problem.

Dick Johnson

Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).

Windows-2000/Professional isn't.

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:27    [W:0.090 / U:1.600 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site