1

Possible Duplicate:
Can we retrieve the applications currently running in iPhone and iPad

I am searching for a way to grab a list of all processes running on an iDevice and their respective PID's.

I want to run this app on my device and it will show a dynamic list of processes running.

I couldn't find a way to do this. I've tried looking at iOS API's, and had a look into the files:

<mach/mach.h>
<sys/utsname.h>
<sys/resource.h>
<sys/syslog.h>

I have seen apps that can do exactly this (dynamically showing PID and process names). Any tips and direction would be much appreciated!

Note: I can extract the process-ID of my own app on the device through 'sys/utsname.h' but not any other app

Community
  • 1
  • 1
Jack
  • 11
  • 1
  • 2
  • 1
    possible duplicate of [Can we retrieve the applications currently running in iPhone and iPad](http://stackoverflow.com/questions/4312613/can-we-retrieve-the-applications-currently-running-in-iphone-and-ipad) and [How to get the active processes running in iOS](http://stackoverflow.com/questions/9342578/how-to-get-the-active-processes-running-in-ios) – Black Frog Jan 23 '13 at 22:31
  • Look for the following reply: http://stackoverflow.com/a/9886453/1778980 – p0lAris Jan 23 '13 at 22:49

1 Answers1

0

Found the answer, thanks Black Frog. Not sure why I couldn't find the thread before.

I've updated the code from the link to work in Xcode 4.5 for use of ARC and iOS6

int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;

size_t size;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);

struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;

do {

    size += size / 10;
    newprocess = realloc(process, size);

    if (!newprocess){
        if (process){
            free(process);
        }
    }

    process = newprocess;
    st = sysctl(mib, miblen, process, &size, NULL, 0);

} while (st == -1 && errno == ENOMEM);

if (st == 0){
    if (size % sizeof(struct kinfo_proc) == 0){
        int nprocess = size / sizeof(struct kinfo_proc);

        if (nprocess){

            NSMutableArray * array = [[NSMutableArray alloc] init];

            for (int i = nprocess - 1; i >= 0; i--){

                NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];

                NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
                                                                    forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
                [array addObject:dict];
            }

            free(process);
            NSLog(@"System pid: %@", array);
        }
    }
}
Jack
  • 11
  • 1
  • 2