HP-UX Reference (11i v3 07/02) - 2 System Calls (vol 5)
p
pstat(2) pstat(2)
for (i = 0; i < count; i++) {
(void)printf("pid is %lld, command is %s\n",
pst[i].pst_pid, pst[i].pst_ucomm);
}
/*
* now go back and do it again, using the next index after
* the current ’burst’
*/
idx = pst[count-1].pst_idx + 1;
(void)memset(pst,0,BURST*sizeof(struct pst_status));
}
if (count == -1)
perror("pstat_getproc()");
#undef BURST
}
Example 4B
Get information about all processes, 10 at a time. We do it this way since the current count of active
processes is unknown. The program explicitly handles the possibility of [EOVERFLOW], as it is using data
from a field that may in principle overflow.
main()
{
#define BURST ((size_t)10)
struct pst_status pst[BURST];
int i, ret, count;
int idx = 0; /* pstat index within the Process pstat context */
do {
(void)memset(pst,0,BURST*sizeof(struct pst_status));
count = 0;
ret = pstat_getproc(pst, sizeof(pst[0]),BURST,idx);
if (ret > 0) {
/* normal case: got ret Process contexts to report */
count = ret;
}
else if ((ret == 0)) {
/* normal case: finished all Process contexts */
count = 0;
}
else if ((ret == -1) && (errno == EOVERFLOW)) {
/* EOVERFLOW case: at least one Process context hit it,
so we find the one that caused this error. Neither of
the fields we are interested in has a validity macro
defined, so we just count the number of structures
returned to us */
for (i=0;i<BURST;i++) {
count++;
if (pst[i].pst_valid!=-1) {
break;
}
}
}
else if ((ret == -1)) {
/* error case */
perror("pstat_getproc()");
count = 0;
}
HP-UX 11i Version 3: February 2007 − 19 − Hewlett-Packard Company 309