Reputation:
What is the mechanism that is used internally by the function clock() in the c standard library and other related timing functions? Can someone write a similar function in C? Or in assembly?
What knowledge is needed to write such functions manually? Is it necessary to know details about the structure of the processor?
Upvotes: 0
Views: 456
Reputation: 188
The actual implementation of clock() depends on the running kernel. Note that in full-blown Operating Systems you are restricted from accessing the hardware directly and have to use the kernel mechanics such as syscalls.
For example by looking at linux version of clock() in glibc you can see that it uses the function __times() defined in times.c which in turn uses Linux syscall.
Compare that with BSD version which is clearly different and relies on __getrusage().
In order to write such functions yourself you have to know the kernel and that usually requires you to know the details of the hardware it runs on.
Upvotes: 1