Unable to retrieve information form HP-UX pst_status object

Posted by bogertron on Stack Overflow See other posts from Stack Overflow or by bogertron
Published on 2010-03-15T23:14:44Z Indexed on 2010/03/15 23:19 UTC
Read the original article Hit count: 721

Filed under:
|
|

I am attempting to get process information by using the HP-UX C/C++ library. After scouring the internet, I have discovered that HP-UX has the pstat.h header file which allows programmers to retrieve the process information. After attempting to understand code from the internet hp website, I attempted to create a little test sample to comprehend what the code does.

I attempted to use example 3, however, I ran into several issues. The first issue came when I attempted to execute the following line of code:

(void)printf("pid is %d, command is %s\n",  pst[i].pst_pid, pst[i].pst_ucomm); 

When I attempted to print the string, I hit a memory fault.

So I decided to attempt to see what the string is and came up with the following:

#include <sys/param.h>
#include <sys/pstat.h>
#include <sys/unistd.h>
#include <string.h>


int main(int argc, char** argv)
{

#define BURST ((size_t)10)
        struct pst_status pst[BURST];
        int i, count;
        int idx = 0; /* index within the context */
        int index = 0;
        /* loop until count == 0, will occur all have been returned */

        while ((count=pstat_getproc(pst, sizeof(pst[0]),BURST,idx))>0)
        {
                index = 0;
                printf("index: %d", index);
                /* got count (max of BURST) this time. process them */
                while (pst[i].pst_ucomm[index] != '\0')
                {
                        printf("%c", pst[i].pst_ucomm[index]);
                        index++;
                }
                printf("\n");
                for (i = 0; i < count; i++) {
                        printf("pid is %d, command is \n",  pst[i].pst_pid);
                }
/*
 * now go back and do it again, using the next index after
 * the current 'burst'
 */
                idx = pst[count-1].pst_idx + 1;
        }
        if (count ==     -1)
                perror("pstat_getproc()");
#undef BURST

}

Unfortunately, what happens is that I get the first process printed, then pid is 2, command is pid is 2, command is pid is 2, command is...

I know that I must be doing something foolish since my C/C++ skills are not that great, but I cannot figure out what the issue is since the code is largely copied from the hp website. So here's the question(s) for clarity:
1. Why can't printf("%s", pst[i].pst_ucomm); handle strings?
2. Why can't I iterate over the processes in the system?

Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++