Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    6

    Exclamation Process page file usage in C

    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 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);
    }
    The problem is that it randomly crashes in fscanf().
    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:

    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
    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.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    That's puzzling, for sure. As a matter of superstition, I never use fscanf(). Try declaring a local
    Code:
    char fred[1024];
    and doing an fgets() followed by an sscanf(), just to see what happens.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...