Home > Archive > Unix Programming > September 2006 > nanosleep does not sleep at all!
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 |
nanosleep does not sleep at all!
|
|
| qazmlp1209@rediffmail.com 2006-09-26, 7:00 pm |
| nanosleep does not seem to work. I have given a delay of 100 msec
i.e.0.1 sec. But, the below program prints always 0. I wonder whether
nanosleep work at all. Can somebody help?
---------------
timespec timespecObj ;
timespecObj.tv_sec = 0.1 ;
timespecObj.tv_nsec = 0 ;
timeval firstTime, secondTime;
void *tempPtr=NULL;
long locSec, locMilliSec, locMilliSec2, firstSec, secondSec,
timediff;
gettimeofday( &firstTime, tempPtr );
locSec = firstTime.tv_sec;
locMilliSec = firstTime.tv_usec;
firstSec = locMilliSec/1000 + locSec * 1000;
nanosleep( ×pecObj , NULL ) ;
tempPtr=NULL;
gettimeofday( &secondTime, tempPtr );
locSec = secondTime.tv_sec;
locMilliSec2 = secondTime.tv_usec;
secondSec = locMilliSec2/1000 + locSec * 1000;
timediff= secondSec-firstSec;
printf("Time in milliseconds is %d\n", timediff);
---------------
Here is the solaris version information:
$more /etc/release
Solaris 8 10/00 s28s_u2wos_11b SPARC
| |
| Wayne C. Morris 2006-09-26, 7:00 pm |
| In article <1159287418.461072.120370@h48g2000cwc.googlegroups.com>,
qazmlp1209@rediffmail.com wrote:
> nanosleep does not seem to work. I have given a delay of 100 msec
> i.e.0.1 sec. But, the below program prints always 0. I wonder whether
> nanosleep work at all. Can somebody help?
> ---------------
> timespec timespecObj ;
> timespecObj.tv_sec = 0.1 ;
tv_sec is an integer. You're setting it to zero.
Note that gettimeofday is limited to the resolution of the hardware clock
in your computer. If it only counts time in seconds, that'll be the
smallest interval of time you can measure with gettimeofday. Similarly,
nanosleep can't pause for a smaller time interval than that allowed by the
hardware timer(s) in your computer.
| |
| Renato Golin 2006-09-27, 8:00 am |
| qazmlp1209@rediffmail.com wrote:
> nanosleep does not seem to work. I have given a delay of 100 msec
> i.e.0.1 sec. But, the below program prints always 0. I wonder whether
> nanosleep work at all. Can somebody help?
> ---------------
> timespec timespecObj ;
> timespecObj.tv_sec = 0.1 ;
> timespecObj.tv_nsec = 0 ;
timespecObj.tv_sec = 0 ;
timespecObj.tv_nsec = 100 * 1000000 ;
But Wayne is right, you won't be able to sleep in smaller intervals than
your clock. I suggest you use select instead of nanosleep.
cheers,
--renato
| |
| Rainer Weikusat 2006-09-27, 8:00 am |
| Renato Golin <rengolin@gmail.com> writes:
> qazmlp1209@rediffmail.com wrote:
>
> timespecObj.tv_sec = 0 ;
> timespecObj.tv_nsec = 100 * 1000000 ;
>
> But Wayne is right, you won't be able to sleep in smaller intervals
> than your clock. I suggest you use select instead of nanosleep.
And this is going to change the clock resolution precisely how?
| |
| Renato Golin 2006-09-27, 8:00 am |
| Rainer Weikusat wrote:
> Renato Golin <rengolin@gmail.com> writes:
>
> And this is going to change the clock resolution precisely how?
I didn't say it would.
I just said: use select instead of nanosleep as a suggestion, not that
it would change any hardware crystal's physical properties.
Btw, if you find one please let me know, I need to replace my old atomic
clock... ;)
--renato
| |
| qazmlp1209@rediffmail.com 2006-09-27, 8:00 am |
|
Renato Golin wrote:
> qazmlp1209@rediffmail.com wrote:
>
> timespecObj.tv_sec = 0 ;
> timespecObj.tv_nsec = 100 * 1000000 ;
Thanks! It was a silly mistake from my side. But, there seems to be lot
of web links, where they have given such examples. Anyway, I will
ignore them and use it as you have mentioned.
> But Wayne is right, you won't be able to sleep in smaller intervals than
> your clock. I suggest you use select instead of nanosleep.
I want to use it for the multi-threaded program. Is 'select' MT-safe in
Solaris implementation?
Also,
sleep() - will make the complete process to sleep.
nanosleep() - will make the current thread to sleep.
select() - ???
| |
| Renato Golin 2006-09-27, 8:00 am |
| qazmlp1209@rediffmail.com wrote:
> I want to use it for the multi-threaded program. Is 'select' MT-safe in
> Solaris implementation?
>
> Also,
> sleep() - will make the complete process to sleep.
> nanosleep() - will make the current thread to sleep.
> select() - ???
I don't know in Solaris but select and nanosleep are usually thread-safe.
nanosleep is POSIX and should be available (and similarly implemented on
all unices) and select is original from BSD and ported later to other
unices but the support is quite uniform and well implemented.
Both should make your thread (and not your program) sleep.
cheers,
--renato
| |
| Nils O. Selåsdal 2006-09-27, 8:00 am |
| qazmlp1209@rediffmail.com wrote:
> Renato Golin wrote:
> Thanks! It was a silly mistake from my side. But, there seems to be lot
> of web links, where they have given such examples. Anyway, I will
> ignore them and use it as you have mentioned.
>
> I want to use it for the multi-threaded program. Is 'select' MT-safe in
> Solaris implementation?
>
> Also,
> sleep() - will make the complete process to sleep.
> nanosleep() - will make the current thread to sleep.
> select() - ???
On systems supporting posix threads, all these functions
suspend only the calling thread.
| |
| Casper H.S. Dik 2006-09-27, 8:00 am |
| Rainer Weikusat <rainer.weikusat@sncag.com> writes:
[color=darkred]
>And this is going to change the clock resolution precisely how?
It doesn't; select can actually be worse than nanosleep.....
In Solaris, we allow for two default settings of the system clock
100Hz and 1000Hz; you can actually also set the clock to higher values;
prior to Solaris 10, select was based on poll() which only allows
for 1ms resolution and because of clock jitter you will always
need to sleep at least two ticks, or [10-20)ms by default.
With a 10kHz clock frequency, poll will do no better than 2ms; but
nanosleep can return more quickly.
Solaris 10 select is different again because it's based on syspoll()
which takes a struct timespec as interval.
In any case, if you have nanosleep() there's no reason to expect select
to have a finer grained sleep time; and I cite Solaris here as an example
where select() can give worse behaviour.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Casper H.S. Dik 2006-09-27, 8:00 am |
| qazmlp1209@rediffmail.com writes:
>sleep() - will make the complete process to sleep.
Why do you think that? Sleep() only suspends the calling thread,
not the calling process. (Any wording to the contrary such as
"syspends the calling process" is left over from the prethreading
era and is a manual page bug)
In Solaris 10, sleep calls nanosleep().
>nanosleep() - will make the current thread to sleep.
Yes.
>select() - ???
May behave more poorly than nanosleep(); select() is only advisable
in case you run on a system w/o either usleep() or nanosleep.
Solaris has both; nanosleep is standard so use that.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Renato Golin 2006-09-27, 8:00 am |
| Casper H.S. Dik wrote:
> In any case, if you have nanosleep() there's no reason to expect select
> to have a finer grained sleep time; and I cite Solaris here as an example
> where select() can give worse behaviour.
That was my point, he don't need (at least not in his example) a sleep
for less than 100ms, so both select and nanosleep will yield similar
results.
--renato
| |
| Nils O. Selåsdal 2006-09-27, 8:00 am |
| Casper H.S. Dik wrote:
> qazmlp1209@rediffmail.com writes:
>
>
> Why do you think that?
My guess is because the (outdated and wrong) linux
manpage says so :-)
| |
| Rick Jones 2006-09-27, 7:02 pm |
| In comp.unix.programmer Renato Golin <rengolin@gmail.com> wrote:
> But Wayne is right, you won't be able to sleep in smaller intervals
> than your clock. I suggest you use select instead of nanosleep.
If all that is being done is sleeping, it is far (IMO) better to use a
call that was meant just for sleeping than overloading on one that was
meant for other things. Even if, over the years the overloaded's call
path has been tweaked to recognize the "only sleep" part.
I suppose the same principle is behind why I prefer to use gethrtime()
to gettimeofday() to perform "how long did foo take" measurements.
When measuring an interval, you don't care about wall-clock, or being
sync'ed to "real" time, which means much of what happens in
gettimeofday() is a don't care you don't really want.
rick jones
--
web2.0 n, the dot.com reunion tour...
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
|
|
|
|
|