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

4

1 回答 1

0

找到答案了,谢谢Black Frog。不知道为什么我以前找不到线程。

我已经更新了链接中的代码以在 Xcode 4.5 中使用 ARC 和 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);
        }
    }
}
于 2013-01-24T21:25:30.000 回答