Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using nanosecond sleep , will i able to sleep for 1 nanosecond?.
Is that much finer timer granularity is supported on linux?
Mostly timer granularity will be 10mSec(scheduler tick), so my understanding is that it is not possible to achieve timer granularity less than 10mSec.
can someone clarify please?

If suppose finer resolution is supported , my task will be put into sleep or kernel will do busy loop on behalf of my task to pass time(say 100 microsecond or 500 nanosecond)?

Can someone please clarify?

What I have tried:

If suppose finer resolution is supported , my task will be put into sleep or kernel will do busy loop on behalf of my task to pass time(say 100 microsecond or 500 nanosecond)?

Can someone please clarify?
Posted
Updated 22-Jul-16 3:04am
Comments
[no name] 22-Jul-16 8:43am    
Do you mean nanosleep, like described here nanosleep(2): high-resolution sleep - Linux man page[^]?

You cannot use timers for absolute timing in a multi-activity operating system. When a timer expires that merely means that the operating system places your activity (process) back on the execution queue, so it will resume the next time it reaches the top of the queue. This will be after 1 or more nanoseconds.

See nanosleep(2): high-resolution sleep - Linux man page[^].
 
Share this answer
 
v2
Comments
chandrAN2& 22-Jul-16 9:00am    
Thanks for your reply. I am not talking about absolute timing. My question here is kernel supports 1ns timer granularity? If so how via sleep or busyloop(bogus mips?)
Richard MacCutchan 22-Jul-16 9:52am    
I don't know the details without looking at the Linux source.
You will probably not be able to sleep for a single nano second because the available resolution depends on the granularity of the underlying clock which is system dependant. If the underlying clock has not a resolution of a nano second or less, the passed value will be by rounded up.

See also the OpenGroup specification nanosleep[^] and the nanosleep(2): high-resolution sleep - Linux man page[^].

To get the resolution of the hardware clock see clock_getres(2): clock/time functions - Linux man page[^]. Note that there is also the clock_nanosleep(2) - Linux man page[^] function which allows specification of the clock to be used.

When using sleep functions only the current thread (or the current process when it is single threaded) will be waiting. Other threads including the kernel will continue operation.
 
Share this answer
 
Comments
chandrAN2& 22-Jul-16 9:11am    
cat /proc/timers. I could see timer resolution of 1ns? Is that means 1ns resolution is supported right? If so the process will be put to sleep for 1ns ?
Jochen Arndt 22-Jul-16 9:34am    
Probably not but I'm not sure. The resolution is not the granularity of the clock.

You may look for min_delta_ns in /proc/timer_list instead.
Example output:
min_delta_ns: 15001 (Rapsberry Pi B+)
min_delta_ns: 1000 (VirtualBox on Windows 7 with Ubuntu 14.04 LTS )

The resolution reported for the Pi is 1 ns too but it definetely has not a granularity of 1ns.

A simple check is using clock_gettime(CLOCK_MONOTONIC_RAW) repeatedly until the returned value differs from the previous one. The available resolution is than the difference (it might be necessary to do this multiple times and use the lowest value to eleminate errors when the testing thread is suspended).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900