Subject: Re: new sysctl(KERN_PROC, ...) interface (was: sysinfo(2))
To: None <>
From: Brian C. Grayson <>
List: tech-kern
Date: 04/15/2000 23:12:27
On Sat, Apr 15, 2000 at 08:51:40PM -0500, wrote:
> What about the make snapshot, return snapshot split.  sysctl(KINFO_PROC2)
> with a total length of zero would take a snapshot of the processes 
> at that point, save it until the next call with a non-zero total length,
> and return the number of entries needed.  Then the program doing the
> sysctl could allocate exactly as much memory as needed and do another
> call with a non-zero length to get the saved data back.

  Why do the snapshot save?  In the procfs-groveling I added to
/bin/ps, I took the following approach:

  1.  Query maxproc via a sysctl to see what kern.maxproc is set to.
  2.  Double the return value, to avoid a race where someone
    twiddles it and then forks a lot.  Hopefully they can't
    manage to fork too much stuff between your first sysctl
    call and your grab of all the info you need.
  3.  Malloc space for that many entries, and call the
  4.  Look at how many entries you actually got, and realloc()
    your buffers if you're concerned about long-term memory
    and/or virtual address space usage.

  Sure, it mallocs more memory than needed, but the memory
isn't really allocated until touched, only the address space is
allocated.  Or did all the traffic about conservative
malloc/swap overcommit cause a change?