Results 1 to 2 of 2
Hi all,
I'm using this code to obtain the process page file usage:
Code:
long getProcessPageFileUsage()
{
int pid;
char name[1024];
char state;
int ppid, pgrp, session, tty, tpgid;
unsigned ...
- 07-04-2008 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 6
Process page file usage in C
Hi all,
I'm using this code to obtain the process page file usage:
The problem is that it randomly crashes in fscanf().Code:long getProcessPageFileUsage() { int pid; char name[1024]; char state; int ppid, pgrp, session, tty, tpgid; unsigned flags, minflt, cminflt, majflt, cmajflt; int utime, stime, cutime, cstime, counter, priority, timeout; unsigned itrealvalue, starttime, vsize = 0, rss = 0; FILE *f = fopen("/proc/self/stat", "r"); fscanf(f, "%d %s %c %d %d %d %d %d %u " "%u %u %u %u %d %d %d " "%d %d %d %u %u %d " "%u %u", &pid, name, &state, &ppid, &pgrp, &session, &tty, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime, &cutime, &cstime, &counter, &priority, &timeout, &itrealvalue, &starttime, &vsize, &rss); fclose(f); return rss * sysconf(_SC_PAGESIZE); }
That code is taken from http://www.cgal.org/Manual/3.2/inclu...Memory_sizer.h
I'm calling it from Java, so I have the hot spot crash log:
Any ideas why it is failing? Besides checking the file pointer not to be NULL. Performing a read lock on that file, I don't think it will help. Since the kernel is changing the file.Code:Stack: [0x746f1000,0x74742000], sp=0x747406b8, free space=317k Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C [libc.so.6+0x472a3] _IO_vfscanf+0xa3 C [libc.so.6+0x4c6a1] vfscanf+0x31 C [libc.so.6+0x50c2f] fscanf+0x1f C [libnatutil.so+0x19e48] _Z23getProcessPageFileUsagev+0xfe C [libnatutil.so+0x19e87] Java_com_sync_util_NatUtil_getProcessPageFileUsage+0x17
- 07-04-2008 #2
That's puzzling, for sure. As a matter of superstition, I never use fscanf(). Try declaring a local
and doing an fgets() followed by an sscanf(), just to see what happens.Code:char fred[1024];
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote