Unix Programming - how to use ioctl to check out memory usage

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > how to use ioctl to check out memory usage





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to use ioctl to check out memory usage
lanchen.ubuntu@googlemail.com

2006-01-31, 7:21 pm

Hi all,

I tried to output memory usage information while the process is
executing at a particular time. I found out some people in this group
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 << "pid:" << pid << " init memory:" << ps_info.pr_bysize << endl;

char ** pString;

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

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


pString[k] = p;
}

ioctl(fd, PIOCPSINFO, &ps_info);

cout << "pid:" << pid << " after memory:" << ps_info.pr_bysize <<
endl;

close(fd);

for(int k = 0; k < 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,

Patricia

Rainer Temme

2006-01-31, 7:21 pm

lanchen.ubuntu@googlemail.com wrote:

Hi lanchen.ubuntu@googlemail.com / Patricia,

see my comments in your code ...

> #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);


do you really want to open "proc/12345"
.... isn't it "/proc/12345" that you mean?

may be you correct this to sprintf(fname,"/proc/%ld",pid);

> int fd;
> fd=open(fname, O_RDONLY);


it's always a good idea to check returncodes ...
fd=open(fname,O_RDONLY);
if (fd<0)
{
/* some error handling here ... */
}

> prpsinfo_t ps_info;
>
> ioctl(fd, PIOCPSINFO, &ps_info);


see above ...
int rc;
rc=ioctl(fd,.....)
if (rc<0)
{
/* some error handling */
}
>
> cout << "pid:" << pid << " init memory:" << ps_info.pr_bysize << endl;
>
> char ** pString;
>
> //char * pInteger[128];
> pString = new char * [128];
>
> for(int k = 0; k < 128 ; k++)
> {
> char *p = new char [1024 * 1024];
>
>
> pString[k] = p;
> }
>
> ioctl(fd, PIOCPSINFO, &ps_info);


again errorchecking

>
> cout << "pid:" << pid << " after memory:" << ps_info.pr_bysize <<
> endl;
>
> close(fd);
>
> for(int k = 0; k < 128; k++)
> delete [](pString[k]);
>
> delete []pString;
> }


Regards ... Rainer
lanchen.ubuntu@googlemail.com

2006-01-31, 7:21 pm

thank you. I got so stupid error. It works now

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com