Thread: Where to find source code for functions introduced in time.h?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8

    Where to find source code for functions introduced in time.h?

    I'm trying to figure out how nanosleep() function is implemented for Linux. It is declared in time.h header located at /usr/include/ -directory. But there is only the function declaration part like this:
    Code:
    extern int nanosleep (__const struct timespec *__requested_time,
                          struct timespec *__remaining);
    Where to find the definition part?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    nanosleep is a system call, so the source of it is in the kernel sources.

    look at hrtimer.c: sys_nanosleep()

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    OK, thank you Mats! I'll try to check it out. Meanwhile, you can also help me telling which oscillator is used by nanosleep. Does it use processor cycle counter or software clock to count ticks?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It uses the "high resolution timer", which is not based directly on the cycle counter in the processor, but some external timer - it is dependant on how you configured the system as well as the hardware in your system which timer it actually uses (PM timer, HPET are some of the choices).

    Here's the "meat" of the function: http://lxr.linux.no/source/kernel/hrtimer.c#L678

    --
    Mats

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just a note: The nanosleep system call allows you to specify a nanosecond interval to sleep. It, however, doesn't mean that even under ideal circumstances that the time from the call to the return from the call is anywhere near the number of nanoseconds you've specified if you give a small number... It is the guaranteed MINIMUM time your process will sleep, but there's no real upper bound.

    --
    Mats

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jtk View Post
    OK, thank you Mats! I'll try to check it out. Meanwhile, you can also help me telling which oscillator is used by nanosleep. Does it use processor cycle counter or software clock to count ticks?
    Your question makes me suspect that you are looking for a real-time-capable delay function. In Linux there's no such thing, as Linux is not a real-time operating system.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Your question makes me suspect that you are looking for a real-time-capable delay function. In Linux there's no such thing, as Linux is not a real-time operating system.
    At least not without for example Ingo Molnar's realtime extensions, yes. Even with, I'm not entirely sure how fine-grain the timing is, for example. I don't know much at all about Ingo's real-time project...

    Linux is fine for very mild real-time on a system where you understand what is running, and you don't have any REAL hard real-time limits (e.g. the hardware will NOT stop the system from doing it's job if you are taking the interrupt that comes in every 4 milliseconds after 160 ro 200 microseconds instead of within 100 microseconds).

    --
    Mats

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    Thank you for help. So, I'm looking for a delay function that is not based on the system clock. In my program, I am using the network time protocol which manipulates the system clock frequency, but I need also a delay function which is not affected by NTP. That delay function should have precision of a microsecond.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you expect the sleep to return "within one microsecond of the specified time" then I think you should think again about using Linux as your OS (that is, if you actually REQUIRE this, rather than "it's ok as long as it does so most of the time").

    --
    Mats

  10. #10
    Registered User
    Join Date
    Aug 2007
    Location
    Finland
    Posts
    8
    I think it is sufficient that the microsecond resolution is available "most of time", not all the time. So, is nanosleep capable to do that and not affected by system clock (or NTP)? If not, what other appropriate delay functions exist?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jtk View Post
    I think it is sufficient that the microsecond resolution is available "most of time", not all the time. So, is nanosleep capable to do that and not affected by system clock (or NTP)? If not, what other appropriate delay functions exist?
    I don't know if high-res timer will be affected by NTP - it may be.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  2. starting linux's source code and kernel
    By sawer in forum Linux Programming
    Replies: 9
    Last Post: 08-01-2006, 07:46 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. where can I find source code for a binary tree?
    By binary_man in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 09:53 AM
  5. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM