且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从PID获取的名字吗?

更新时间:2023-11-30 23:03:28

函数着眼于价值是结构 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);