This is tasks.c in view mode; [Download] [Up]
#include <mach.h> #include <sys/table.h> #include <pwd.h> #include <time.h> void main(int argc, char *argv[]) { int i,j,cpu,tot_cpu,RAM; int u_time, s_time; task_array_t task_list; unsigned int task_count; thread_array_t thread_list; unsigned int thread_count; char ESC = 27; time_t clock; processor_set_t default_set, default_set_priv; kern_return_t error; unsigned int pid; struct host_basic_info host_Info; unsigned int host_info_count=HOST_BASIC_INFO_COUNT; struct task_basic_info task_Info; unsigned int task_info_count=TASK_BASIC_INFO_COUNT; struct thread_basic_info thread_Info; unsigned int thread_info_count=THREAD_BASIC_INFO_COUNT; struct tbl_procinfo proc_info; struct pig_struct { char name[20]; int pid; int cpu; int mem; int vsize; int rsize; char tty[20]; char status[2]; char time[7]; char command[20]; }; struct pig_struct pigs[50]; /* PID,CPU */ int pig_count; struct passwd *pw; char host[12]; int delay = 3; /*================================================================*/ if(argc==2) delay=atoi(argv[1]); gethostname(host,12); error=processor_set_default(host_self(), &default_set); if (error!=KERN_SUCCESS) { mach_error("Error calling processor_set_default()", error); exit(1); } error=host_processor_set_priv(host_priv_self(), default_set, &default_set_priv); if (error != KERN_SUCCESS) { mach_error("Call to host_processor_set_priv() failed", error); exit(1); } error=host_info(host_self(), HOST_BASIC_INFO, (host_info_t)&host_Info, &host_info_count); if (error != KERN_SUCCESS) mach_error("host_info() call failed", error); else RAM = host_Info.memory_size; while(1) { error=processor_set_tasks(default_set_priv, &task_list, &task_count); if (error != KERN_SUCCESS) { mach_error("Call to processor_set_tasks() failed", error); exit(1); } tot_cpu = 0; pig_count = 0; for(i=0; i<task_count; i++) { error=task_info(task_list[i], TASK_BASIC_INFO, (task_info_t)&task_Info,&task_info_count); if(error!=KERN_SUCCESS) mach_error("Error calling task_info",error); error = task_threads(task_list[i], &thread_list, &thread_count); if (error != KERN_SUCCESS) mach_error("Error calling task_threads", error); else { unix_pid(task_list[i],&pid); u_time = s_time = 0; cpu = 0; for(j=0; j<thread_count; j++) { error=thread_info(thread_list[j], THREAD_BASIC_INFO, (thread_info_t)&thread_Info, &thread_info_count); if (error!=KERN_SUCCESS) mach_error("Error calling thread_info()", error); else { u_time += thread_Info.user_time.seconds; s_time += thread_Info.system_time.seconds; cpu += thread_Info.cpu_usage; } } error = vm_deallocate(task_self(), (vm_address_t)thread_list, sizeof(thread_list)*thread_count); if (error != KERN_SUCCESS) mach_error("Trouble freeing thread_list", error); /* account for terminated threads */ u_time += task_Info.user_time.seconds; s_time += task_Info.system_time.seconds; tot_cpu += cpu; if(cpu) { /* pid */ pigs[pig_count].pid = pid; /* cpu % */ pigs[pig_count].cpu = cpu; /* total time */ if((u_time + s_time) % 60 > 9) sprintf(pigs[pig_count].time,"%u:%2u",(u_time+s_time)/60, (u_time+s_time) % 60); else sprintf(pigs[pig_count].time,"%u:0%1u",(u_time+s_time)/60, (u_time+s_time) % 60); /* get process info */ table(TBL_PROCINFO, pid, (char *)&proc_info, 1,sizeof proc_info); /* get user name */ pw = getpwuid(proc_info.pi_uid); strcpy(pigs[pig_count].name,pw->pw_name); /* get tty name */ if(isatty(proc_info.pi_ttyd)) strcpy(pigs[pig_count].tty,ttyname(proc_info.pi_ttyd)+5); else strcpy(pigs[pig_count].tty,""); /* get command line */ strcpy(pigs[pig_count].command,proc_info.pi_comm); /* memory sizes */ pigs[pig_count].vsize = task_Info.virtual_size/1024; pigs[pig_count].rsize = task_Info.resident_size/1024; pig_count++; } } } printf("%c[00;00H",ESC); /* goto 0,0 */ printf("%c[2J",ESC); /* clear the screen */ clock = time(NULL); printf("%-12s LOAD: %5.1f %26s",host,tot_cpu*100.0/LOAD_SCALE, ctime(&clock)); printf("%c[7m",ESC); /* reverse video */ printf("USER PID %%CPU %%MEM VSIZE RSIZE TTY TIME COMMAND \n"); printf("%c[0m",ESC); /* normal video */ for(j=0; j<pig_count; j++) { printf("%-8s %5u %4.1f %4.1f %6u %6u %-5s %7s %-20s\n", pigs[j].name, pigs[j].pid, pigs[j].cpu*100.0/LOAD_SCALE, pigs[j].rsize*100.0/(RAM/1024), pigs[j].vsize, pigs[j].rsize, pigs[j].tty, pigs[j].time, pigs[j].command); } error=vm_deallocate(task_self(), (vm_address_t)task_list, sizeof(task_list)*task_count); if (error != KERN_SUCCESS) mach_error("Trouble freeing task_list", error); sleep(delay); } return; }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.