linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* timer->result
@ 2004-09-28 18:12 Ankit Jain
  2004-09-28 18:40 ` timer->result Alphex Kaanoken
  0 siblings, 1 reply; 5+ messages in thread
From: Ankit Jain @ 2004-09-28 18:12 UTC (permalink / raw)
  To: linux prg

hi

 #include<stdio.h>
 #include<sys/time.h>
 #include<time.h>

 int main(void)
 {
         struct timeval t1,t2,result;
         gettimeofday(&t1,NULL);
         usleep(500);
         gettimeofday(&t2,NULL);
         timersub(&t2,&t1,&result);
         printf("%ld",result.tv_usec);
}
now technically i feel it should display 500
microsecond even not accurate then also it should show
near by 500 or less than 600 microsecond atleast.

on my system it shows 40000 microsecond....
whats the reason? if i remove usleep then it shows 2
or 1 microsecond.....

whats wonrg here?

thanks

ankit

________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: timer->result
  2004-09-28 18:12 timer->result Ankit Jain
@ 2004-09-28 18:40 ` Alphex Kaanoken
  0 siblings, 0 replies; 5+ messages in thread
From: Alphex Kaanoken @ 2004-09-28 18:40 UTC (permalink / raw)
  To: Ankit Jain; +Cc: linux-c-programming

On Tue, 28 Sep 2004 19:12:36 +0100 (BST)
Ankit Jain <ankitjain1580@yahoo.com> wrote:

> hi
> 
>  #include<stdio.h>
>  #include<sys/time.h>
>  #include<time.h>
> 
>  int main(void)
>  {
>          struct timeval t1,t2,result;
>          gettimeofday(&t1,NULL);
>          usleep(500);
>          gettimeofday(&t2,NULL);
>          timersub(&t2,&t1,&result);
>          printf("%ld",result.tv_usec);
> }
> now technically i feel it should display 500
> microsecond even not accurate then also it should show
> near by 500 or less than 600 microsecond atleast.
> 
> on my system it shows 40000 microsecond....
> whats the reason? if i remove usleep then it shows 2
> or 1 microsecond.....
> 
> whats wonrg here?
Linux does not real time, and this calculation very approximetly...
> 
> thanks
> 
> ankit
> 
> ________________________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping" 
> your friends today! Download Messenger Now 
> http://uk.messenger.yahoo.com/download/index.html
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


-- 
Alphex Kaanoken,
Senior Developer of
Crew IT research labs.
Web: http://crew.org.ru
Mail: kaanoken at crew.org.ru

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: timer->result
@ 2004-09-28 19:59 Huber, George K RDECOM CERDEC STCD SRI
  2004-09-29 11:22 ` timer->result Ankit Jain
  0 siblings, 1 reply; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-28 19:59 UTC (permalink / raw)
  To: 'Ankit Jain', linux prg

Ankit wrote:

> #include<stdio.h>
> #include<sys/time.h>
> #include<time.h>

> int main(void)
> {
>         struct timeval t1,t2,result;
>         gettimeofday(&t1,NULL);
>         usleep(500);
>         gettimeofday(&t2,NULL);
>         timersub(&t2,&t1,&result);
>         printf("%ld",result.tv_usec);
>}
>now technically i feel it should display 500
>microsecond even not accurate then also it should show
>near by 500 or less than 600 microsecond atleast.

Why?

A quick glance at the man page for usleep reveals:

"BUGS

 Probably not acurate on many machines down to the 
 microsecond.  Count on precision only to -4 or maybe
 -3.
"

You may want to try using  `nanosleep', but first read
the man-page, paying close attention to the BUGS section.

Also the first sentence from the DESCRIPTION section is 
particularly informative:

"nanosleep delays the exection of the program for at 
 least the time specified in *req."


Something else to consider.  Linux is a multi-tasking operating 
system, meaning that while your process is sleeping it is swapped 
out an another process executed.  As an example, consider the 
following program:

#include <time.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

/* this program must be linked with librt */

int main(int argc, char** argv)
{
   long              timediff;
   struct timespec   tpstart;
   struct timespec   tpend;
   struct timeval    tpsleep;

   tpsleep.tv_sec = 0;
   tpsleep.tv_usec = 100;

   clock_gettime(CLOCK_REALTIME, &tpstart);

   select(0, NULL, NULL, NULL, &tpsleep);

   clock_gettime(CLOCK_REALTIME, &tpend);

   timediff = MILLION*(tpend.tv_sec - tpstart.tv_sec) + 
                      (tpend.tv_nsec - tpend.tv_nsec) / 1000;

   printf("Select took %ld micorseconds\n", timediff);
}

On my system this returned the following values:

Select took 1444 micorseconds
Select took 467 micorseconds
Select took 15983 micorseconds
Select took 14589 micorseconds
Select took 8730 micorseconds
Select took 21619 micorseconds
Select took 13304 micorseconds
Select took 8764 micorseconds
Select took 21604 micorseconds
Select took 7117 micorseconds

