Need help in getting the process name based on the

2019-06-26 03:25发布

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

标签: aix
2条回答
爷、活的狠高调
2楼-- · 2019-06-26 04:08

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;
}
查看更多
叼着烟拽天下
3楼-- · 2019-06-26 04:28

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

....
查看更多
登录 后发表回答