#include #include #include #include int main() { int pid; FILE *fd; char filename[32]; /* proc file variables */ pid_t tpid, ppid, pgid, sid; char tcomm[16], state; int tty_nr, tty_pgrp, num_threads, exit_signal; unsigned long flags, min_flt, cmin_flt, maj_flt, cmaj_flt, vsize, rss; unsigned long rsslim, start_code, end_code, start_stack, signal; unsigned long blocked, sigign, sigcatch, wchan, dumb, rt_priority; unsigned long policy; clock_t utime, stime, cutime, cstime, esp, eip; long priority, nice; unsigned long long start_time, ticks; unsigned int task_cpu; pid = 1; sprintf(filename, "/proc/%d/stat", pid); fd = fopen(filename, "r"); fscanf(fd, "%d %s %c %d %d %d %d %d %lu \ %lu %lu %lu %lu %lu %lu %ld \ %ld %ld %ld %d %lu %llu %lu \ %ld %lu %lu %lu %lu %lu %lu \ %lu %lu %lu %lu %lu %lu %lu \ %d %d %lu %lu %llu\n", &tpid, tcomm, &state, &ppid, &pgid, &sid, &tty_nr, &tty_pgrp, &flags, &min_flt, &cmin_flt, &maj_flt, &cmaj_flt, &utime, &stime, &cutime, &cstime, &priority, &nice, &num_threads, &dumb, &start_time, &vsize, &rss, &rsslim, &start_code, &end_code, &start_stack, &esp, &eip, &signal, &blocked, &sigign, &sigcatch, &wchan, &dumb, &dumb, &exit_signal, &task_cpu, &rt_priority, &policy, &ticks); printf("%d %s: jiffies in user mode: %lu jiffies\n", pid, tcomm, utime); printf("%d %s: seconds in user mode: %lu sec\n", pid, tcomm, utime/HZ); printf("%d %s: jiffies in kernel mode: %lu jiffies\n", pid, tcomm, stime); printf("%d %s: seconds in kernel mode: %lu sec\n", pid, tcomm, stime/HZ); fclose(fd); return 0; }