system clock

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Hello,

I am getting confused in reading a paragraph I have read but I do not understand completely. Can anyone explain for me what is described in this paragraph

The system clock is typically implemented as a programmable interval timer that periodically interrupts the CPU, which then starts executing a timer interrupt service routine. This routine typically adds one tick to the system clock (a simple counter) and handles other periodic housekeeping tasks (preemption, etc.) before returning to the task the CPU was executing before the interruption.
CPU takes time to execute instructions written in program. I guess system clock is related to time.
 

DickCappels

Joined Aug 21, 2008
10,661
This relates to multitasking in which a timer periodically interrupts whatever task is being executed at the moment and hands control to the task scheduler which switches control to another task, thus the active tasks get their turn to operate during their respective slices of time.
 

Ya’akov

Joined Jan 27, 2019
10,226
Hello,

I am getting confused in reading a paragraph I have read but I do not understand completely. Can anyone explain for me what is described in this paragraph



CPU takes time to execute instructions written in program. I guess system clock is related to time.
The system clock is two parts: hardware and software.

The hardware component is the PIT (Programmable Interval Timer), it generates a hardware interrupt at some fixed interval. It is independent of the CPU hardware, so it can't be interfered with by any running programs.

The software component is the operating system's timer ISR (Interrupt Service Routine). An ISR is the code that runs when a particular hardware interrupt fires. In this case, the PIT-generated interrupt, attached to the timer ISR.

When the timer ISR is executed it does time critical tasks before returning control to the OS. It updates the system clock, which is a counter that starts at 0 and counts up one tick for every PIT interval (like the output of millis()in the Arduino).

It also does housekeeping tasks related to time critical code found in multitasking operating systems.

The confusion might be because the hardware/software combination is called the system clock and so is the counter that is updated by the ISR each time the PIT fires it.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
It means there is a single system timer interrupt. It keeps track of system time.

I don't understand what happens when timer interrupt occurred. This is an event generated by the program, What action is taken on it
 

geekoftheweek

Joined Oct 6, 2013
1,429
The timer interrupt is independent of the program. When the timer interrupt occurs, the system finishes execution of the current instruction, saves the location of the next instruction, and then goes to the address of the interrupt routine. What actually happens at that point is up to whatever the programmer wants to happen. It could increment a counter, change a pin, start some sort of other peripheral, or whatever else. After the interrupt routine is complete the system then goes back to the saved instruction address and continues on with the rest of the program.

In the case of a system clock what could happen is the interrupt routine sets a flag that is caught on the next time through the main program loop and then modifies the time. It could also be the case (especially with microcontrollers) that the clock is totally updated in the interrupt routine. It all depends on how much time the system can spend in the interrupt routine versus handling other interrupts and normal program execution.
 
Top