需要帮助以获取基于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;
}