使用sysctl检测当前在iOS上运行的应用程序 [英] Detect which app is currently running on iOS using sysctl

查看:508
本文介绍了使用sysctl检测当前在iOS上运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已经实现了一个简单的活动监视器,以监视iOS上所有正在运行的进程.

I have currently implemented a simple activity monitor to watch all running processes on iOS.

要检索所有正在运行的进程的列表,请执行以下操作:

To retrieve a list of all running processes, I do this:

size_t size;
struct kinfo_proc *procs = NULL;
int status;
NSMutableArray *killedProcesses = [[NSMutableArray alloc] init];

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

status  = sysctl(mib, 4, NULL, &size, NULL, 0);
procs   = malloc(size);
status  = sysctl(mib, 4, procs, &size, NULL, 0);

// now, we have a nice list of processes

如果我想了解有关特定过程的更多信息,我会做:

And if I want more information about a specific process, I'll do:

struct kinfo_proc *proc;
int mib[5] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pidNum, 0 };
int count;
size_t size = 0;

// ask the proc size
if(sysctl(mib, 4, NULL, &size, NULL, 0) < 0) return -1;

// allocate memory for proc
proc = (struct kinfo_proc *)malloc(size);

sysctl(mib, 4, proc, &size, NULL, 0);

我想要的所有多余的proc信息现在都存储在 proc 中.

All the extra proc info I want is now stored in proc.

我注意到应用程序不会被操作系统杀死.即使长时间不使用某个应用程序(超过10分钟),该应用程序仍会保留在进程列表中.即使我查询该进程具有什么状态"(proc-> kp_proc.p_stat),它也会返回正在运行".

I notice that apps won't be killed by the OS. Even when an app is not used for a long time (longer than 10 min.) it will remain in the process list. Even when I query what "state" the process has (proc->kp_proc.p_stat), it returns "running".

我的问题是:有人知道一种方法来检测当前正在/正在使用的PID(可能是:增加cpu时间?运行时间?cpu滴答声等)?

My question is: does anybody know a method to detect which PID is currently running/actively used (maybe: increasing cpu time? running time? cpu ticks etc.) ??

推荐答案

回答问题的当前运行"部分:

Answering the "currently running" part of your question:

我使用了此答案中的代码

I used the code from this answer Can we retrieve the applications currently running in iPhone and iPad

在这里查看了k_proc声明:

Looked after the k_proc declarations here: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/proc.h

通过反复试验发现,将p_flag设置为18432的进程是当前正在运行的应用程序(在本例中为测试).

With trial and error found out that the processes with the p_flag set to 18432 is the currently running application (in this case this test).

查看第一个链接,并将for循环的内部替换为:

See the first link, and replace the inner of the for cycle with:

if (process[i].kp_proc.p_flag == 18432){

                        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 * status       = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag ];

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

                        [array addObject:dict];

}

这篇关于使用sysctl检测当前在iOS上运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