Reading the process table?

Paul Turner pturner at ese.ese.ogi.edu
Sun Feb 10 05:29:07 AEST 1991


System is a S/6000 320, AIX 3.1 w/3001.

I'm interested in reading the process table. I notice that in <sys/proc.h>
that there is an external variable declared

extern struct proc proc[NPROC];         /* the process table, itself */

Looks like this is it, and nm /unix indicates it is there. I'm using
the knlist() call to get it. The following is code modified from
code posted to the net by Charley Kline (he was getting load averages).
Any help? (better yet, has anyone ported top?). If not obvious, I
assure you, I'm no kernel hacker.

Thanks,

Paul J Turner
pturner at ese.ogi.edu

-- cut here --

#include <stdio.h>
#include <sys/proc.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <nlist.h>

main()
{
    void GetProcTable();
    while (1) {
        GetProcTable();
        sleep(1);
    }
}

struct nlist kernelnames[] = {
                              {"proc", 0, 0, 0, 0, 0},
                              {NULL, 0, 0, 0, 0, 0},
};

void GetProcTable()
{
    struct proc *p;
    static initted = 0;
    static int fd;
    /*
     * * Do stuff we only need to do once per invocation, like opening * the
     * kmem file and fetching the parts of the symbol table. 
     */
    if (!initted) {
        initted = 1;
        knlist(kernelnames, 1, sizeof(struct nlist));
        fd = open("/dev/kmem", O_RDONLY);
        if (fd < 0) {
            perror("kmem");
            exit(1);
        }
    }
    lseek(fd, kernelnames[0].n_value, SEEK_SET);
    read(fd, p, sizeof(struct proc *));
    /* print some stuff, what do I do now?  */
    printf("%u %u %u %u\n", kernelnames[0].n_value, p->p_uid, PROCMASK(p->p_pid), p->p_ppid);
}



More information about the Comp.unix.aix mailing list