struct proc - basic question

Peter Pentchev roam at ringlet.net
Mon Sep 13 05:50:02 PDT 2004


On Mon, Sep 13, 2004 at 02:01:39PM +0200, Joanna Sledzik wrote:
> Hi :)
> I'm very very begginer in Unix system programming.
> What function should I use to catch the struct proc for some process?
> Is it possible to get the pointer to struct proc using for example the
> pid_t pid as an argument?

Yes, the function you are looking for is

  #include <sys/proc.h>

  struct proc *pfind(pid_t pid)

You pass it a pid_t argument and it returns a pointer to a proc
structure.

However, note that the pfind() function is only available to code
running in kernel space - it has to be invoked either from a kernel
module, or from code compiled into the kernel itself.

If you want to get a process's struct proc from a user-space program,
there are two ways to go about it:

- use libkvm to muck around the kernel's memory: this is a pretty bad
  approach for most cases, especially as the kernel's internal
  structures may change even as you try to examine them and follow the
  list pointers.  If you choose this approach, take a look at the kvm(3)
  manual page, and note that you need to look for the 'allproc' list,
  which is usually a singly-linked list, but see <sys/proc.h> for
  details.

- use the sysctl(3) interface to fetch the value of the kern.proc.all
  sysctl.  It is a snapshot of the current process info, see the
  kinfo_proc structure and the comments around it in the <sys/user.h>
  file for more information.

G'luck,
Peter

-- 
Peter Pentchev	roam at ringlet.net    roam at cnsys.bg    roam at FreeBSD.org
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
If the meanings of 'true' and 'false' were switched, then this sentence wouldn't be false.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20040913/86844bbb/attachment.bin


More information about the freebsd-hackers mailing list