PROCPS_PIDS(3)
π PROCPS_PIDS(3) π
okay bestie, gather round, because this is the procps_pids page from the Library Functions Manual (section 3, obvi) and it's literally the API to access process information in the /proc filesystem. it's giving sysadmin main character energy and i'm obsessed. let's go π«Ά
β¨ NAME β¨
procps_pids β API to access process information in the /proc filesystem
πΈ SYNOPSIS πΈ
#include <libproc2/pids.h>
int procps_pids_new (struct pids_info **info, enum pids_item *items, int numitems);
int procps_pids_ref (struct pids_info *info);
int procps_pids_unref (struct pids_info **info);
struct pids_stack *procps_pids_get (
struct pids_info *info,
enum pids_fetch_type which);
struct pids_fetch *procps_pids_reap (
struct pids_info *info,
enum pids_fetch_type which);
struct pids_fetch *procps_pids_select (
struct pids_info *info,
unsigned *these,
int numthese,
enum pids_select_type which);
struct pids_stack **procps_pids_sort (
struct pids_info *info,
struct pids_stack *stacks[],
int numstacked,
enum pids_item sortitem,
enum pids_sort_order order);
int procps_pids_reset (
struct pids_info *info,
enum pids_item *newitems,
int newnumitems);
struct pids_stack *fatal_proc_unmounted (
struct pids_info *info,
int return_self);
and when you link it, bestie: link with -lproc2. that's the whole vibe check right there π
π¦ DESCRIPTION π¦
π Overview π
so the whole glow-up of this interface revolves around one lil 'result' structure β it's holding an 'item' plus its value (and that value lives in a union with standard C language types as members, cute right?). every single 'result' structure is automatically allocated and handed to you by the library. you don't malloc, you don't free, you just vibe. β¨
now here's the slay part: if you hand over an array of 'items', those structures get organized as a 'stack', and that stack can potentially yield so many results with just a single function call. so like, a 'stack' is basically a variable length record whose content and order is determined solely by you, the user. you're the main character of your own data structure fr fr π
and as part of this whole interface there are two unique enumerators β the 'noop' and 'extra' items β and they exist purely to hold user values. the library never sets them, BUT the 'extra' result will be zeroed with each library interaction. noted. π
the pids.h file is gonna be your essential document while you're developing, no cap. that's where you'll find the available items, their return type (that's the 'result' struct member name) and the source for such values. plus additional enumerators and structures are documented in there too. keep it open in another tab, bestie π
π« Usage π«
here's a typical sequence of calls to this interface, in order:
fatal_proc_unmounted()procps_pids_new()procps_pids_get(),procps_pids_reap()orprocps_pids_select()procps_pids_unref()
the get function is an iterator for successive PIDs/TIDs, returning those 'items' you previously identified via new or reset. it's giving little conveyor belt of processes π
two functions support unpredictable variable outcomes. the reap function gathers data for all processes while the select function deals with specific PIDs or UIDs. both can return multiple 'stacks', each containing multiple 'result' structures. and optionally? you can choose to sort such results. cute! π
to exploit any 'stack', and to access individual 'result' structures, you need a relative_enum β exactly like the VAL macro defined in the header file. such values could be hard coded as: 0 through numitems-1. however, this need is typically satisfied by creating your own enumerators corresponding to the order of the 'items' array. do it the nice way, bestie πΈ
π Caveats π
the <pids> API differs from others in that those items of interest must be provided at new or reset time, the latter being unique to this API. and listen: if either the items or numitems parameter is zero at new time, then reset becomes mandatory before issuing any other call. otherwise, procps_pids_reset() should only be called when the order or number of items in your stacks is changed.
for the new and unref functions, the address of an info struct pointer must be supplied. with new it must have been initialized to NULL. with unref it will be reset to NULL if the reference count reaches zero. so clean. so tidy. we love a well-behaved pointer π§
the get and reap functions use the which parameter to specify whether just tasks or both tasks and threads are to be fetched. pick your fighter.
the select function requires an array of PIDs or UIDs, since these β along with numthese β identify which processes are to be fetched. this function then operates as a subset of reap. it's reap's little sibling, lowkey π
when using the sort function, the parameters stacks and numstacked would normally be those returned in the 'pids_fetch' structure. like, that's just the natural flow.
lastly, a fatal_proc_unmounted function may be called before any other function to ensure that the /proc/ directory is mounted. as such, the info parameter would be NULL and the return_self parameter zero. if, however, some items are desired for the issuing program (a return_self other than zero) then the new call must precede it, to identify the items and obtain the required info pointer. got it? good. π
β RETURN VALUE β
π₯ Functions Returning an 'int' π₯
an error will be indicated by a negative number that is always the inverse of some well known errno.h value.
success is indicated by a zero return value. slay. however, the ref and unref functions return the current info structure reference count β so don't get it twisted, those two are giving you a count, not just a yes/no π
π₯ Functions Returning an 'address' π₯
an error will be indicated by a NULL return pointer, with the reason found in the formal errno value.
success is indicated by a pointer to the named structure. however, if one survives the fatal_proc_unmounted call, NULL is always returned when return_self is zero. periodt.
π DEBUGGING π
to aid in program development, there are two procps-ng provisions that can be exploited. yes bestie, you get little helper features. we love to see it π«Ά
the first is a supplied file named libproc.supp which may be useful when developing a multi-threaded application. when used with the valgrind --suppressions= option, warnings associated with the procps library itself are avoided.
such warnings arise because the library handles heap based allocations in a thread-safe manner. a single-threaded application will not receive those warnings. so if you're single-threaded and confused about where the warnings went β that's why, babe π
the second provision can help ensure 'result' member references agree with library expectations. it assumes that a supplied macro in the header file is used to access the 'result' value.
this feature can be activated through either of the following methods, and any discrepancies will be written to stderr.
- Add
CFLAGS='-DXTRA_PROCPS_DEBUG'to any other./configureoptions your project may employ. - Add
#include <libproc2/xtra-procps-debug.h>to any program after the#include <libproc2/pids.h>.
β οΈ okay this next part is important bestie: this verification feature incurs substantial overhead. therefore, it is important that it not be activated for a production/release build. do not ship it. i'm serious. π ββοΈ
π ENVIRONMENT VARIABLE(S) π
the value set for the following is unimportant β just its presence. literally just show up, the number doesn't matter, it's giving "attend the party, don't bring a gift" energy β¨
LIBPROC_HIDE_KERNELβ this will hide kernel threads which would otherwise be returned with aprocps_pids_get,procps_pids_selectorprocps_pids_reapcall.
π SEE ALSO π
procps(3), procps_misc(3), proc(5).
and that's the whole page, bestie π¦ go forth and read your /proc like the icon you are. this was brought to you by procps-ng, dated 2004-09-03 β vintage, but still serving looks, just like this manual. mwah π
π
π¦ where this page comes from
- alpine 3.23: procps-ng-doc 4.0.5-r0 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- alpine 3.24: procps-ng-doc 4.0.6-r0 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- arch rolling: procps-ng 4.0.7-1 Β· GPL AND LGPL
- fedora 44: procps-ng-devel 4.0.6-1.fc44 Β· GPL-2.0-or-later AND LGPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: procps-ng-devel 4.0.6-1.fc44 Β· GPL-2.0-or-later AND LGPL-2.0-or-later AND LGPL-2.1-or-later
- nixos 26.05: procps 4.0.6
- nixpkgs unstable: procps 4.0.7
- opensuse-leap 16.0: procps-devel 4.0.5-160000.2.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: procps-devel 4.0.5-160000.2.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: procps-devel 4.0.5-160000.2.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: procps-devel 4.0.5-160000.2.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- ubuntu stonking: procps 2:4.0.6-3ubuntu1 Β· LGPL-2.1-or-later
- Copyright (c) 2020-2024 Jim Warner <james.warner@comcast.net>
- Copyright (c) 2020-2024 Craig Small <csmall@dropbear.xyz>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms