int get_next_process(struct process_iterator *it, struct process *p) { if (it->dip == NULL) { //end of processes return -1; } if (it->filter->pid != 0 && !it->filter->include_children) { int ret = read_process_info(it->filter->pid, p); //p->starttime += it->boot_time; closedir(it->dip); it->dip = NULL; if (ret != 0) return -1; return 0; } struct dirent *dit = NULL; //read in from /proc and seek for process dirs while ((dit = readdir(it->dip)) != NULL) { if(strtok(dit->d_name, "0123456789") != NULL) continue; p->pid = atoi(dit->d_name); if (it->filter->pid != 0 && it->filter->pid != p->pid && !is_child_of(p->pid, it->filter->pid)) continue; read_process_info(p->pid, p); //p->starttime += it->boot_time; break; } if (dit == NULL) { //end of processes closedir(it->dip); it->dip = NULL; return -1; } return 0; }
int show_process() { struct dirent *ent; DIR *dir; process_nums = 0; dir = opendir("/proc"); printf("<tr style='background-color:#f60;'><td>进程名</td><td>进程号</td><td>虚拟地址</td><td>RSS</td><td>代码段</td></tr>"); while ((ent = readdir(dir))) { if(*ent->d_name < '0' || *ent->d_name > '9') continue; if (read_process_info(atoi(ent->d_name))) continue; if (process_nums%2) printf("<tr style='background-color:#992796;' ><td>%s</td><td>%d</td><td>%p</td><td>%p</td><td>%p</td></tr>", p_cmd, p_pid, (void *)p_vsize, (void *)p_rss, (void *)(p_end_code - p_start_code)); else printf("<tr style='background-color:#999996;'><td>%s</td><td>%d</td><td>%p</td><td>%p</td><td>%p</td></tr>", p_cmd, p_pid, (void *)p_vsize, (void *)p_rss, (void *)(p_end_code - p_start_code)); process_nums++; } closedir(dir); return 1; }