|
Home > Archive > Unix Programming > March 2005 > I cant seem to get nanosleep to work (beginner)
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 |
I cant seem to get nanosleep to work (beginner)
|
|
| Panama Red 2005-03-27, 5:54 pm |
|
Ive screwed around for hours and read samples off the internet but I
cant seem to get nanosleep to behave like I think it should. Cam
someone tell me what is wrong with my (beginner) code?
This is on Mandrake Linux using gcc to compile...
------------------------------------------------------
#include <sys/time.h>
#include <stdlib.h>
#define NANOS 1000000
double getnsec(void);
struct timespec ts;
struct timeval tv;
int main() {
double before, after;
int seconds = 2;
ts.tv_sec = 0;
ts.tv_nsec = seconds * NANOS;
before = getnsec();
printf("%f Before\n",before);
/*
I would expect a sleep of two seconds
here. I really want to sleep for much
much less but Im trying to figure out
the behavior of nanosleep first
*/
nanosleep(&ts,NULL);
/* This returns immediately */
after = getnsec();
printf("%f After\n",after);
exit(0);
}
double getnsec(void) {
int ret;
ret = gettimeofday(&tv,NULL);
if ( ret != 0 )
{
perror("gettimeofday");
}
return (tv.tv_sec + tv.tv_usec);
}
--------------------------------------------------------
[chris@localhost time]$ ./a.out
1112592100.000000 Before
1112595401.000000 After
--
-----------------------------------------------------------------------------
Usenet Poster
usenet@gnubin.com
-----------------------------------------------------------------------------
| |
| Måns Rullgård 2005-03-27, 5:54 pm |
| Panama Red <complaintdepartment2002@yahoo.com> writes:
> Ive screwed around for hours and read samples off the internet but I
> cant seem to get nanosleep to behave like I think it should. Cam
> someone tell me what is wrong with my (beginner) code?
>
> This is on Mandrake Linux using gcc to compile...
>
> ------------------------------------------------------
> #include <sys/time.h>
> #include <stdlib.h>
>
> #define NANOS 1000000
That's three zeros short of a nano.
> double getnsec(void);
> struct timespec ts;
> struct timeval tv;
>
> int main() {
> double before, after;
> int seconds = 2;
> ts.tv_sec = 0;
> ts.tv_nsec = seconds * NANOS;
> before = getnsec();
> printf("%f Before\n",before);
> /*
> I would expect a sleep of two seconds
> here. I really want to sleep for much
> much less but Im trying to figure out
> the behavior of nanosleep first
> */
> nanosleep(&ts,NULL);
Hint: man nanosleep
The value of the nanoseconds field must be in the range 0 to 999999999.
--
Måns Rullgård
mru@inprovide.com
| |
| Gianni Mariani 2005-03-27, 5:54 pm |
| Panama Red wrote:
> Ive screwed around for hours and read samples off the internet but I
> cant seem to get nanosleep to behave like I think it should. Cam
> someone tell me what is wrong with my (beginner) code?
It's more likely that the resolution of the system is not better than
1/100 sec.
getitimer/setitimer might be better. (I have not used real-time support
on Linux so I'm at the limit of my knowledge here.).
>
> This is on Mandrake Linux using gcc to compile...
>
> ------------------------------------------------------
> #include <sys/time.h>
> #include <stdlib.h>
>
> #define NANOS 1000000
BTW - NANOS is 1000000000
(that means that 3 nanos will overflow !).
>
> double getnsec(void);
> struct timespec ts;
> struct timeval tv;
>
> int main() {
> double before, after;
> int seconds = 2;
> ts.tv_sec = 0;
> ts.tv_nsec = seconds * NANOS;
(I think ts.tv_nsec will need to be less than one second's worth of NANOS).
> before = getnsec();
> printf("%f Before\n",before);
> /*
> I would expect a sleep of two seconds
> here. I really want to sleep for much
> much less but Im trying to figure out
> the behavior of nanosleep first
> */
> nanosleep(&ts,NULL);
> /* This returns immediately */
> after = getnsec();
> printf("%f After\n",after);
>
> exit(0);
> }
>
> double getnsec(void) {
> int ret;
> ret = gettimeofday(&tv,NULL);
> if ( ret != 0 )
> {
> perror("gettimeofday");
> }
>
> return (tv.tv_sec + tv.tv_usec);
> }
>
> --------------------------------------------------------
>
> [chris@localhost time]$ ./a.out
> 1112592100.000000 Before
> 1112595401.000000 After
>
>
| |
| Panama Red 2005-03-27, 5:54 pm |
| I believe it was Måns Rullgård who said...
>
> That's three zeros short of a nano.
>
> Hint: man nanosleep
>
> The value of the nanoseconds field must be in the range 0 to 999999999.
Ok, heres the corrected code that seems to work better, but I still
have trouble determining how long I have slept with gettimeofday()
-------------------------------------------------
#include <sys/time.h>
#include <stdlib.h>
double getnsec(void);
struct timespec ts;
struct timeval tv;
int main() {
double before, after;
int milliseconds = 500;
ts.tv_sec = 0;
ts.tv_nsec = milliseconds * 1000000;
before = getnsec();
printf("%f Before\n",before);
int i;
for ( i = 0 ; i < 10 ; i++ ) {
nanosleep(&ts,NULL);
}
after = getnsec();
printf("%f After\n",after);
printf("%f\n",after - before);
exit(0);
}
double getnsec(void) {
int ret;
ret = gettimeofday(&tv,NULL);
if ( ret != 0 )
{
perror("gettimeofday");
}
return (tv.tv_sec + tv.tv_usec);
}
-----------------------------------------------------
[chris@localhost time]$ ./a.out
1112114135.000000 Before
1112133279.000000 After
19144.000000
I dont understand the 19144 number. I would have expected about
5000000 microseconds.
--
-----------------------------------------------------------------------------
Usenet Poster
usenet@gnubin.com
-----------------------------------------------------------------------------
| |
| Måns Rullgård 2005-03-27, 5:54 pm |
| Panama Red <complaintdepartment2002@yahoo.com> writes:
> I believe it was Måns Rullgård who said...
>
> Ok, heres the corrected code that seems to work better, but I still
> have trouble determining how long I have slept with gettimeofday()
>
> -------------------------------------------------
> #include <sys/time.h>
> #include <stdlib.h>
>
> double getnsec(void);
> struct timespec ts;
> struct timeval tv;
>
> int main() {
>
> double before, after;
> int milliseconds = 500;
>
> ts.tv_sec = 0;
> ts.tv_nsec = milliseconds * 1000000;
OK, you're within range this time, but only as long as you stay lower
than 1 second.
> before = getnsec();
>
> printf("%f Before\n",before);
> int i;
> for ( i = 0 ; i < 10 ; i++ ) {
> nanosleep(&ts,NULL);
> }
> after = getnsec();
> printf("%f After\n",after);
> printf("%f\n",after - before);
> exit(0);
> }
>
> double getnsec(void) {
> int ret;
> ret = gettimeofday(&tv,NULL);
> if ( ret != 0 )
> {
> perror("gettimeofday");
> }
>
> return (tv.tv_sec + tv.tv_usec);
You forgot to multiply tv_sec by 1000000.
> }
> -----------------------------------------------------
>
> [chris@localhost time]$ ./a.out
> 1112114135.000000 Before
> 1112133279.000000 After
> 19144.000000
>
> I dont understand the 19144 number. I would have expected about
> 5000000 microseconds.
--
Måns Rullgård
mru@inprovide.com
| |
| Panama Red 2005-03-27, 5:54 pm |
| I believe it was Måns Rullgård who said...
> Panama Red <complaintdepartment2002@yahoo.com> writes:
>
> You forgot to multiply tv_sec by 1000000.
Thanks!
--
-----------------------------------------------------------------------------
Usenet Poster
usenet@gnubin.com
-----------------------------------------------------------------------------
| |
| David Schwartz 2005-03-27, 8:47 pm |
|
"Panama Red" <complaintdepartment2002@yahoo.com> wrote in message
news:4a4jh2-6tr.ln1@ralph.homelinux.net...
> return (tv.tv_sec + tv.tv_usec);
What is the supposed to do? If it's 3:39AM, what good does it do to add
the "3" to the "39" and call the time "42"? 42 what?
DS
| |
| Panama Red 2005-03-28, 8:50 pm |
| I believe it was David Schwartz who said...
>
> "Panama Red" <complaintdepartment2002@yahoo.com> wrote in message
> news:4a4jh2-6tr.ln1@ralph.homelinux.net...
>
>
> What is the supposed to do? If it's 3:39AM, what good does it do to add
> the "3" to the "39" and call the time "42"? 42 what?
>
I dont know....I was hoping youd tell me.
--
-----------------------------------------------------------------------------
Usenet Poster
usenet@gnubin.com
-----------------------------------------------------------------------------
| |
| David Schwartz 2005-03-29, 2:49 am |
|
"Panama Red" <complaintdepartment2002@yahoo.com> wrote in message
news:qgqmh2-juv.ln1@ralph.homelinux.net...
>I believe it was David Schwartz who said...
>
> I dont know....I was hoping youd tell me.
IMO, the best way to get the elapsed time between two intervals is more
or less like this:
struct timeval before, after;
gettimeofday(&before, NULL);
do_stuff();
gettimeofday(&after, NULL);
elapsed_time_msec=(after.tv_sec-before.tv_sec)*1000+(after.tv_usec-before.tv_usec)/1000;
DS
| |
| Panama Red 2005-03-29, 2:49 am |
| I believe it was David Schwartz who said...
>
> IMO, the best way to get the elapsed time between two intervals is more
> or less like this:
>
>
> gettimeofday(&before, NULL);
> do_stuff();
> gettimeofday(&after, NULL);
>
> elapsed_time_msec=(after.tv_sec-before.tv_sec)*1000+(after.tv_usec-before.tv_usec)/1000;
Thanks, will give it a try
--
-----------------------------------------------------------------------------
Usenet Poster
usenet@gnubin.com
-----------------------------------------------------------------------------
| |
| Fabien R 2005-03-29, 2:49 am |
| Panama Red <complaintdepartment2002@yahoo.com> wrote in message news:<t6rih2-kkr.ln1@ralph.homelinux.net>...
> Ive screwed around for hours and read samples off the internet but I
> cant seem to get nanosleep to behave like I think it should. Cam
> someone tell me what is wrong with my (beginner) code?
>
> This is on Mandrake Linux using gcc to compile...
>
> ------------------------------------------------------
> #include <sys/time.h>
> #include <stdlib.h>
>
> #define NANOS 1000000
>
> double getnsec(void);
> struct timespec ts;
> struct timeval tv;
>
> int main() {
> double before, after;
> int seconds = 2;
> ts.tv_sec = 0;
> ts.tv_nsec = seconds * NANOS;
If you want nanosleep to wait for 2 seconds, you have to set
ts.tv_sec = 2
-
Fabien
|
|
|
|
|