You can read the contents of /proc/[your PID]/stat to get information for the whole process, and if you have a 2.6 kernel, there is also /proc/[your PID]/task/[thread ID]/stat with information for individual threads. (see here )
In particular, you will find these two fields:
The number of jiffies that this process has been scheduled in user mode.
stime% lu
The number of jiffies that this process has been scheduled in kernel mode.
cutime% ld
The problem part here is the unit in which the values ββare indicated. Jiffy is 1 / HZ seconds, where HZ is the core clock frequency, and determining this clock frequency is a difficult part.
If you need it only for one specific system, you can simply run some tests or look at the kernel headers and write this value to your program. If you want to learn how to define it in a more general way, you can see how the tool does it, for example, top by looking at its source code (see the old_Hertz_hack() function and related comments)
freak
source share