As documented in proc(5), in the file /proc/(pid)/stat
you have the fields:
utime %lu
Amount of time that this process has been scheduled in
user mode, measured in clock ticks
(divide by sysconf(_SC_CLK_TCK). This
includes guest time, guest_time (time
spent running a virtual CPU, see
below), so that applications that are
not aware of the guest time field do
not lose that time from their
calculations.
stime %lu
Amount of time that this
process has been scheduled in kernel
mode, measured in clock ticks (divide
by sysconf(_SC_CLK_TCK).
To get CPU usage for a specific process, use those fields. The toplevel process will aggregate CPU usage over all threads; for a per-thread breakdown, you can find the other threads in /proc/(pid)/task
.
If you would prefer to be notified when CPU time exceeds some threshold, you can use clock_getcpuclockid to get a handle to its cpu time clock, then timer_create or timerfd to be notified when it hits a specified level. However, note that cross-process cputime timers are an optional feature in the POSIX specification and may not be supported (I've not tested).