#include #include #include #include #include pid_t get_current_max_threads() { pid_t result = pstat(-1, NULL); if (result < 0) { perror("pstat(-1)"); exit(1); } return result; } void display_process(struct pstat* pstatbuf) { printf("%ld %ld %s %s (%ld ms)\n", pstatbuf->pt_pid, pstatbuf->pt_ppid, pstatbuf->pt_name, pstatname(pstatbuf), pstatbuf->pt_time); } int try_pstat(pid_t pid, struct pstat* pstatbuf) { pid_t result = pstat(pid, pstatbuf); if (result < 0) { if (errno == ESRCH) return 0; perror("pstat"); exit(1); } return 1; } int main() { struct pstat pst; pid_t max = get_current_max_threads(); for (pid_t pid = 0; pid <= max; pid++) { if (try_pstat(pid, &pst)) { display_process(&pst); } } }