C Find child processes from parent pid

Hello,
This is the first time I've posted here, I'm wondering how I can find child processes from a parent's pid? preferably a C function to call would be great or if it's in the file system anywhere but no procfs.

Please let me know if you need any more details.
Thanks,
Gibb
 
The FreeBSD kernel and its accompanying base system are open source. ? You can have a look how ps(1) is implemented. ? If you have installed the source code, it is /usr/src/bin/ps/ps.c otherwise cgit. ? The crucial function seems to be kvm_getprocs(3). From there you can filter your results.
[SUB]PS: If you want to know your own children processes, you are supposed to write them down as you fork(2), but you’re probably not asking about that such a situation.[/SUB]​
 
It's important to add the (correct!) solution offered by Kai Burghardt is non-portable (specific to FreeBSD). Neither the C standard nor POSIX offer such a functionality. For a "normal" application program, you're expected to be only interested in processes you "own" (forked yourself), and, as mentioned, keep track of their PIDs yourself.
 
With a view to portability, forking off a process to run ps ax -oppid,pid would work on both FreeBSD and Debian (and I suspect other Linux variants).
 
Decoded …

… en/books/handbook/bsdinstall/#:~:text=src,space. …

Thanks, #:~:text=src,space. is not an anchor.

… If you have installed the source code, …

The FreeBSD Handbook does not yet cover official packages.

gibbthegibbon assuming FreeBSD-CURRENT for development, you can add a package (and not expect automated updates).

At the time of writing, 2024-09-15 07:49:

pkg add https://pkg.freebsd.org/FreeBSD:15:amd64/base_latest/FreeBSD-src-15.snap20240914203449.pkg

Alternatively, create your pkgbase repo configuration file then pkg install FreeBSD-src (and expect updates).

Code:
% cat /usr/local/etc/pkg/repos/FreeBSD-base.conf
FreeBSD-base: {
  url: "pkg+https://pkg.freebsd.org/${ABI}/base_latest",
  signature_type: "fingerprints",
  fingerprints: "/usr/share/keys/pkg",
  mirror_type: "srv",
  enabled: yes
}
%



Alongside the FreeBSD-src package, we have FreeBSD-src-sys.

Code:
% head -n 4 /usr/src/README.md
FreeBSD Source:
---------------
This is the top level of the FreeBSD source directory.

% pkg info --list FreeBSD-src | wc -l
   69633
% head -n 7 /usr/src/sys/README.md
FreeBSD Kernel Source:
----------------------

This directory contains the source files and build glue that make up the FreeBSD
kernel and its modules, including both original and contributed software.

Kernel configuration files are located in the `conf/` subdirectory of each
% pkg info --list FreeBSD-src-sys | wc -l
   31609
%
 
Welcome to The FreeBSD Forums.

I would have suggested bin/pkill/pkill.c, but I see that's already an answer in Stack Overflow …
Thanks, I also posted on stack overflow because I thought this post was not allowed since there was a line through it, that's my bad but I've managed to get a function which works from the pkill source code.

I was just wondering if a process stored the pids of it's children anywhere but it appears not, thanks everyone for the help!
 
Back
Top