Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:16:35+00:00 2026-05-16T10:16:35+00:00

I’ve managed to implement the code on this listing to get a list of

  • 0

I’ve managed to implement the code on this listing to get a list of all the processes running and their IDs. What I need now is to extract how much time each process uses the CPU.

I’ve tried referring to the keys in the code, but when I try to print ‘Ticks of CPU Time’ I get a zero value for all of the processes. Plus, even if I did get a value I’m not sure if ‘Ticks of CPU Time’ is exactly what I’m looking for.

struct  vmspace *p_vmspace; /* Address space. */
struct  sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
int p_flag;         /* P_* flags. */
char    p_stat;         /* S* process status. */
pid_t   p_pid;          /* Process identifier. */
pid_t   p_oppid;     /* Save parent pid during ptrace. XXX */
int p_dupfd;     /* Sideways return value from fdopen. XXX */
/* Mach related  */
caddr_t user_stack; /* where user stack was allocated */
void    *exit_thread;   /* XXX Which thread is exiting? */
int     p_debugger;     /* allow to debug */
boolean_t   sigwait;    /* indication to suspend */
/* scheduling */
u_int   p_estcpu;    /* Time averaged value of p_cpticks. */
int p_cpticks;   /* Ticks of cpu time. */
fixpt_t p_pctcpu;    /* %cpu for this process during p_swtime */
void    *p_wchan;    /* Sleep address. */
char    *p_wmesg;    /* Reason for sleep. */
u_int   p_swtime;    /* Time swapped in or out. */
u_int   p_slptime;   /* Time since last blocked. */
struct  itimerval p_realtimer;  /* Alarm timer. */
struct  timeval p_rtime;    /* Real time. */
u_quad_t p_uticks;      /* Statclock hits in user mode. */
u_quad_t p_sticks;      /* Statclock hits in system mode. */
u_quad_t p_iticks;      /* Statclock hits processing intr. */
int p_traceflag;        /* Kernel trace points. */
struct  vnode *p_tracep;    /* Trace to vnode. */
int p_siglist;      /* DEPRECATED */
struct  vnode *p_textvp;    /* Vnode of executable. */
int p_holdcnt;      /* If non-zero, don't swap. */
sigset_t p_sigmask; /* DEPRECATED. */
sigset_t p_sigignore;   /* Signals being ignored. */
sigset_t p_sigcatch;    /* Signals being caught by user. */
u_char  p_priority; /* Process priority. */
u_char  p_usrpri;   /* User-priority based on p_cpu and p_nice. */
char    p_nice;     /* Process "nice" value. */
char    p_comm[MAXCOMLEN+1];
struct  pgrp *p_pgrp;   /* Pointer to process group. */
struct  user *p_addr;   /* Kernel virtual addr of u-area (PROC ONLY). */
u_short p_xstat;    /* Exit status for wait; also stop signal. */
u_short p_acflag;   /* Accounting flags. */
struct  rusage *p_ru;   /* Exit information. XXX */

In fact I’ve also tried to print Time averaged value of p_cpticks and a few others and never got interesting values. Here is my code which is printing the information retrieved (I got 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’ve just offered a bounty for this question. What I’m looking for specifically is to get the amount of time each process spends in CPU.

If, in addition to this, you can give %CPU being used by a process, that would be fantastic.

The code should be optimal in that it will be called every second and the method will be called on all running processes. Objective-C preferable.

Thanks again!

EDIT 2

Also, any comments as to why people are ignoring this question would also be helpful 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:16:36+00:00Added an answer on May 16, 2026 at 10:16 am

    Have a look at the Darwin source for libtop.c and particularly the libtop_pinfo_update_cpu_usage() function. Note that:

    1. You’ll need a basic understanding of Mach programming fundamentals to make sense of this code, as it uses task ports, etc.
    2. If you want to simply use libtop, you’ll have to download the source and compile it yourself.
    3. Your process will need privileges to get at the task ports for other processes.

    If all this sounds rather daunting, well… There is a way that uses less esoteric APIs: Just spawn a top process and parse its standard output. A quick glimpse over the top(1) man page turned up this little gem:

    $ top -s 1 -l 3600 -stats pid,cpu,time
    

    That is, sample once per second for 3600 seconds (one hour), and output to stdout in log form only the statistics for pid, cpu usage, and time.

    Spawning and managing the child top process and then parsing its output are all straightforward Unix programming exercises.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.