2002-04-09 23:24:13

by mark manning

[permalink] [raw]
Subject: nanosleep


could someone show me how to do a delay of N miliseconds using nanosleep ? (given n delay n mili seconds etc) - i cant seem to get my code to work doh!
--

_______________________________________________
Get your free email from http://www.fastermail.com

Powered by Outblaze


2002-04-10 03:22:18

by mark manning

[permalink] [raw]
Subject: Re: nanosleep

hi - yes i read the man pages, im already using nanosleep for doing 1 second delays and i chose nanosleep because a signal can interrupt the delay which might be useful (and might not :)

----- Original Message -----
From: Mark Hahn <[email protected]>
Date: Tue, 9 Apr 2002 20:32:03 -0400 (EDT)
To: mark manning <[email protected]>
Subject: Re: nanosleep


> > could someone show me how to do a delay of N miliseconds using nanosleep ?
> > (given n delay n mili seconds etc) - i cant seem to get my code to work
> > doh!
>
> did you read the fine man page, esp the section on resolution
> and busy-waits? also, using select for its timeout is probably
> more portable.
>
>

--

_______________________________________________
Get your free email from http://www.fastermail.com

Powered by Outblaze


Attachments:
(No filename) (822.00 B)

2002-04-10 04:42:01

by mark manning

[permalink] [raw]
Subject: Re: nanosleep


thanx - how much of a difference should i expect - i know the syscall is asking for at least the required ammount but that the task switcher might not give me control back for a while after the requested delay but i was expecting to be a little closer to what i had asked for - this isnt critical of corse but i would like to know what to expect.


----- Original Message -----
From: "H. Peter Anvin" <[email protected]>
Date: Tue, 09 Apr 2002 21:17:03 -0700
To: mark manning <[email protected]>
Subject: Re: nanosleep


> mark manning wrote:
> > doh - i think something is still wrong, i ask for 1000 ms and i get a second but if i do a 500 itteration loop asking for 1 ms i get 5 seconds. i am also starting to distrust my elapsed time display which is using the gettimeofday syscall
> >
>
> It doesn't work that way. Each call to nanosleep() gives you a
> *MINIMUM* time to delay. The kernel may decide to schedule you away and
> pick your process up when it suits it.
>
> -hpa
>
>
>

--

_______________________________________________
Get your free email from http://www.fastermail.com

Powered by Outblaze

2002-04-10 04:42:47

by mark manning

[permalink] [raw]
Subject: Re: nanosleep


thanx - how much of a difference should i expect - i know the syscall is asking for at least the required ammount but that the task switcher might not give me control back for a while after the requested delay but i was expecting to be a little closer to what i had asked for - this isnt critical of corse but i would like to know what to expect.


----- Original Message -----
From: "H. Peter Anvin" <[email protected]>
Date: Tue, 09 Apr 2002 21:17:03 -0700
To: mark manning <[email protected]>
Subject: Re: nanosleep


> mark manning wrote:
> > doh - i think something is still wrong, i ask for 1000 ms and i get a second but if i do a 500 itteration loop asking for 1 ms i get 5 seconds. i am also starting to distrust my elapsed time display which is using the gettimeofday syscall
> >
>
> It doesn't work that way. Each call to nanosleep() gives you a
> *MINIMUM* time to delay. The kernel may decide to schedule you away and
> pick your process up when it suits it.
>
> -hpa
>
>
>

--

_______________________________________________
Get your free email from http://www.fastermail.com

Powered by Outblaze

2002-04-10 05:48:07

by H. Peter Anvin

[permalink] [raw]
Subject: Re: nanosleep

Followup to: <[email protected]>
By author: "mark manning" <[email protected]>
In newsgroup: linux.dev.kernel
>
> thanx - how much of a difference should i expect - i know the
> syscall is asking for at least the required ammount but that the
> task switcher might not give me control back for a while after the
> requested delay but i was expecting to be a little closer to what i
> had asked for - this isnt critical of corse but i would like to know
> what to expect.
>

Read the man page:

BUGS
The current implementation of nanosleep is based on the
normal kernel timer mechanism, which has a resolution of
1/HZ s (i.e, 10 ms on Linux/i386 and 1 ms on Linux/Alpha).
Therefore, nanosleep pauses always for at least the speci?
fied time, however it can take up to 10 ms longer than
specified until the process becomes runnable again. For
the same reason, the value returned in case of a delivered
signal in *rem is usually rounded to the next larger mul?
tiple of 1/HZ s.

As some applications require much more precise pauses
(e.g., in order to control some time-critical hardware),
nanosleep is also capable of short high-precision pauses.
If the process is scheduled under a real-time policy like
SCHED_FIFO or SCHED_RR, then pauses of up to 2 ms will be
performed as busy waits with microsecond precision.

