How to get process ID in iPhone or iPad?
i need to to end a process in my iPhone for security issues so i need it's id How to get process ID in iPhone开发者_开发知识库 or iPad ?? in order to end it in Background process
Not entirely sure of what you actually need, but have a look at the NSProcessInfo
API, expecially the value returned by [[NSProcessInfo processInfo] processIdentifier]
.
As the documentation states, NSProcessInfo
provides information about the current process only.
I suggest the following:
- (NSArray *) runningProcesses {
//CTL_KERN,KERN_PROC,KERN_PROC_ALL
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 = NULL;
}
return nil;
}
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--) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];
double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;
NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t];
//NSLog(@"process.kp_proc.p_stat = %c",process.kp_proc.p_stat);
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:processID forKey:@"ProcessID"];
[dic setValue:processName forKey:@"ProcessName"];
[dic setValue:proc_CPU forKey:@"ProcessCPU"];
[dic setValue:proc_useTiem forKey:@"ProcessUseTime"];
[processID release];
[processName release];
[proc_CPU release];
[proc_useTiem release];
[array addObject:dic];
[dic release];
[pool release];
}
free(process);
process = NULL;
NSLog(@"runningProcesses is === %@",array);
return array;
}
}
}
return nil;
}
精彩评论