Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    C Sleep() Underlying Implementation

    Hello Community,

    I am sorry to come to you with such a specific and obscure question. But I am afraid I just don't know where I might find the answer to something so unusual.

    My question is how the sleep() command (in standard C), and commands like it (wait() for instance) are implemented in underlying *nix systems; specifically, how they might affect the Last Level Cache and what they do differently from an ordinary context switch.

    Contextually, I have developed a program that records cache access times (notes hits and misses) at the highest level (ignores the L1 caches, as a context switch would wipe it anyway). It receives hits on consecutive accesses to the same memory space when it is the only program in execution, and likewise gets hits when the program invokes (and completes) other processes in between consecutive accesses (using fork() and execl() to invoke new processes, with a busy-counter to wait for it to finish).

    The strange thing, is that if I replace the busy-counter with a sleep() or wait() command (as would be the proper way to implement such a thing) I get zero cache hits, as though the entire cache was flushed. I am aware that a L1 cache is flushed on a context switch, but, as far as I know, a PIPT L2 cache is explicitly not supposed to, and my busy-counter example suggests that it does not.

    There is some difference I don't understand between the sleep() example and the busy-counter, which baffles me. Any insight into what is going on down there would be much appreciated.


  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    See what happens when you use an itimer (interval timer) instead of sleep()/wait(), etc. I don't know the answer to your question, but if that also seems to flush the cache, then perhaps that will answer your question. It would be my best guess (without looking at kernel code), that sleep() utilizes itimer functions under the covers.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts