从PID获取的名字吗? [英] Get name from PID?

查看:290
本文介绍了从PID获取的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OSX山狮,我试图用它的PID检索过程的名字。

I am on OSX Mountain Lion and am trying to retrieve a processes' name using its PID.

以下是code我使用的:

The following is the code I am using:

pid_t pid = 10687;
char pathBuffer [PROC_PIDPATHINFO_MAXSIZE] = "";
char nameBuffer [256] = "";

int sizeOfVal = sizeof(nameBuffer);
proc_pidpath(pid, pathBuffer, sizeof(pathBuffer));
proc_name(pid, nameBuffer, sizeof(nameBuffer));

NSLog(@"Path: %s\n Name: %s\n", pathBuffer, nameBuffer);

在code以上能够正常检索名称,但它只能检索前15个字符和忽略的其余部分。请注意,这是不符合显示名称的问题,但的检索的它。这个问题是不是与我的应用程序的其他部分,因为我在独立的应用测试上述code。另外请注意,我试图改变PID,但不管是什么PID我尝试code只检索名称的前15个字符。路径检索完美的作品。

The code above is able to retrieve the name properly, however it only retrieves the first 15 characters and "ignores" the rest. Note this is not a problem with displaying the name, but with retrieving it. The problem is not with the rest of my application as I am testing the above code in a standalone application. Also note that I tried changing the PID, but regardless of what PID I try the code only retrieves the first 15 characters of the name. Path retrieval works perfectly.

有没有人有什么我做错了任何想法?

Does anyone have any ideas about what I am doing wrong?

推荐答案

函数着眼于价值是结构 proc_bsdshortinfo 。它仅限于返回一个16字节的字符串,或者包括空终止时15可读字符。

The function looks at the value is the struct proc_bsdshortinfo. It is limited to return a 16 byte string, or 15 readable characters when including the null terminator.

了sys / param.h

#define MAXCOMLEN   16      /* max command name remembered */

SYS / proc_info.h

struct proc_bsdshortinfo {
        uint32_t                pbsi_pid;       /* process id */
        uint32_t                pbsi_ppid;      /* process parent id */
        uint32_t                pbsi_pgid;      /* process perp id */
    uint32_t                pbsi_status;        /* p_stat value, SZOMB, SRUN, etc */
    char                    pbsi_comm[MAXCOMLEN];   /* upto 16 characters of process name */
    uint32_t                pbsi_flags;              /* 64bit; emulated etc */
        uid_t                   pbsi_uid;       /* current uid on process */
        gid_t                   pbsi_gid;       /* current gid on process */
        uid_t                   pbsi_ruid;      /* current ruid on process */
        gid_t                   pbsi_rgid;      /* current tgid on process */
        uid_t                   pbsi_svuid;     /* current svuid on process */
        gid_t                   pbsi_svgid;     /* current svgid on process */
        uint32_t                pbsi_rfu;       /* reserved for future use*/
};

编辑:为了解决这个问题,拿到最后一个路径组件:

To get around this, get the last path component:

pid_t pid = 3051;
char pathBuffer [PROC_PIDPATHINFO_MAXSIZE];
proc_pidpath(pid, pathBuffer, sizeof(pathBuffer));

char nameBuffer[256];

int position = strlen(pathBuffer);
while(position >= 0 && pathBuffer[position] != '/')
{
    position--;
}

strcpy(nameBuffer, pathBuffer + position + 1);

printf("path: %s\n\nname:%s\n\n", pathBuffer, nameBuffer);

这篇关于从PID获取的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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