Looking for a convenient way in C to retrieve CPU and memory usage of a process

Matthew Seaman m.seaman at infracaninophile.co.uk
Sun Dec 27 08:28:20 UTC 2009


Manish Jain wrote:

> I am looking for a convenient way using C to retrieve the current CPU 
> and memory utilization of a process of which I have the pid. Can 
> somebody please give me a hint of which system-calls/library-functions 
> to use for this ? I don't want to use the system() function or grep for 
> information via the /proc filesystem.
> 
> I would be grateful if you could also please mention whether the 
> suggested method[s] is/are FreeBSD-specific or would be portable to 
> other environments like Solaris/Linux ?

Use the source, Luke.  In this cse, probably a very good place to start is
with top(1) since it already does a lot of what you want.  Start with 
/usr/src/contrib/top/

This sort of thing generally requires reading /dev/kmem, which is very OS
dependent.  The same sort of approach will probably work on most Unix-oid
OSes, but the details will be significantly different.

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20091227/c40c207d/signature.pgp


More information about the freebsd-questions mailing list