代码之家  ›  专栏  ›  技术社区  ›  psychotik

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

  •  11
  • psychotik  · 技术社区  · 15 年前

    macs上有没有可以用来枚举进程的carbon/cocoa/c api?我在找类似的东西 EnumProcesses 在Windows上。

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

    谢谢!

    4 回复  |  直到 9 年前
        1
  •  15
  •   valexa    13 年前

    这里有一些具体的实现和详细信息,请注意proc->kp_proc.p_comm有一个字符长度限制,这就是为什么我要实现infoforpid:

    可可:

    [nsworkspace launchedapplications](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;
    }
    

    (见) Technical Q&A QA1123 Getting List of All Processes on 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;
    }
    
        2
  •  9
  •   Dan Fego    9 年前

    Techzen说: 截至2013年12月,流程管理器已完全弃用。

    啊,我刚找到 Process Manager reference

    看起来像 GetNextProcess GetProcessInfo 帮助弄清楚正在运行的内容。根据戴夫的建议, GetBSDProcessList 如果您要查找守护进程而不仅仅是碳/可可进程,则可以使用。

        3
  •  7
  •   Dave DeLong    13 年前

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

    1. 如果是带有停靠图标的gui应用程序,请使用 -[NSWorkspace launchedApplications] .
    2. 通过一个 NSTask ,阅读结果,然后自己搜索(或通过grep或其他方式)。
    3. 使用 GetBSDProcessList 此处描述的功能: http://developer.apple.com/legacy/mac/library/#qa/qa2001/qa1123.html (我以前用过)
        4
  •  2
  •   ericg    15 年前

    NSRunningApplicationClass 它说:

    nsrunningapplication是一个类,用于为应用程序的单个实例操作和提供信息。只跟踪用户应用程序;这不会提供有关系统上每个进程的信息。

    要访问所有正在运行的应用程序的列表,请使用中的running applications方法 NSWorkspace .

    我建议你看看 Workspace Services Programming Topics

    推荐文章