How does a program know actually what a second is?

805 views

So if a program needs to wait 10 seconds for something, how it is actually calculating it if the processors speed continously changes? (And i’m talking about offline programs that does not use NTP)

In: 361

20 Answers

Anonymous 0 Comments

The program that calculates time always knows the CPU speed as well. If you have a constant speed of, say, 1 GHz, you know it takes one second to run through a billion cycles. When the CPU is throttled to, say, 500 MHz, the program knows that now it takes only half a billion cycles until a second has elapsed. This may or may not happen very fast and all the time, but it all stays in sync.

Anonymous 0 Comments

In almost every electronic device there is something called RTC (real time clock)

It uses crystal, that oscilates at the same frequency… This is the “hardware” way

It usually looks like shiny metal component size between rice and tic-tac… Look at your motherboard, or Arduino board and you will see it

Programs don’t worry about it, operating system and bios takes care of this…

Anonymous 0 Comments

Computers have a clock that runs separately from everything else. It If it ever needs to know that time it is, it can ask that clock system.

The clock system is constant. Even if the rest of the computer makes changes in processing speeds, the clock is unaffected.

Computers also have interrupt systems. It can be scheduled to stop the process a computer is working on and have it run another process before continuing.

An interrupt can be something like pressing or releasing a keyboard key or a scheduled event that happens at a particular time.

It’s still possible for timing to get *a little* off. The clock system isn’t 1000% perfect. It can be a bit too slow or fast.

The interrupt system can also have its own problems. Most won’t be able to schedule two events at the exact time and might have to queue them up instead. (I remember in grade school 15+ years ago, you could sometimes type an entire word on the computer before the first letter appeared on screen.)

But we’re probably talking about issues that are only off by tiny fractions of a second these days. Good enough for nearly any use case.

Anonymous 0 Comments

Why does the clock on my microwave move faster than the actual clock? It’s only around 1 or 2 minutes per month but it’s noticeable.

Anonymous 0 Comments

All computers (and most electronics) have a built-in clock. It’s a tiny crystal that vibrates when you put electricity through it, like ticks of an old-timey mechanical clock. There’s a bit of electronics that counts the ticks to keep time. The computer can read the time from the clock, and programs can “set an alarm” to let know when number of ticks has passed or the clock is at a certain time.

Anonymous 0 Comments

Most programs either ask the OS “What time is it?” or tell the OS “I’m done working for now, you can put me to sleep, but please wake me up in 10 seconds.”

But how does the OS itself know? Or what if you’re writing a program that runs directly (“bare metal”), without any OS?

There’s actually a separate circuit that counts clock pulses. In some “simple” systems, those clock pulses might come from the same clock used to run the CPU. This design makes sense if the CPU always runs at the same speed, or if the speed changes “predictably” (i.e. the program is the only part of the system capable of commanding a speed change).

In systems where the CPU speed can change “unpredictably,” usually the system includes a separate clock that doesn’t change. In older PC’s (approximately 1980-2005), this was the [Intel 8253](https://en.wikipedia.org/wiki/Intel_8253), but it has been replaced by more modern [APIC](https://en.wikipedia.org/wiki/Advanced_Programmable_Interrupt_Controller) or [HPET](https://en.wikipedia.org/wiki/High_Precision_Event_Timer).

The actual source of clock pulses for high-speed applications (like a CPU clock) is almost always a [crystal oscillator](https://en.wikipedia.org/wiki/Crystal_oscillator). Simple circuits that don’t need high-speed timing can measure the charging and discharging of a capacitor instead, often with the famous [555 chip](https://en.wikipedia.org/wiki/555_timer_IC).

Anonymous 0 Comments

Your computer has a clock built in.

The clock runs on its own.

NTP is just to check the clock against a global “master” clock, so everyone has the same time. The clock in your computer runs just fine without NTP. It might be off by half a second during the year, which is where correcting it via NTP is useful, but during a day or a few seconds that small difference isn’t noticeable.

Anonymous 0 Comments

In addition to the RTC mentioned by a few people, it is important to realize that a CPU does not have a *single* speed.

It is very common for there to be one or two medium-frequency “master” clocks, which are then multiplied and divided to get a clock for the processing part, a different one for the memory interface, yet another for the PCIe bus, one for USB, and also one for timekeeping. A modern CPU will have *dozens* of clocks, all set up in a so-called “clock tree”.

When a processor’s speed changes it will *only* change the processor clock’s multiplier, while all the other clocks remain as-is.

Anonymous 0 Comments

The computer has a clock built into it which is a crystal that vibrates at a known frequency. It also has a chip which knows that every time the crystal vibrates a certain number of times, a second has passed.

Anonymous 0 Comments

Well, sort of have to tell it what a second is. Most systems have some sort of fixed-frequency side circuit that always runs at that frequency as long as it is provided energy. The computer can count the signal and declare passage of time (some number of vibrations that we tell it equals one second).

There are multiple ways to filter the signal to a fixed frequency, but a cheap and common one has always been quartz crystals due to the piezoelectric effect. They vibrate at a fixed frequency when zapped with a current (they can also make current if squeezed or deformed, but that is not what they are usually used for, although it seems to explain electrical phenomena associated with some earthquakes).

Squish a quartz crystal in the right way and electrons run away. lots of deformation and lots of quartz can make quite a bit of electrons move. Zap it with current and it vibrates at a specific frequency. A very good timekeeper as long as you provide the current.