Gettimeofday problem


 
Thread Tools Search this Thread
Top Forums Programming Gettimeofday problem
# 1  
Old 11-07-2008
Question Gettimeofday problem

i have written the code in which i want to calculate timedifference of request sent time and response receive time from device in second and microsecond. but when i executes the binary i get the response receive time earlier than request sent time which in turn returns the negative time difference.
code is given here:--
static void
this_moment(struct timeval *tv)
{
gettimeofday(tv, (void *)NULL);
}
static int
tdiff(struct timeval *after, struct timeval *before)
{
int udiff;
int sdiff;
int ret;
udiff = after->tv_usec - before->tv_usec;
sdiff = after->tv_sec - before->tv_sec;
if (udiff < 0)
{
udiff += 1000000;
sdiff -= 1;
}
return(sdiff * 1000000 + udiff);
}
int main()
{
struct timeval now1,now2;
int timedifference;
#request sent here
this_moment(&now1);
#response received here
this_moment(&now1);
timedifference=tdiff(&now2, &now1);
printf("Time elapsed for ping request is %d\n",timedifference);
}

I have used this code but i am getting -ne timedifference.
The network i used is of very high latency so getting response very quickly in 5-7 miliseconds.
Please help to to get out of this problem if some body knows about this issue.
# 2  
Old 11-07-2008
There are some other problems, but start with this move lines around, then
Code:
this_moment(&now1);
#request sent here
#response received here
this_moment(&now2);

# 3  
Old 11-07-2008
thank you jim for quick reply.....

i have tried all things replacing lines also ....but problem is still as it is....

actually i have added some loggings in the code and found that the time collected in now2 is older than now1 becouse of which i am getting -ve difference.....

here are loggings---
04-11-2008 02:44:49|Echo req sent to 152.168.158.58 at 1225734289sec : 675093microsec
04-11-2008 02:44:49|Echo resp received from 152.168.158.58 at 1225734289sec :674749microsec
04-11-2008 02:44:49|The ping time for 152.168.158.58 is = -344

I think gettimeofday is not returning the proper time.....


Please suggest ...


Thanks a lot.
# 4  
Old 11-07-2008
It's your code. Here is an example, it is not really useful.
Code:
/* tmcmd.c time a command example */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
/* time a command   usage: tmcmd <command> */
int main(int argc, char **argv)
{
	struct timeval  first,
					second,
					lapsed;
	double elapsed=0.;
	struct timezone tzp;

	gettimeofday (&first, &tzp);
	system(argv[1]);	
	gettimeofday (&second, &tzp);

	if (first.tv_usec > second.tv_usec) {
	   second.tv_usec += 1000000;
	   second.tv_sec--;
	}

	lapsed.tv_usec = second.tv_usec - first.tv_usec;
	lapsed.tv_sec  = second.tv_sec  - first.tv_sec;
	elapsed=lapsed.tv_sec + (1./(double)lapsed.tv_usec);
	printf("\n\n%s: %f\n", argv[1], elapsed);

	return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Router problem or ISP problem ?

Hi everyone, I am experiencing discontinuity of Internet service, this started 1 month ago. Everything worked very well for 1 year of intensive use, but now, I have problems reaching my gateway. The gateway is not my router but a node belonging to my ISP and I share the same public IP with... (3 Replies)
Discussion started by: remic
3 Replies

2. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

3. IP Networking

Problem with forwarding emails (SPF problem)

Hi, This is rather a question from a "user" than from a sys admin, but I think this forum is apropriate for the question. I have an adress with automatic email forwarding and for some senders (two hietherto), emails are bouncing. This has really created a lot of problems those two time so I... (0 Replies)
Discussion started by: carwe
0 Replies

4. UNIX for Dummies Questions & Answers

DHCP problem and eth1 problem

At work I am trying to get this one Linux machine (let's call it ctesgm07) to behave like another Linux machine that we have (let's call it test007). test007 returns the following version info: cat /etc/debian_version: lenny/sid uname -a: Linux test007 2.6.27-7-generic #1 SMP Tue Nov 4... (0 Replies)
Discussion started by: sllinux
0 Replies

5. Programming

smallest resolution using gettimeofday() in C

Hello ... Can somebody help telling me how smallest clock resolution on ones sytem can be in obtained in C using gettimeofday() . Thankz in advance... (4 Replies)
Discussion started by: harsha10
4 Replies

6. AIX

user login problem & Files listing problem.

1) when user login to the server the session got colosed. How will resolve? 2) While firing the command ls -l we are not able to see the any files in the director. but over all view the file system using the command df -g it is showing 91% used. what will be the problem? Thanks in advance. (1 Reply)
Discussion started by: pernasivam
1 Replies

7. Solaris

problem in finding a hardware problem

Hi I am right now facing a strange hardware problem. System get booted with the following error: Fatal Error Reset CPU 0000.0000.0000.0003 AFSR 0100.0000.0000.0000 SCE AFAR 0000.07c6.0000.1000 SC Alert: Host System has Reset It happen 4 or 5 times and get the same error every time.I... (8 Replies)
Discussion started by: girish.batra
8 Replies

8. Programming

the problem of gettimeofday

Hi all, I just wrote a small problem to check gettimeofday in a multi-cores environment. int timediff(double fTimeQvs, double fTimeTcp) { int iTimeDiff; iTimeDiff = (((((int)fTimeQvs)/10000) - (((int)fTimeTcp)/10000)) * 3600) * 1000000; iTimeDiff +=... (17 Replies)
Discussion started by: dophine
17 Replies

9. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies

10. UNIX for Advanced & Expert Users

SSH Problem auth problem

Hi, Just recently we seem to be getting the following error message relating to SSH when we run the UNIX script in background mode: warning: You have no controlling tty. Cannot read confirmation.^M warning: Authentication failed.^M Disconnected; key exchange or algorithm negotiation... (1 Reply)
Discussion started by: budrito
1 Replies
Login or Register to Ask a Question