#include #include #include #include #define NUM_THREAD 10 int pids[NUM_THREAD]; int num_thread; void report_proc_time(int pid) { char fname[80]; FILE* procf; char cmd[80]; unsigned long utime, stime; sprintf(fname, "/proc/%d/stat", pid); procf = fopen(fname, "r"); if (procf == NULL) { printf("failed to open file %s\n", fname); assert(0 ==1); } fscanf(procf, "%d %s %*c %*d %*d %*d %*d %*d %*lu %*lu %*lu %*lu %*lu %lu %lu", &pid, cmd, &utime, &stime); printf("pid=%d, cmd: %s, time(ms): %lu, utime(ms): %lu, stime(ms): %lu\n", pid, cmd, (utime+stime)*10, utime*10, stime*10); fclose(procf); } void report_time(void) { int i; printf("-----------------------------------------\n"); for (i=0; i< num_thread; i++) report_proc_time(pids[i]); } main() { int i; int pid; printf("input first pid : "); scanf("%d", &pid); printf("input num of threads: "); scanf("%d", &num_thread); for (i=0; i< num_thread; i++) pids[i] = pid + i; for (;;) { sleep(2); report_time(); } };