Re: nanosleep with small value

linux-os (Dick Johnson)
2005-11-17T16:55:18Z

On Thu, 17 Nov 2005, Dag Nygren wrote:

>
> Hi,
>
> seeing a strange thing happening here:
> using nanosleep() with a smallish value gives me a very long sleeptime?
>
> Is this because of a context switch being forced?
> Shouldn't the scheduler change affect that?
>
> The test program:
> ===================================
> #include <time.h>
> #include <sched.h>
> #include <stdio.h>
>
> void delay_ns(unsigned long dly)
> {
> static struct timespec time;
> int err;
> {
> time.tv_sec = 0;
> time.tv_nsec = dly;
> err = nanosleep(&time, NULL);
> if (err) {
> perror( "nanosleep failed" );
> }
> }
> }
>
>
> main()
> {
> int i;
>
> struct sched_param mysched;
> int err;
>
> if ( sched_getparam( 0, &mysched ) != 0 )
> perror( "" );
> else {
> mysched.sched_priority = sched_get_priority_max(SCHED_FIFO);
> err = sched_setscheduler(0, SCHED_FIFO, &mysched);
> if( err != 0 ) {
> fprintf (stderr,"sched_setscheduler returned: %d\n",
> err );
> perror( "" );
> }
> }
>
> for (i=0; i < 1000; i++)
> delay_ns(1000UL);
> }
> ==================================
> The result running this is:
> % time ./tst
>
> real 0m8.000s
> user 0m0.000s
> sys 0m0.000s

On an unprivilged account, I get this with
version 2.6.13.4

Script started on Thu 17 Nov 2005 11:44:47 AM EST
LINUX> time ./xxx
sched_setscheduler returned: -1
Operation not permitted

real 0m2.000s
user 0m0.000s
sys 0m0.001s
LINUX> uname -r
2.6.13.4
LINUX> exit
Script done on Thu 17 Nov 2005 11:45:07 AM EST

>From the root account where the scheduler could be set:

Script started on Thu 17 Nov 2005 11:45:29 AM EST
[root@chaos root]# time /tmp/xxx

real 0m2.001s
user 0m0.000s
sys 0m0.001s
[root@chaos root]# exit

Script done on Thu 17 Nov 2005 11:45:53 AM EST

... essentially the same thing. And 2 seconds, not 8.

The HZ value for my kernel is 1000. It you are at 100 HZ,
that might explain it.

Note that nanosleep() doesn't claim to be able to sleep
less than the resolution of some kernel timer. It just takes
parameters in seconds and nanoseconds.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.44 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.