---
name: "sg_process_compare_res"
section: "3"
platform: "linux"
package: "libstatgrab"
version: "0.92.1-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sg_process_compare_res"
---

# sg\_process\_compare\_res(3)

get process statistics

## NAME

sg\_get\_process\_stats, sg\_get\_process\_stats\_r, sg\_get\_process\_count, sg\_get\_process\_count\_of, sg\_get\_process\_count\_r, sg\_free\_process\_count, sg\_process\_compare\_name, sg\_process\_compare\_pid, sg\_process\_compare\_uid, sg\_process\_compare\_gid, sg\_process\_compare\_size, sg\_process\_compare\_res, sg\_process\_compare\_cpu, sg\_process\_compare\_time - get process statistics

## SYNOPSIS

```
FC#include <statgrab.h>F[0]
```

FCsg\_process\_stats \***sg\_get\_process\_stats**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_process\_stats \***sg\_get\_process\_stats\_r**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_error **sg\_free\_process\_stats**F\[0\]  FC(sg\_process\_stats \**data*);F\[0\]

FCsg\_process\_count \***sg\_get\_process\_count**F\[0\]  FC(void);F\[0\]

FCsg\_process\_count \***sg\_get\_process\_count\_of**F\[0\]  FC(sg\_process\_count\_source *pcs*);F\[0\]

FCsg\_process\_count \***sg\_get\_process\_count\_r**F\[0\]  FC(sg\_process\_stats const \**whereof*);F\[0\]

FCsg\_error **sg\_free\_process\_count**F\[0\]  FC(sg\_process\_count \**data*);F\[0\]

FCint **sg\_process\_compare\_name**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_pid**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_uid**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_gid**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_size**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_res**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_cpu**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

FCint **sg\_process\_compare\_time**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

## DESCRIPTION

The FC**sg\_get\_process\_stats**F\[0\] functions provide statistics about the currently running processes. Both functions, FC**sg\_get\_process\_stats**F\[0\]() and FC**sg\_get\_process\_stats\_r**F\[0\](), take an optional FCentriesF\[0\] parameter, which points (when given) to a size\_t to take the number of returned vector entries.

The functions FC**sg\_get\_process\_count\_of**F\[0\]() and FC**sg\_get\_process\_count\_r**F\[0\]() provide an aggregated view of the process table - they deliver the amount of processes per process state. The FC**sg\_get\_process\_count**F\[0\]() is in fact a preprocessor macro for backward compatibility and calls FC**sg\_get\_process\_count\_of**F\[0\]() with the parameter FCpcsF\[0\] of sg\_entire\_process\_count to emulate the behavior until 0.17.

**API Shortcut**

| | | |
| --- | --- | --- |
| function | returns | data owner |
| sg\_get\_process\_stats | FCsg\_process\_statsF\[0\] \* | libstatgrab (thread local) |
| sg\_get\_process\_stats\_r | FCsg\_process\_statsF\[0\] \* | caller |
| sg\_get\_process\_count\_of | FCsg\_process\_countF\[0\] \* | libstatgrab (thread local) |
| sg\_get\_process\_count\_r | FCsg\_process\_countF\[0\] \* | caller |

The FCsg\_process\_statsF\[0\] vectors received from FC**sg\_get\_process\_stats\_r**F\[0\]() or the FCsg\_process\_countF\[0\] summaries received from FC**sg\_get\_process\_count\_r**F\[0\] must be freed using FC**sg\_free\_process\_stats**F\[0\]() or FC**sg\_free\_process\_count**F\[0\](), respectively. The caller is responsible for doing it when the data isn't needed any more.

FC**sg\_process\_compare\_name**F\[0\]\
 FC**sg\_process\_compare\_pid**F\[0\]\
 FC**sg\_process\_compare\_uid**F\[0\]\
 FC**sg\_process\_compare\_gid**F\[0\]\
 FC**sg\_process\_compare\_size**F\[0\]\
 FC**sg\_process\_compare\_res**F\[0\]\
 FC**sg\_process\_compare\_cpu**F\[0\]\
 FC**sg\_process\_compare\_time**F\[0\]

These functions compare two sg\_process\_stats entries, and return an int to represent which one is greater. The main use of these functions is to be passed to qsort to sort the sg\_process\_stats by the given type.

**Example**

```
FC
size_t entries;
sg_process_stats *proc_stats = NULL;
while( NULL != ( proc_stats = sg_get_process_stats_r(&entries) ) ) {
    /* order entries by comparing the process identifier */
    qsort( proc_stats, entries, sizeof(proc_stats[0]), &sg_process_compare_pid );
    show_proc_stats( proc_stats );
    sg_free_process_stats( proc_stats );
}
        F[0]
```

## RETURN VALUES

The structure returned by sg\_get\_process\_stats is of type FCsg\_process\_statsF\[0\].

