patternMinor
How does a computer "wait" time?
Viewed 0 times
waittimecomputerdoeshow
Problem
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?
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?
Solution
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.
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
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 MHzWhen 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
Code Snippets
threshold = length of interval in ms / 1000 * frequency of crystal in MHzContext
StackExchange Computer Science Q#104094, answer score: 2
Revisions (0)
No revisions yet.