Skip to content
Advertisement

How do the clock function works in operating systems?

I don’t understand how clock function works within an operating system.

First the documentation for clock function in www.cplusplus.com is:

Returns the processor time consumed by the program.

The value returned is expressed in clock ticks[...] 

As far as I understand, the clock function must access directly some register within the CPU that is a counter for CPU cycles. How is this possible? I mean, any register of 32 bits would overflow very soon if it is being incremented one unit at the CPU frecuency. Is the OS handling this overflow in some way?

Advertisement

Answer

“Clock ticks” are implementation-defined, not in units of the cpu clock. Historically they were fixed-length, coarse-grained scheduling timeslices. There is no need for any special hardware TSC to implement clock. It suffices to simply count up the timeslices the process is scheduled for (which are ultimately enforced by a timer interrupt or similar).

Advertisement