以编程方式检查进程是否在Mac上运行

psy*_*tik 12 unix macos cocoa macos-carbon

Mac上是否有可用于枚举进程的Carbon/Cocoa/C API?我正在寻找类似EnumProcessesWindows的东西.

我的目标是从代码中检查进程是否正在运行(按名称).

谢谢!

val*_*exa 16

以下是一些具体的实现和细节,请注意proc-> kp_proc.p_comm有一个字符长度限制,这就是为什么我实现了infoForPID:而不是

可可 :

[NSWorkspace launchApplications](10.2+,在10.7中弃用,非常有限的进程列表)[NSWorkspace runningApplications](10.6+,更少限制的进程列表,但仍然不包括守护进程)

碳 :

- (NSArray*)getCarbonProcessList
{
    NSMutableArray *ret = [NSMutableArray arrayWithCapacity:1];
    ProcessSerialNumber psn = { kNoProcess, kNoProcess };
    while (GetNextProcess(&psn) == noErr) {
        CFDictionaryRef cfDict = ProcessInformationCopyDictionary(&psn,  kProcessDictionaryIncludeAllInformationMask);
        if (cfDict) {
            NSDictionary *dict = (NSDictionary *)cfDict;
            [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                            [NSString stringWithFormat:@"%@",[dict objectForKey:(id)kCFBundleNameKey]],@"pname",
                            [NSString stringWithFormat:@"%@",[dict objectForKey:@"pid"]],@"pid",
                            [NSString stringWithFormat:@"%d",(uid_t)getuid()],@"uid",                                               
                            nil]]; 
            CFRelease(cfDict);          
        }
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

C :(参见技术问答QA1123获取Mac OS X上的所有进程列表)

- (NSArray*)getBSDProcessList
{
    NSMutableArray *ret = [NSMutableArray arrayWithCapacity:1];
    kinfo_proc *mylist;
    size_t mycount = 0;
    mylist = (kinfo_proc *)malloc(sizeof(kinfo_proc));
    GetBSDProcessList(&mylist, &mycount);
    int k;
    for(k = 0; k < mycount; k++) {
        kinfo_proc *proc = NULL;
        proc = &mylist[k];
        NSString *fullName = [[self infoForPID:proc->kp_proc.p_pid] objectForKey:(id)kCFBundleNameKey];
        if (fullName == nil) fullName = [NSString stringWithFormat:@"%s",proc->kp_proc.p_comm];
        [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                        fullName,@"pname",
                        [NSString stringWithFormat:@"%d",proc->kp_proc.p_pid],@"pid",
                        [NSString stringWithFormat:@"%d",proc->kp_eproc.e_ucred.cr_uid],@"uid",                                               
                        nil]];                                            
    }
    free(mylist);  
    return ret;
}

- (NSDictionary *)infoForPID:(pid_t)pid 
{
    NSDictionary *ret = nil;
    ProcessSerialNumber psn = { kNoProcess, kNoProcess };
    if (GetProcessForPID(pid, &psn) == noErr) {
        CFDictionaryRef cfDict = ProcessInformationCopyDictionary(&psn,kProcessDictionaryIncludeAllInformationMask); 
        ret = [NSDictionary dictionaryWithDictionary:(NSDictionary *)cfDict];
        CFRelease(cfDict);
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)


psy*_*tik 10

TechZen表示:截至2013年12月,流程管理器已完全弃用.

啊,我刚刚找到了Process Manager参考

看起来GetNextProcessGetProcessInfo帮助弄清楚正在运行的是什么.正如Dave所建议的那样,GetBSDProcessList如果您正在寻找守护进程而不仅仅是碳/可可进程,则可以使用它.

  • Process Manager(目前)不被弃用,并且以64位可用.我认为它不像其他一些API那样具有斧头. (2认同)

Dav*_*ong 7

有几种方法可以做到这一点:

  1. 如果它是带有Dock图标的GUI应用程序,请使用-[NSWorkspace launchedApplications].
  2. 通过a分叉另一个进程(如ps或top或其他)NSTask,读取结果,并自己搜索(或通过grep或其他东西管道).
  3. 使用GetBSDProcessList此处描述的功能:http: //developer.apple.com/legacy/mac/library/#qa/qa2001/qa1123.html(我以前成功使用过此功能)

  • 嗨戴夫,你有没有机会挖出更新的链接?似乎Apple重新安排了他们的开发者网站,该链接不再有效.谢谢! (2认同)