Skip to content

Instantly share code, notes, and snippets.

@MagerValp
Created April 19, 2016 16:36
Show Gist options
  • Select an option

  • Save MagerValp/0473c8c81dd16f0e203b4f05120d5420 to your computer and use it in GitHub Desktop.

Select an option

Save MagerValp/0473c8c81dd16f0e203b4f05120d5420 to your computer and use it in GitHub Desktop.
Get process run time
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[]) {
pid_t pid = atoi(argv[1]);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
struct kinfo_proc proc;
size_t size = sizeof(proc);
sysctl(mib, 4, &proc, &size, NULL, 0);
printf("pid %d was started %ld secs ago\n", pid, time(NULL) - proc.kp_proc.p_starttime.tv_sec);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment