Reputation: 9214
On linux I can use /proc
as stated in How to calculate the CPU usage of a process by PID in Linux from C? to get the cpu time of a process and it children.
How would I do this on OS X?
Upvotes: 1
Views: 279
Reputation: 27611
You can get process information using sysctl. So, let's assume that you have the pid for a process: -
#include <sys/sysctl.h>
struct kinfo_proc *getProcessInfo(pid_t pid)
{
struct kinfo_proc* list = NULL;
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
size_t size = 0;
sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size, NULL, 0);
list = (kinfo_proc*)malloc(size);
sysctl(mib, sizeof(mib) / sizeof(*mib), list, &size, NULL, 0);
return list;
}
Remember to check for errors returned from sysctl. I've left them out for brevity and don't forget to free the returned structure when you're finished with it.
The returned kinfo_proc structure contains a structure extern_proc, which you will see has the following attributes: -
struct extern_proc {
union {
struct {
struct proc *__p_forw; /* Doubly-linked run/sleep queue. */
struct proc *__p_back;
} p_st1;
struct timeval __p_starttime; /* process start time */
} p_un;
....
}
__p_starttime, is what you're looking for.
Upvotes: 1