1833779 Members
2297 Online
110063 Solutions
New Discussion

nanosleep granularity

 
Massimo Brandini
New Member

nanosleep granularity

Hi all,

some collegues of mine are having troubles in getting a granularity finer than 10 ms when using the system call nanosleep, they need to be able to make a process (eventually a thread) wait for a very precise amount of time.
Is there some system parameter to change for allowing intervals smaller than 10 ms? (without affecting the other system components ;-)

Many thanks in advance
4 REPLIES 4
rick jones
Honored Contributor

Re: nanosleep granularity

i'm reasonably certain that 10ms is the limit at the point in time.
there is no rest for the wicked yet the virtuous have no pillows
A. Clay Stephenson
Acclaimed Contributor

Re: nanosleep granularity

Actually this is hopeless. Even if you were able to change the miminum timing resolution there is still no guarantee that nanosleep will
suspend the thread for a precise amount of time. A process (thread) with higher priority,
signals, and other scheduling activities could clobber any attempt at precise delays. You would have better luck running at real-time priority but that is only done under very special circumstances. Remember, this is UNIX; it's really not a good fit for applications relying upon very precise timing and scheduling. Your programmers really need to rethink the problem and come up with a better synchronization scheme.

Regards, Clay
If it ain't broke, I can fix that.
rick jones
Honored Contributor

Re: nanosleep granularity

that precise timing may not have been for synchronization. it could have been for pacing of data or some such. indeed, if it was for synchronization, the programmers need to be re-educated.

i suppose that some sort of loop with gettimeofday() and sched_yield() could allow a thread to perform other actions with greater timing precision, but the overhead would be (likely) considerable
there is no rest for the wicked yet the virtuous have no pillows
Klaus Crusius
Trusted Contributor

Re: nanosleep granularity


The restriction of 10ms comes from the internal clock resolution seen by the kernel. See the constant CLK_TCK = 100 (ticks per second) in /usr/include/sys/time.h.

Klaus
There is a live before death!