-hpa
--
<[email protected]> at work, <[email protected]> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <[email protected]>

2002-04-10 05:57:13

by mark manning

[permalink] [raw]
Subject: Re: nanosleep

hrm - im confiused now - how can you do a n NANO second delay when the resolution is 10 mili seconds ?

forgive my ignorance and my persistant "stupid" questions :)

----- Original Message -----
From: "H. Peter Anvin" <[email protected]>
Date: 9 Apr 2002 22:47:42 -0700
To: [email protected]
Subject: Re: nanosleep


> Followup to: <[email protected]>
> By author: "mark manning" <[email protected]>
> In newsgroup: linux.dev.kernel
> >
> > thanx - how much of a difference should i expect - i know the
> > syscall is asking for at least the required ammount but that the
> > task switcher might not give me control back for a while after the
> > requested delay but i was expecting to be a little closer to what i
> > had asked for - this isnt critical of corse but i would like to know
> > what to expect.
> >
>
> Read the man page:
>
> BUGS
> The current implementation of nanosleep is based on the
> normal kernel timer mechanism, which has a resolution of
> 1/HZ s (i.e, 10 ms on Linux/i386 and 1 ms on Linux/Alpha).
> Therefore, nanosleep pauses always for at least the speci
> fied time, however it can take up to 10 ms longer than
> specified until the process becomes runnable again. For
> the same reason, the value returned in case of a delivered
> signal in *rem is usually rounded to the next larger mul
> tiple of 1/HZ s.
>
> As some applications require much more precise pauses
> (e.g., in order to control some time-critical hardware),
> nanosleep is also capable of short high-precision pauses.
> If the process is scheduled under a real-time policy like
> SCHED_FIFO or SCHED_RR, then pauses of up to 2 ms will be
> performed as busy waits with microsecond precision.
>
> -hpa
> --
> <[email protected]> at work, <[email protected]> in private!
> "Unix gives you enough rope to shoot yourself in the foot."
> http://www.zytor.com/~hpa/puzzle.txt <[email protected]>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>

--

_______________________________________________
Get your free email from http://www.fastermail.com

Powered by Outblaze

2002-04-10 05:57:19

by Robert Love

[permalink] [raw]
Subject: Re: nanosleep

On Wed, 2002-04-10 at 00:41, mark manning wrote:

> thanx - how much of a difference should i expect - i know the syscall is
> asking for at least the required ammount but that the task switcher might
> not give me control back for a while after the requested delay but i was
> expecting to be a little closer to what i had asked for - this isnt critical
> of corse but i would like to know what to expect.

The minimum granularity of the timer is 1/HZ, which on a i386 is only
10ms.

If you want high-resolution timers, check out the high-res-timers
project at http://high-res-timers.sf.net/ - they implement POSIX timers
(which include a nanosleep call) with very high resolution (1/cpu
clock).

Robert Love

2002-04-10 06:01:29

by Robert Love

[permalink] [raw]
Subject: Re: nanosleep

On Wed, 2002-04-10 at 01:57, mark manning wrote:
> hrm - im confiused now - how can you do a n NANO second delay when the
> resolution is 10 mili seconds ?

Uh, you can't - that was his point.

You can try, and you will certainly sleep at least that long, but any
time given modulo 10ms is out the door ...

Like I said, check out the high resolution timers project.

Robert Love


2002-04-13 21:53:09

by andrew may

[permalink] [raw]
Subject: Re: nanosleep

On Wed, Apr 10, 2002 at 02:01:35AM -0400, Robert Love wrote:
> On Wed, 2002-04-10 at 01:57, mark manning wrote:
> > hrm - im confiused now - how can you do a n NANO second delay when the
> > resolution is 10 mili seconds ?
>
> Uh, you can't - that was his point.

Well there is a cheap trick if you catch what the man page says.

> You can try, and you will certainly sleep at least that long, but any
> time given modulo 10ms is out the door ...

Make all your calls to nanasleep be less than 2ms, and loop through as
many as you need until you are under 2ms.

Don't do it for too long because you get no other use out of your machine
while your doing this but it does work.

2002-04-14 20:37:46

by H. Peter Anvin

[permalink] [raw]
Subject: Re: nanosleep

Followup to: <[email protected]>
By author: andrew may <[email protected]>
In newsgroup: linux.dev.kernel
>
> Make all your calls to nanasleep be less than 2ms, and loop through as
> many as you need until you are under 2ms.
>
> Don't do it for too long because you get no other use out of your machine
> while your doing this but it does work.
>

If you want to busy-wait, you might as well do it in userspace (use
RDTSC.)

Maybe a future version of the kernel will have more flexible
scheduling.

-hpa

--
<[email protected]> at work, <[email protected]> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt <[email protected]>