简体   繁体   中英

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?

"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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM