In Linux systems programming, monitoring server runtime and system load without parsing shell command outputs requires querying kernel metrics directly using the sysinfo() POSIX system call declared in <sys/sysinfo.h>.
Querying Uptime via sysinfo() System Call
#include <stdio.h>
#include <sys/sysinfo.h>
int main(void) {
struct sysinfo info;
if (sysinfo(&info) != 0) {
perror("sysinfo call failed");
return 1;
}
long uptime_sec = info.uptime;
int days = uptime_sec / (24 * 3600);
int hours = (uptime_sec % (24 * 3600)) / 3600;
int minutes = (uptime_sec % 3600) / 60;
int seconds = uptime_sec % 60;
printf("System Uptime: %d days, %d hours, %d mins, %d secs\n",
days, hours, minutes, seconds);
printf("1-Min Load Average: %.2f\n", info.loads[0] / (float)(1 << SI_LOAD_SHIFT));
return 0;
}
Comments and corrections