-1

In order to calculate time we usually use a system call, but what if I wanted to implement it myself, is it possible? Using rdtsc gives me the amount of cpu clocks from the time we turned it on. It is still not possible to calculate the time since we need the cpu frequency for this purpose. The basic problem is that it looks to me that not only the cpu frequency that I measure is different from what I see in /proc/cpuinfo but it also can change over time (overclocking and underclocking). So how is the system call implemented?

The solution framework I'm looking for is to calculate the time by looking at some initial time t0, the amount of cpu clocks since t0 (using rdtsc) and the cpu frequency. Something like

t1 = t0 + (rdtsc1 - rdtsc0) / frequency
e271p314
  • 3,841
  • 7
  • 36
  • 61

1 Answers1

1

Most computers have a real-time clock separate from the CPU, e.g. in the "BIOS." The interrupt code for the real-time clock is 0x1A as you can see here: http://en.wikipedia.org/wiki/BIOS_interrupt_call

So if you were to implement something like time() you could do it simply by invoking the 0x1A interrupt to query the real-time clock. Of course this is somewhat platform-dependent.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • As far as I understand the interrupt 0x1A will take roughly the same as the interrupt 0x80 which is used for the time system call. They both will cause a context switch, I'd like to avoid it. – e271p314 Apr 29 '14 at 14:31