【问题标题】:Need help in getting the process name based on the pid in aix在根据 aix 中的 pid 获取进程名称时需要帮助
【发布时间】:2013-12-12 18:40:23
【问题描述】:

我需要在 AIX 环境中编写一个 C 程序,它会给我进程名称。 我可以根据 pid 获取 pid 但不能获取进程名称。 aix 环境中可用的任何特定系统调用?

谢谢

【问题讨论】:

  • 哪个 AIX? AIX4 和 5L 之间有一些相当根本的改进,现在我们达到了 7 个。
  • 我正在尝试在 AIX 5.1 上编写它。

标签: aix


【解决方案1】:

我意识到这是一个老问题。 但是,为了将@CoreyStup 答案转换为更接近 OP 的函数,我提供了这个:(在 AIX 6.1 上测试,使用:g++ -o pn pn.cc)

--- pn.cc ---

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <procinfo.h>
#include <sys/types.h>

using namespace std;

string getProcName(int pid)
{
    struct procsinfo pinfo[16];
    int numproc;
    int index = 0;
    while((numproc = getprocs(pinfo, sizeof(struct procsinfo), NULL, 0, &index, 16)) > 0)
    {
        for(int i=0; i<numproc; ++i)
        {
            // skip zombies
            if (pinfo[i].pi_state == SZOMB)
                continue;
            if (pid == pinfo[i].pi_pid)
            {
                return pinfo[i].pi_comm;
            }
        }
    }
    return "";
}

int main(int argc, char** argv)
{
    for(int i=1; i<argc; ++i)
    {
        int pid = atoi(argv[i]);
        string name = getProcName(pid);
        cout << "pid: " << pid << " == '" << name << "'" << endl;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    getprocs 可能是您想要的。我在 AIX 5.x 下创建了它。

    我有一个小程序循环遍历所有进程并转储它们的信息。

    while ((numproc = getprocs(pinfo, sizeof(struct procsinfo),
            NULL,
            0,
            &index,
            MAXPROCS)) > 0  ) {
                for (i = 0;i < numproc; i++) {
                        /* skip zombie processes */
                        if (pinfo[i].pi_state==SZOMB)
                           continue;
                        printf("%-6d %-4d %-10d %-16s\n", pinfo[i].pi_pid, pinfo[i].pi_uid, pinfo[i].pi_start, pinfo[i].pi_comm);
                }
    }
    
    ....
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2019-09-20
      • 2023-04-10
      相关资源
      最近更新 更多