Look at the large differences in the reported time.  I would suspect that 
for the large differences another process was swapped in and ran for its 
alotted `quanta'.  Maybe if this process was switched to a higher priority
the timing values would be different.

Just out of curosity? what are you trying to do?

George


   

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: timer->result
  2004-09-28 19:59 timer->result Huber, George K RDECOM CERDEC STCD SRI
@ 2004-09-29 11:22 ` Ankit Jain
  0 siblings, 0 replies; 5+ messages in thread
From: Ankit Jain @ 2004-09-29 11:22 UTC (permalink / raw)
  To: Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux prg

thanks for help


 --- "Huber, George K RDECOM CERDEC STCD SRI"
<George.K.Huber@us.army.mil> wrote: 
> Ankit wrote:
> 
> > #include<stdio.h>
> > #include<sys/time.h>
> > #include<time.h>
> 
> > int main(void)
> > {
> >         struct timeval t1,t2,result;
> >         gettimeofday(&t1,NULL);
> >         usleep(500);
> >         gettimeofday(&t2,NULL);
> >         timersub(&t2,&t1,&result);
> >         printf("%ld",result.tv_usec);
> >}
> >now technically i feel it should display 500
> >microsecond even not accurate then also it should
> show
> >near by 500 or less than 600 microsecond atleast.
> 
> Why?
> 
> A quick glance at the man page for usleep reveals:
> 
> "BUGS
> 
>  Probably not acurate on many machines down to the 
>  microsecond.  Count on precision only to -4 or
> maybe
>  -3.
> "
> 
> You may want to try using  `nanosleep', but first
> read
> the man-page, paying close attention to the BUGS
> section.
> 
> Also the first sentence from the DESCRIPTION section
> is 
> particularly informative:
> 
> "nanosleep delays the exection of the program for at
> 
>  least the time specified in *req."
> 
> 
> Something else to consider.  Linux is a
> multi-tasking operating 
> system, meaning that while your process is sleeping
> it is swapped 
> out an another process executed.  As an example,
> consider the 
> following program:
> 
> #include <time.h>
> #include <stdio.h>
> #include <sys/time.h>
> #include <unistd.h>
> 
> /* this program must be linked with librt */

if u can tell me the compiler and linker options for
this program..could not ru nthis program

thanks again

> 
> int main(int argc, char** argv)
> {
>    long              timediff;
>    struct timespec   tpstart;
>    struct timespec   tpend;
>    struct timeval    tpsleep;
> 
>    tpsleep.tv_sec = 0;
>    tpsleep.tv_usec = 100;
> 
>    clock_gettime(CLOCK_REALTIME, &tpstart);
> 
>    select(0, NULL, NULL, NULL, &tpsleep);
> 
>    clock_gettime(CLOCK_REALTIME, &tpend);
> 
>    timediff = MILLION*(tpend.tv_sec -
> tpstart.tv_sec) + 
>                       (tpend.tv_nsec -
> tpend.tv_nsec) / 1000;
> 
>    printf("Select took %ld micorseconds\n",
> timediff);
> }
> 
> On my system this returned the following values:
> 
> Select took 1444 micorseconds
> Select took 467 micorseconds
> Select took 15983 micorseconds
> Select took 14589 micorseconds
> Select took 8730 micorseconds
> Select took 21619 micorseconds
> Select took 13304 micorseconds
> Select took 8764 micorseconds
> Select took 21604 micorseconds
> Select took 7117 micorseconds
> 
> Look at the large differences in the reported time. 
> I would suspect that 
> for the large differences another process was
> swapped in and ran for its 
> alotted `quanta'.  Maybe if this process was
> switched to a higher priority
> the timing values would be different.
> 
> Just out of curosity? what are you trying to do?
> 
> George
> 
> 
>    
> -
> To unsubscribe from this list: send the line
> "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at 
> http://vger.kernel.org/majordomo-info.html
>  

________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: timer->result
@ 2004-09-29 14:02 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-29 14:02 UTC (permalink / raw)
  To: 'Ankit Jain', Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux prg

> Ankit wrote:
> 
> /* this program must be linked with librt */

> if u can tell me the compiler and linker options for
> this program..could not ru nthis program

I am using gcc (version 3.3.3) on a RedHat Fedora 2 box.
I was able to compile the program (assuming that its name 
is time.c) as:

gcc time.c -o time_test -lrt

George

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-29 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-28 19:59 timer->result Huber, George K RDECOM CERDEC STCD SRI
2004-09-29 11:22 ` timer->result Ankit Jain
  -- strict thread matches above, loose matches on Subject: below --
2004-09-29 14:02 timer->result Huber, George K RDECOM CERDEC STCD SRI
2004-09-28 18:12 timer->result Ankit Jain
2004-09-28 18:40 ` timer->result Alphex Kaanoken

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).