static double get_create_time(pid_t pid) { FILE *fp = NULL; char procfile[50]; char s_pid[10]; char *tmp; double ct_jiffies; double ct_seconds; double clock_ticks = sysconf(_SC_CLK_TCK); long boot_time = get_boot_time(); sprintf(s_pid, "%d", pid); sprintf(procfile, "/proc/%d/stat", pid); fp = fopen(procfile, "r"); check(fp, "Couldn't open process stat file"); tmp = grep_awk(fp, s_pid, 21, " "); ct_jiffies = atof(tmp); free(tmp); fclose(fp); ct_seconds = boot_time + (ct_jiffies / clock_ticks); return ct_seconds; error: if (fp) fclose(fp); return -1; }
static pid_t get_ppid(pid_t pid) { FILE *fp = NULL; pid_t ppid = -1; char *tmp; char procfile[50]; sprintf(procfile, "/proc/%d/status", pid); fp = fopen(procfile, "r"); check(fp, "Couldn't open process status file"); tmp = grep_awk(fp, "PPid", 1, ":"); ppid = tmp ? atoi(tmp) : -1; check(ppid != -1, "Couldnt' find PPid in process status file"); fclose(fp); free(tmp); return ppid; error: if (fp) fclose(fp); return -1; }
static unsigned int get_ppid(unsigned pid) { FILE *fp = NULL; unsigned int ppid = -1; char *tmp; char procfile[50]; sprintf(procfile,"/proc/%d/status", pid); fp = fopen(procfile,"r"); check(fp, "Couldn't open process status file"); tmp = grep_awk(fp, "PPid", 1, ":"); ppid = tmp?strtoul(tmp, NULL, 10):-1; check(ppid != -1, "Couldnt' find Ppid in process status file"); fclose(fp); free(tmp); return ppid; error: if (fp) fclose(fp); return -1; }