/*
* Copyright (C) 2020 Sergio García-Cuevas González
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* load-thermostat
* Control the load average with an on-off control loop.
*/
#include
#include
#include
#include
void load_control_loop(double target_load)
{
double load;
while (1)
{
(void) getloadavg(&load, 1);
if (load >= target_load)
{
(void) sleep(1);
}
}
}
void print_usage(char *program_name)
{
(void) fprintf(stderr, "Usage: %s TARGET_LOAD\n", program_name);
(void) fprintf(stderr, "load-thermostat: control the load average with an on-off control loop.\n");
(void) fprintf(stderr, "Copyright (C) 2020 Sergio García-Cuevas González\n");
(void) fprintf(stderr, "License GPLv3+: GNU GPL version 3 or later .\n");
(void) fprintf(stderr, "This is free software: you are free to change and redistribute it.\n");
(void) fprintf(stderr, "There is NO WARRANTY, to the extent permitted by law.\n");
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
print_usage(argv[0]);
return EXIT_FAILURE;
}
const double target_load = atof(argv[1]);
for (int i = 0; i < (int) target_load; i++)
{
const pid_t pid = fork ();
if (pid == 0)
{
load_control_loop(target_load);
}
}
load_control_loop(target_load);
return EXIT_SUCCESS;
}