Need help getting a pid based process name on aix

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

thanks

+4
source share
2 answers

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

I have a small procedure that cycles through all the processes and uploads 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);
            }
}

....
+1
source

I understand that this is an old question. But, to convert @CoreyStup's answer to a function closer to OP, I suggest 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;
}
+1
source

All Articles