This API provides a minimal, cross-platform abstraction for interacting with core operating system functionality, focusing on process management and basic system introspection. It allows applications to spawn and terminate processes, enumerate running processes, query system uptime, and yield execution to the scheduler—all through a consistent interface that hides platform-specific details. The central structure, fossil_sys_os_process_t, captures essential metadata such as process ID, name, memory usage, and status, making it useful for building monitoring tools, task runners, or lightweight system utilities. The C++ wrapper (fossil::sys::Os) further simplifies usage with static methods, enabling clean, expressive calls while retaining the efficiency and control of the underlying C implementation.
Code reference for C and C++ APIs for the respective Fossil Logic library.
HEADER REFERENCE #
#ifndef FOSSIL_SYS_OS_H
#define FOSSIL_SYS_OS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
/* ------------------------------------------------------
* Types
* ----------------------------------------------------- */
typedef struct {
const char* id; /* string ID for AI/meta usage */
uint32_t pid;
const char* name;
size_t memory_usage;
int status; /* platform-independent status */
} fossil_sys_os_process_t;
/* ------------------------------------------------------
* Functions
* ----------------------------------------------------- */
/**
* Spawn a new process with the given command string.
*
* @param command The command string to execute (e.g., "ls -la")
* @param out_process Pointer to process structure to populate with spawn info
* @return true if process spawned successfully, false otherwise
*/
bool fossil_sys_os_spawn(const char* command, fossil_sys_os_process_t* out_process);
/**
* Terminate a running process by its process ID.
*
* @param pid The process ID to terminate
* @return true if process was terminated successfully, false otherwise
*/
bool fossil_sys_os_kill(uint32_t pid);
/**
* Retrieve a list of all currently running processes on the system.
*
* @param buffer Pointer to array of process structures to populate
* @param max_count Maximum number of processes to retrieve
* @return Number of processes actually retrieved and populated in buffer
*/
size_t fossil_sys_os_list(fossil_sys_os_process_t* buffer, size_t max_count);
/**
* Get the total system uptime since last boot.
*
* @return System uptime in seconds
*/
uint64_t fossil_sys_os_uptime(void);
/**
* Yield the current thread's execution time to the scheduler.
* Allows other threads to run without blocking.
*/
void fossil_sys_os_yield(void);
#ifdef __cplusplus
}
namespace fossil::sys {
class Os {
public:
/**
* Spawn a new process with the given command string.
*
* @param command The command string to execute (e.g., "ls -la")
* @param out_process Pointer to process structure to populate with spawn info
* @return true if process spawned successfully, false otherwise
*/
static bool spawn(const char* command, fossil_sys_os_process_t* out_process) {
return fossil_sys_os_spawn(command, out_process);
}
/**
* Terminate a running process by its process ID.
*
* @param pid The process ID to terminate
* @return true if process was terminated successfully, false otherwise
*/
static bool kill(uint32_t pid) {
return fossil_sys_os_kill(pid);
}
/**
* Retrieve a list of all currently running processes on the system.
*
* @param buffer Pointer to array of process structures to populate
* @param max_count Maximum number of processes to retrieve
* @return Number of processes actually retrieved and populated in buffer
*/
static size_t list(fossil_sys_os_process_t* buffer, size_t max_count) {
return fossil_sys_os_list(buffer, max_count);
}
/**
* Get the total system uptime since last boot.
*
* @return System uptime in seconds
*/
static uint64_t uptime() {
return fossil_sys_os_uptime();
}
/**
* Yield the current thread's execution time to the scheduler.
* Allows other threads to run without blocking.
*/
static void yield() {
fossil_sys_os_yield();
}
};
} // namespace fossil::sys
#endif
#endif /* FOSSIL_SYS_OS_H */SAMPLE CODE C #
#include "fossil/sys/os.h"
#include <stdio.h>
int main(void)
{
fossil_sys_os_process_t proc;
/* Spawn a process */
if (!fossil_sys_os_spawn("echo Hello from Fossil OS API", &proc)) {
printf("Failed to spawn process\n");
return 1;
}
printf("Spawned process PID: %u\n", proc.pid);
/* List processes */
fossil_sys_os_process_t list[10];
size_t count = fossil_sys_os_list(list, 10);
printf("Running processes:\n");
for (size_t i = 0; i < count; i++) {
printf("PID: %u | Name: %s | Memory: %zu bytes\n",
list[i].pid,
list[i].name ? list[i].name : "unknown",
list[i].memory_usage);
}
/* Get uptime */
uint64_t uptime = fossil_sys_os_uptime();
printf("System uptime: %llu seconds\n", (unsigned long long)uptime);
/* Yield CPU */
fossil_sys_os_yield();
return 0;
}SAMPLE CODE C++ #
#include "fossil/sys/os.h"
#include <iostream>
using namespace fossil::sys;
int main()
{
fossil_sys_os_process_t proc{};
/* Spawn process */
if (!Os::spawn("echo Hello from C++ OS API", &proc)) {
std::cerr << "Failed to spawn process\n";
return 1;
}
std::cout << "Spawned PID: " << proc.pid << "\n";
/* List processes */
fossil_sys_os_process_t processes[10];
size_t count = Os::list(processes, 10);
std::cout << "Process list:\n";
for (size_t i = 0; i < count; ++i) {
std::cout << "PID: " << processes[i].pid
<< " | Name: " << (processes[i].name ? processes[i].name : "unknown")
<< " | Memory: " << processes[i].memory_usage
<< " bytes\n";
}
/* Uptime */
std::cout << "Uptime: " << Os::uptime() << " seconds\n";
/* Yield execution */
Os::yield();
return 0;
}