Need help in getting the process name based on the pid in aix

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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??

Thanks

回答1:

getprocs is likely what you want. I created this under AIX 5.x.

I have a little routine that cycles thru all processes and dumps their information.

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


回答2:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!