how to use ioctl to check out memory usage

Hi all,

I tried to output memory usage information while the process is
executing at a particular time. I found out some people
suggesting calling the ioctl. I followed it and wrote a test example:

#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <fcntl.h>
#include <sys/procfs.h>

int main(int * argc, char ** argv)
{
pid_t pid = getpid();
char fname[100];
sprintf(fname, "/proc/%ld", pid);
int fd;
fd=open(fname, O_RDONLY);
prpsinfo_t ps_info;

    ioctl\(fd, PIOCPSINFO, &ps_info\);

    cout &lt;&lt; "pid:" &lt;&lt; pid &lt;&lt; " init memory:" &lt;&lt; ps\_info.pr_bysize &lt;&lt; endl;

    char ** pString;

    //char * pInteger[128];
    pString = new char * [128];

    for\(int k = 0; k &lt; 128 ; k\+\+\)
    \{
            char *p = new char [1024 * 1024];

            pString[k] = p;

}

    ioctl\(fd, PIOCPSINFO, &ps_info\);

    cout &lt;&lt; "pid:" &lt;&lt; pid &lt;&lt; " after memory:" &lt;&lt; ps\_info.pr_bysize &lt;&lt;

endl;

    close\(fd\);

    for\(int k = 0; k &lt; 128; k\+\+\)
            delete []\(pString[k]\);

            delete []pString;

}

And compiled it with Sun CC on solaris5.8. I thought the second output
must be more than 128M. But the real one is : memory:0. Have I misused
ioctl or something else?

Best Regards,

Lan Chen

I don't know what you're doing - but getrusage() is the standard way to get cpu usage - usually in ms.

I got:
143> /opt/SUNWspro/bin/CC proc.c -o proc
144> ./proc
pid:15832 init memory:1208320
pid:15832 after memory:135426048

Try opening "/proc/self" which is how everyone else does it. But getting your own pid should work too. Check the error codes after each system call. That will give you a clue.