3
$\begingroup$

Me and a friend were discussing how programming languages can perform asynchronous tasks, like waiting 15 seconds before performing another task, and we started a debate.

I know that computers have an internal clock that uses the CMOS battery that can keep track of time, but how does it know it has been 15 seconds since right now and since when I told it to wait 15 seconds without looping infinitely and performing conditional operations, or does it do that?

$\endgroup$

3 Answers 3

2
$\begingroup$

If you are using an x86 based computer, see this article: https://en.wikipedia.org/wiki/High_Precision_Event_Timer Other current architectures will use similar methods.

Part of your CPU is a programmable timer. The timer increases a counter at fixed intervals, maybe every 100 nanoseconds, with very high precision. It can be programmed to raise an interrupt when certain pre-programmed points in time happen.

If you want your program to execute some code exactly 15 seconds from now, you call some operating system function which will program the programmable timer to raise an interrupt after the counter increases by exactly 1.5 billion counts = 15 seconds. 15 seconds later the interrupt happens, and the operating system will cause your code to run.

If the operating system is clever it will figure out how long it will take from the time the interrupt happens to the time your code will start running (things don't just happen instantly). If this is estimated to take 300 nanoseconds, then a clever operating system will program the timer so that the interrupt happens 300 nanoseconds earlier, so your code starts running after exactly 15 seconds.

$\endgroup$
1
  • $\begingroup$ Thank you, this is exactly what I was looking for. $\endgroup$ Commented Feb 10, 2019 at 17:05
2
$\begingroup$

In their most basic form, computers use electrical signals that are represented as 1's or 0's. A 1 meaning that there is an electrical signal and a zero meaning that there isn't an electrical signal.

A computer keeps track of time using a vibrating crystal oscillator. This crystal oscillator gives off a constant and precise frequency. The oscillator is powered by a small and weak CMOS battery on your motherboard that provides a constant electrical signal for up to three years.

So how can a computer use this oscillator along with binary (1s and 0s) to keep track of timers?

When an interval is set, a value of zero is given to the computer along with a threshold based on the length of the interval and the frequency of the oscillator.

threshold = length of interval in ms / 1000 * frequency of crystal in MHz

When the oscillator vibrates, it puts off an electrical signal (1) and triggers a binary calculation that adds 1 to to the initial binary number (the one that started at 0). Once the initial value passes the threshold, the binary function puts off a 1 indicating that the interval has been fulfilled. Depending on the operations that are done after the threshold is met, the interval may be reset, saved, altered, or deleted.

Helpful links:

https://www.youtube.com/watch?v=1gyCZIBk_88

https://www.computerhope.com/jargon/q/quartz.htm

$\endgroup$
4
  • $\begingroup$ CMOS batteries never recharge, even while your PC is turned on. This post only explains how the PC knows the current time, but how does it know time has passed? Does it loop infinitely and perform conditional checks for delta times? I couldn't imagine that a computer would infinitely do something as follows: value / 32768 >= 15. $\endgroup$ Commented Feb 10, 2019 at 2:55
  • $\begingroup$ it creates a binary threshold and increases a value until that threshold is met $\endgroup$
    – DMVerfurth
    Commented Feb 10, 2019 at 2:56
  • $\begingroup$ So you're saying that the crystal oscillator puts off an electrical signal to the computer, causing it to increase in value, and that represents delta timing? And that electrical signal causes the computer to "talk to the code" and tell it that amount of time has passed? $\endgroup$ Commented Feb 10, 2019 at 3:00
  • $\begingroup$ Yes, the computer performs binary addition until it has reached a threshold where it then sends an electrical signal to run more operations as defined in your code (but still done in binary). $\endgroup$
    – DMVerfurth
    Commented Feb 10, 2019 at 3:02
1
$\begingroup$

It depends on the specific architecture. But I think typically there is a timer interrupt that occurs every so often, so the computer can enter a low-power state and "go to sleep" and be woken up by the next timer interrupt; and if it's not time yet, it can go back to sleep.

At any time, the processor can query the clock hardware to observe what the current time is. How does that work? I think there's a simple hardware circuit that has a counter that increments each time the crystal "ticks". Thus, if you know the date and time at which this was reset to zero, by querying this counter you can learn the current time. So, the computer can query this whenever useful (including in response to an interrupt), and check whether it's time to do the scheduled task yet.

See https://superuser.com/q/253471/93541 and https://superuser.com/q/907902/93541 for lots of details on how this works.

$\endgroup$
4
  • $\begingroup$ Okay, that's great, but how does the software/hardware that performs and interrupt know its been x amount of time since when it was started? $\endgroup$ Commented Feb 10, 2019 at 2:32
  • $\begingroup$ How does my watch know what the time is? $\endgroup$
    – gnasher729
    Commented Feb 10, 2019 at 13:33
  • $\begingroup$ @JacobGunther, see updated answer. $\endgroup$
    – D.W.
    Commented Feb 10, 2019 at 18:05
  • $\begingroup$ @gnasher729 That is an entirely different topic, and doesn't relate to computer science. You should ask on Stack Overflow instead. $\endgroup$ Commented Feb 10, 2019 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.