Process

The Fossil System Process module provides low-level, cross-platform access to process enumeration and management facilities. It allows callers to query process identity, hierarchy, resource usage, and execution metadata, as well as retrieve environment variables and terminate processes in a controlled manner. The C API emphasizes fixed-size structures, deterministic memory usage, and best-effort introspection suitable for system utilities, monitors, and debuggers. The C++ wrapper offers a lightweight, static interface that maps directly to the underlying C functions while enabling safer and more idiomatic access patterns.

HEADER REFERENCE #

#ifndef FOSSIL_SYS_PROCESS_H
#define FOSSIL_SYS_PROCESS_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#define FOSSIL_SYS_PROCESS_NAME_MAX 256
#define FOSSIL_SYS_PROCESS_ENV_MAX 1024

typedef struct {
    uint32_t pid;                      // Process ID
    uint32_t ppid;                     // Parent PID
    char name[FOSSIL_SYS_PROCESS_NAME_MAX];  // Process name
    uint64_t memory_bytes;             // Resident memory
    uint64_t virtual_memory_bytes;     // Virtual memory
    float cpu_percent;                 // CPU usage %
    uint32_t thread_count;             // Number of threads
} fossil_sys_process_info_t;

/**
 * Process list container
 */
#define FOSSIL_SYS_PROCESS_MAX 1024
typedef struct {
    size_t count;
    fossil_sys_process_info_t list[FOSSIL_SYS_PROCESS_MAX];
} fossil_sys_process_list_t;

/**
 * Get current process PID
 */
uint32_t fossil_sys_process_get_pid(void);

/**
 * Get process name by PID
 */
int fossil_sys_process_get_name(uint32_t pid, char *name, size_t name_len);

/**
 * Get process info by PID
 */
int fossil_sys_process_get_info(uint32_t pid, fossil_sys_process_info_t *info);

/**
 * Get list of all processes
 */
int fossil_sys_process_list(fossil_sys_process_list_t *plist);

/**
 * Terminate process by PID
 *
 * @param pid Process ID
 * @param force 1 = force kill, 0 = graceful
 */
int fossil_sys_process_terminate(uint32_t pid, int force);

/**
 * Retrieve environment variables of a process (best-effort)
 *
 * @param pid Process ID
 * @param buffer Preallocated buffer of length buf_len
 * @param buf_len Size of buffer
 * @return Number of bytes written, -1 on error
 */
int fossil_sys_process_get_environment(uint32_t pid, char *buffer, size_t buf_len);

#ifdef __cplusplus
}
#include <string>

/**
 * Fossil namespace.
 */
namespace fossil {

    /**
     * System namespace.
     */
    namespace sys {

        class Process {
        public:
            /**
             * @brief Retrieves the process ID (PID) of the current process.
             * 
             * @return uint32_t The PID of the current process.
             */
            static uint32_t get_pid() {
                return fossil_sys_process_get_pid();
            }

            /**
             * @brief Retrieves the name of the process with the specified PID.
             * 
             * @param pid The process ID of the target process.
             * @param name Reference to a std::string where the process name will be stored on success.
             * @return int 0 on success, or a negative error code on failure.
             */
            static int get_name(uint32_t pid, std::string &name) {
                char buf[FOSSIL_SYS_PROCESS_NAME_MAX] = {0};
                int ret = fossil_sys_process_get_name(pid, buf, sizeof(buf));
                if (ret == 0) {
                    name = buf;
                }
                return ret;
            }

            /**
             * @brief Retrieves detailed information about the process with the specified PID.
             * 
             * @param pid The process ID of the target process.
             * @param info Reference to a fossil_sys_process_info_t structure to be filled with process information.
             * @return int 0 on success, or a negative error code on failure.
             */
            static int get_info(uint32_t pid, fossil_sys_process_info_t &info) {
                return fossil_sys_process_get_info(pid, &info);
            }

            /**
             * @brief Retrieves a list of all running processes.
             * 
             * @param plist Reference to a fossil_sys_process_list_t structure to be filled with the process list.
             * @return int 0 on success, or a negative error code on failure.
             */
            static int list(fossil_sys_process_list_t &plist) {
                return fossil_sys_process_list(&plist);
            }


            /**
             * @brief Terminates the process with the specified PID.
             * 
             * @param pid The process ID of the target process.
             * @param force If true, forces termination; otherwise, attempts graceful termination.
             * @return int 0 on success, or a negative error code on failure.
             */
            static int terminate(uint32_t pid, bool force) {
                return fossil_sys_process_terminate(pid, force ? 1 : 0);
            }

            /**
             * @brief Retrieves the environment variables of the process with the specified PID.
             * 
             * @param pid The process ID of the target process.
             * @param env Reference to a std::string where the environment variables will be stored on success.
             * @return int Number of bytes written on success, or a negative error code on failure.
             */
            static int get_environment(uint32_t pid, std::string &env) {
                char buf[FOSSIL_SYS_PROCESS_ENV_MAX] = {0};
                int ret = fossil_sys_process_get_environment(pid, buf, sizeof(buf));
                if (ret >= 0) {
                    env.assign(buf, ret);
                }
                return ret;
            }

        };

    }

}

#endif

#endif /* FOSSIL_SYS_FRAMEWORK_H */

SAMPLE CODE C #

#include "fossil/sys/process.h"
#include <stdio.h>

int main(void) {
    uint32_t self = fossil_sys_process_get_pid();
    fossil_sys_process_info_t info;
    fossil_sys_process_list_t plist;

    printf("Current PID: %u\n", self);

    if (fossil_sys_process_get_info(self, &info) == 0) {
        printf("Process name: %s\n", info.name);
        printf("Memory (RSS): %llu KB\n",
               (unsigned long long)(info.memory_bytes / 1024));
        printf("Threads: %u\n", info.thread_count);
        printf("CPU usage: %.2f%%\n", info.cpu_percent);
    }

    if (fossil_sys_process_list(&plist) == 0) {
        printf("\nProcess list (%zu processes):\n", plist.count);
        for (size_t i = 0; i < plist.count; ++i) {
            printf("  [%u] %s\n",
                   plist.list[i].pid,
                   plist.list[i].name);
        }
    }

    return 0;
}

SAMPLE CODE C++ #

#include "fossil/sys/process.h"
#include <iostream>

using fossil::sys::Process;

int main() {
    uint32_t self = Process::get_pid();
    fossil_sys_process_info_t info;

    std::cout << "Current PID: " << self << "\n";

    if (Process::get_info(self, info) == 0) {
        std::cout << "Process:\n";
        std::cout << "  Name: " << info.name << "\n";
        std::cout << "  Parent PID: " << info.ppid << "\n";
        std::cout << "  Threads: " << info.thread_count << "\n";
        std::cout << "  CPU: " << info.cpu_percent << "%\n";
        std::cout << "  Memory: "
                  << (info.memory_bytes / 1024) << " KB\n";
    }

    fossil_sys_process_list_t plist;
    if (Process::list(plist) == 0) {
        std::cout << "\nRunning processes:\n";
        for (size_t i = 0; i < plist.count; ++i) {
            const auto &p = plist.list[i];
            std::cout << "  [" << p.pid << "] "
                      << p.name << "\n";
        }
    }

    std::string env;
    if (Process::get_environment(self, env) >= 0) {
        std::cout << "\nEnvironment (truncated):\n";
        std::cout << env << "\n";
    }

    return 0;
}

What are your feelings

Updated on February 7, 2026