```
FC
typedef struct {
        char *process_name;
        char *proctitle;
        pid_t pid;    /* process identifier */
        pid_t parent; /* Parent pid */
        pid_t pgid;   /* process id of process group leader */
        pid_t sessid; /* session id of the session the process belongs to */
        uid_t uid;
        uid_t euid;
        gid_t gid;
        gid_t egid;
        unsigned long long context_switches;
        unsigned long long voluntary_context_switches;
        unsigned long long involuntary_context_switches;
        unsigned long long proc_size; /* in bytes */
        unsigned long long proc_resident; /* in bytes */
        time_t start_time; /* When was the process started */
        time_t time_spent; /* time running in seconds */
        double cpu_percent;
        int nice;
        sg_process_state state;
        time_t systime;
} sg_process_stats;
    F[0]
```

```
FC
typedef enum {
        SG_PROCESS_STATE_RUNNING,
        SG_PROCESS_STATE_SLEEPING,
        SG_PROCESS_STATE_STOPPED,
        SG_PROCESS_STATE_ZOMBIE,
        SG_PROCESS_STATE_UNKNOWN
} sg_process_state;
    F[0]
```

- FCprocess\_nameF\[0\]

  The name of the command that was run. The content of this field heavily depends on the underlying operating system, some store the basename the executable passes to the FC**exec**F\[0\](2) system call, some the entire path. Most OS restrict the size of this field - some like the \*BSD family to a very low value of 15 bytes.

  This field is usually immutable for userland processes.

- FCproctitleF\[0\]

  The command line (the "title") of the process. Take note - this can be modified by the process, so isn't guaranteed to be the original command line.

- FCpidF\[0\]

  The process ID.

- FCparentF\[0\]

  The parent process ID.

- FCpgidF\[0\]

  The process ID of the process group leader.

- FCsessidF\[0\]

  Session id of the session the process belongs to.

- FCuidF\[0\]

  The ID of the user the process is running as.

- FCeuidF\[0\]

  The ID of the effective user the process is running as.

- FCgidF\[0\]

  The ID of the group the process is running as.

- FCegidF\[0\]

  The ID of the effective group the process is running as.

- FCcontext\_switchesF\[0\]

  The number of context switches of this process (voluntary and involuntary).

- FCvoluntary\_context\_switchesF\[0\]

  The number of voluntary context switches of this process (eg. by calling FC**sched\_yield**F\[0\]() or FC**sleep**F\[0\]()).

- FCinvoluntary\_context\_switchesF\[0\]

  The number of involuntary context switches of this process (eg. time slice exhausted or signal sent).

- FCproc\_sizeF\[0\]

  The virtual memory size of the process in bytes.

- FCproc\_residentF\[0\]

  The size of the process that's resident in memory.

- FCstart\_timeF\[0\]

  The time when the process has been started in seconds since epoch.

- FCtime\_spentF\[0\]

  The number of seconds the process has been running (user+system time, without time spent by child processes).

- FCcpu\_percentF\[0\]

  The current percentage of CPU the process is using.

- FCniceF\[0\]

  The nice value of the process.

- FCstateF\[0\]

  The current state of the process. See sg\_process\_state for permitted values.

- FCsystimeF\[0\]

  The time in seconds since epoch of the moment where the present statistic has been created. This might be (but doesn't have to be) the same moment for all returned entries, regardless whether they're fetched with one snapshot or puzzled from some kind of procfs.

The structure returned by sg\_get\_process\_count\_of and sg\_get\_process\_count\_r is of type FCsg\_process\_countF\[0\].

```
FC
typedef enum sg_process_count_source {
        sg_entire_process_count,
        sg_last_process_count
} sg_process_count_source;
    F[0]
```

```
FC
typedef struct{
        unsigned long long total;
        unsigned long long running;
        unsigned long long sleeping;
        unsigned long long stopped;
        unsigned long long zombie;
        unsigned long long unknown;
        time_t systime;
}sg_process_count;
    F[0]
```

- FCtotalF\[0\]

  The total number of processes.

- FCrunningF\[0\]

  The number of running processes.

- FCsleepingF\[0\]

  The number of sleeping processes.

- FCstoppedF\[0\]

  The number of stopped processes.

- FCzombieF\[0\]

  The number of zombie processes.

- FCunknownF\[0\]

  The number of processes not matching any of above named categories.

- FCsystimeF\[0\]

  The time in seconds since epoch of the moment where the present statistic has been created.

## BUGS

The very first call of FC**sg\_get\_process\_count\_of**F\[0\](sg\_last\_process\_count) will return the same as FC**sg\_get\_process\_count\_of**F\[0\](sg\_entire\_process\_count).

The compare functions exist rather for backward compatibility than for functionality enhancements. Limited flexibility (e.g. reverse order) and lack of optimising opportunities for the compiler leads to the recommendation to implement the required compare routines locally.

## SEE ALSO

[**statgrab**(3)](/statgrab/3.md)

## WEBSITE

⟨https://libstatgrab.org/⟩
