Retrieving load averages in your C/C++ program

Instead of parsing /proc/loadavg directly, there is a nice convenience function in <cstdlib> (or stdlib.h if you’re using C):

#include <cstdlib>
#include <iostream>

double averages[3];
std::cout << getloadavg(averages, 3) << " elements retrieved (should be 3)" << std::endl;
std::cout << "Average 1-min: " << averages[0] << std::endl;
std::cout << "Average 5-min: " << averages[1] << std::endl;
std::cout << "Average 15-min: " << averages[2] << std::endl;