BSD Processes (with code)

  • Hello,

    I have found on the net a piece of code that allow to me to receive a
    list of the MacOSX active processes. Now i have "adapted" it to work
    with an ObjC program. Unfortunatly i don't know much about this
    things... the only thing that i receive is the PID of the process and
    the name of it.
    I would to know:
    1) the % of cpu used
    2) the % of memory used
    3) the total free memory
    4) the total used memory
    5) show only the current user processes

    Anyone can help me to receive these infos? I have found some infos
    inside the sys/proc.h file but i don't know how to use these infos.
    This is my code:

    - (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);
        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);
        }
        free(mylist);

        return [ProcList autorelease];
    }

    static int GetBSDProcessList(kinfo_proc **procList, size_t *procCount)
        // Returns a list of all BSD processes on the system.  This routine
        // allocates the list and puts it in *procList and a count of the
        // number of entries in *procCount.  You are responsible for freeing
        // this list (use "free" from System framework).
        // On success, the function returns 0.
        // On error, the function returns a BSD errno value.
    {
        int                err;
        kinfo_proc *        result;
        bool                done;
        static const int    name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0
    };
        // Declaring name as const requires us to cast it when passing it to
        // sysctl because the prototype doesn't include the const modifier.
        size_t              length;

        assert( procList != NULL);
      // assert(*procList == NULL);
        assert(procCount != NULL);

        *procCount = 0;

        // We start by calling sysctl with result == NULL and length == 0.
        // That will succeed, and set length to the appropriate length.
        // We then allocate a buffer of that size and call sysctl again
        // with that buffer.  If that succeeds, we're done.  If that fails
        // with ENOMEM, we have to throw away our buffer and loop.  Note
        // that the loop causes use to call sysctl with NULL again; this
        // is necessary because the ENOMEM failure case sets length to
        // the amount of data returned, not the amount of data that
        // could have been returned.

        result = NULL;
        done = false;
        do {
            assert(result == NULL);

            // Call sysctl with a NULL buffer.

            length = 0;
            err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1,
                          NULL, &length,
                          NULL, 0);
            if (err == -1) {
                err = errno;
            }

            // Allocate an appropriately sized buffer based on the results
            // from the previous call.

            if (err == 0) {
                result = malloc(length);
                if (result == NULL) {
                    err = ENOMEM;
                }
            }

            // Call sysctl again with the new buffer.  If we get an ENOMEM
            // error, toss away our buffer and start again.

            if (err == 0) {
                err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) -
    1,
                              result, &length,
                              NULL, 0);
                if (err == -1) {
                    err = errno;
                }
                if (err == 0) {
                    done = true;
                } else if (err == ENOMEM) {
                    assert(result != NULL);
                    free(result);
                    result = NULL;
                    err = 0;
                }
            }
        } while (err == 0 && ! done);

        // Clean up and establish post conditions.

        if (err != 0 && result != NULL) {
            free(result);
            result = NULL;
        }
        *procList = result;
        if (err == 0) {
            *procCount = length / sizeof(kinfo_proc);
        }

        assert( (err == 0) == (*procList != NULL) );

        return err;
    }

    ----
    Daniele Margutti            mail: <malcom.mac...>
    Westcode Software, Ltd.        info: http://www.westcode.vze.com/
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On lundi, mai 5, 2003, at 09:42  PM, Daniele M. wrote:

    > Hello,
    >
    > I have found on the net a piece of code that allow to me to receive a
    > list of the MacOSX active processes. Now i have "adapted" it to work
    > with an ObjC program. Unfortunatly i don't know much about this
    > things... the only thing that i receive is the PID of the process and
    > the name of it.
    > I would to know:
    > 1) the % of cpu used
    > 2) the % of memory used
    > 3) the total free memory
    > 4) the total used memory
    > 5) show only the current user processes

    One solution could be to have a look at the source code for top in the
    Darwin repository.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month may 2003 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today