Unix Programming - converting into milliseconds

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2006 > converting into milliseconds





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author converting into milliseconds
Roman Mashak

2006-09-20, 1:32 am

Hello,

I've been researching the code from R.Stevens book "UNIX network
programming, 2nd edition". The code is on the page 668 (chapter about raw
sockets). What I'm confused is the way he converts value from microseconds
to milliseconds:

....
tvsend = (struct timeval *) icmp->icmp_data;
tv_sub(tvrecv, tvsend);
rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;
....

Because "milli" means 10e-3, I think it should be:

rtt = tvrecv->tv_sec / 1000.0 + tvrecv->tv_usec * 1000.0;

Please correct me if I'm wrong.

Thanks.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Erik Max Francis

2006-09-20, 1:32 am

Roman Mashak wrote:

> I've been researching the code from R.Stevens book "UNIX network
> programming, 2nd edition". The code is on the page 668 (chapter about raw
> sockets). What I'm confused is the way he converts value from microseconds
> to milliseconds:
>
> ...
> tvsend = (struct timeval *) icmp->icmp_data;
> tv_sub(tvrecv, tvsend);
> rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;
> ...
>
> Because "milli" means 10e-3, I think it should be:


It's 10^-3 or 1e-3, but yeah.

> rtt = tvrecv->tv_sec / 1000.0 + tvrecv->tv_usec * 1000.0;
>
> Please correct me if I'm wrong.


Yes, you're right. The code sample in the book is incorrect.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Who, my friend, can scale heaven?
-- _Gilgamesh_, ca. 3rd C. BC
Ralf Fassel

2006-09-20, 7:34 am

* "Roman Mashak" <mrv@tusur.ru>
| rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;

One second is 1000 milliseconds, one Microsecond is 1/1000
millisecond, so the above arithmetics seems ok to me if you want to
know how many milliseconds are in the tvrecv struct.

R'
Erik Max Francis

2006-09-20, 7:34 am

Ralf Fassel wrote:

> One second is 1000 milliseconds, one Microsecond is 1/1000
> millisecond, so the above arithmetics seems ok to me if you want to
> know how many milliseconds are in the tvrecv struct.


Yeah, you're right. We were both thinking about it the opposite way.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
It's a winter day / Five years too late
-- En Vogue
Roman Mashak

2006-09-20, 7:34 am

Hello, Ralf!
You wrote on Wed, 20 Sep 2006 10:25:47 +0200:

RF> * "Roman Mashak" <mrv@tusur.ru>
??|> rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;

RF> One second is 1000 milliseconds, one Microsecond is 1/1000
RF> millisecond, so the above arithmetics seems ok to me if you want to
RF> know how many milliseconds are in the tvrecv struct.
Exactly! Why did I get stuck in such a trivial case?
Thanks a lot for elemenary school class

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Ralf Fassel

2006-09-20, 7:34 am

* "Roman Mashak" <mrv@tusur.ru>
| Exactly! Why did I get stuck in such a trivial case?

Once you got it wrong in production code, you'll remember it for the
rest of your career ;-)

R'
David Schwartz

2006-09-20, 7:52 pm


Roman Mashak wrote:

> ...
> tvsend = (struct timeval *) icmp->icmp_data;
> tv_sub(tvrecv, tvsend);
> rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;
> ...


The simplest way to check is whether or not it converts both tv_sec and
tv_usec into the same units, because otherwise it makes no sense to add
them.

By multiplying seconds by 1000, you turn 1 second into 1000
milliseconds, which is correct. Similarly, this turns 8 seconds into
8000 milliseconds, against, it seems correct.

> Because "milli" means 10e-3, I think it should be:
>
> rtt = tvrecv->tv_sec / 1000.0 + tvrecv->tv_usec * 1000.0;
>
> Please correct me if I'm wrong.


This would turn 2 microseconds into 2000 milliseconds, or two seconds.
Clearly two microseconds is not the same as 2 seconds. So this
"correction" is wrong.

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com