Thread: timers implemantation

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008

    timers implemantation

    Lets say you have some programs that use a timer. Lets say that every X seconds the program sends a signal that is handled by a signal handler. Thus by a timer handler. The time is checked etc etc.
    My point is how will the program know how much time has passed? It could:
    1) Check every some milliseconds, which isn't efficient
    2) The system might send a signal to the program every X seconds. But that goes back to 1) in a way.

    You get the idea. How does the OS handle the "send a signal in X seconds" thing in an efficient way?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    I should preface this by stating that it may not be as inefficient as you think. If you have a thread that specifcally signals in set intervals, then you can simply have it keep records of all the time signalled threads and send a signal accordingly.

    Usually a system's most accurate clock is waaaaaaaay more accurate than 1ms. Yet, usually +-1ms is about as accurate as you can really trust your clock. Why? Because of the reasons you just mentioned here.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Timers are generally implemented within the OS as a queue which is sorted on "closest to expiring". The timer tick handler then processes the list by looking at the first element, if it's expired, it will set the owning process up to run next, with a "make a signal" step added to what the process should do when it "wakes up".

    Most of the time, there is no expired timers, so the check is done quite quickly, and we go on with other work in the timer tick (such as processing the "which process has done how much work" admin tasks), and finally a check for "is any process over their quanta, if so, call the scheduler".

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    There may not even be periodic checks. Because of the queue, the one pending event could be delegated to an external chip which causes a high-priority hardware interrupt at the expired time. There would be no additional overhead to the main processor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Timers
    By Deb in forum C Programming
    Replies: 16
    Last Post: 04-23-2007, 10:15 AM
  3. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  4. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM