需要帮助以获取基于aix中pid的进程名称 [英] Need help in getting the process name based on the pid in aix

查看:131
本文介绍了需要帮助以获取基于aix中pid的进程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在AIX环境中编写一个C程序,这将为我提供进程名称。
我可以获取pid,但不能获取基于pid的进程名称。在aix环境中是否可以使用任何特定的系统调用?

I need to write a C program in AIX environment which will give me the process name. I can get the pid but not the process name based on the pid. Any specific system calls available in aix environment??

谢谢

推荐答案

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

I realize this is an old question. But, to convert the @CoreyStup answer into a function that more closely addresses the OP, I offer this: (tested on AIX 6.1, using: g++ -o pn pn.cc)

--- 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;
}

这篇关于需要帮助以获取基于aix中pid的进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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