I managed to implement the code on this listing to get a list of all running processes and their identifiers. Now I need to find out how long each processor uses the processor.
I tried to refer to the keys in the code, but when I try to print "Ticks of CPU Time", I get a zero value for all processes. Plus, even if I got the value, I'm not sure that "Ticks of CPU Time" is exactly what I'm looking for.
struct vmspace *p_vmspace; struct sigacts *p_sigacts; int p_flag; char p_stat; pid_t p_pid; pid_t p_oppid; int p_dupfd; caddr_t user_stack; void *exit_thread; int p_debugger; boolean_t sigwait; u_int p_estcpu; int p_cpticks; fixpt_t p_pctcpu; void *p_wchan; char *p_wmesg; u_int p_swtime; u_int p_slptime; struct itimerval p_realtimer; struct timeval p_rtime; u_quad_t p_uticks; u_quad_t p_sticks; u_quad_t p_iticks; int p_traceflag; struct vnode *p_tracep; int p_siglist; struct vnode *p_textvp; int p_holdcnt; sigset_t p_sigmask; sigset_t p_sigignore; sigset_t p_sigcatch; u_char p_priority; u_char p_usrpri; char p_nice; char p_comm[MAXCOMLEN+1]; struct pgrp *p_pgrp; struct user *p_addr; u_short p_xstat; u_short p_acflag; struct rusage *p_ru;
In fact, I also tried to print the average time value of p_cpticks and several others and never got interesting values. Here is my code that prints the received information (I received it from cocoabuilder.com):
- (NSDictionary *) getProcessList { NSMutableDictionary *ProcList = [[NSMutableDictionary alloc] init]; kinfo_proc *mylist; size_t mycount = 0; mylist = (kinfo_proc *)malloc(sizeof(kinfo_proc)); GetBSDProcessList(&mylist, &mycount); printf("There are %d processes.\n", (int)mycount); NSLog(@" = = = = = = = = = = = = = = ="); int k; for(k = 0; k < mycount; k++) { kinfo_proc *proc = NULL; proc = &mylist[k]; // NSString *processName = [NSString stringWithFormat: @"%s",proc->kp_proc.p_comm]; //[ ProcList setObject: processName forKey: processName ]; // [ ProcList setObject: proc->kp_proc.p_pid forKey: processName]; // printf("ID: %d - NAME: %s\n", proc->kp_proc.p_pid, proc->kp_proc.p_comm); printf("ID: %d - NAME: %s CPU TIME: %d \n", proc->kp_proc.p_pid, proc->kp_proc.p_comm, proc->kp_proc.p_pid ); // Right click on p_comm and select 'jump to definition' to find other values. } free(mylist); return [ProcList autorelease]; }
Thanks!
EDIT . I just offered generosity for this question. What I am looking for specifically is the time spent by each process on the processor.
If in addition to this you can give% CPU used by the process, it will be fantastic.
The code should be optimal, because it will be called every second, and the method will be called for all running processes. Objective-C is preferred.
Thanks again!
EDIT 2
Also, any comments regarding why people ignore this question will also be helpful :)
objective-c process cocoa macos
Eric brotto
source share