1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 14:16:00 +00:00

multicpu1sec-c: adding locking to avoid races

This commit is contained in:
Steve Schnepp 2015-03-03 22:40:37 +00:00
parent 6dec8c3357
commit 339bfec68f

View file

@ -7,6 +7,8 @@
#include <time.h>
#include <sys/file.h>
#define PROC_STAT "/proc/stat"
int fail(char* msg) {
@ -98,8 +100,9 @@ int acquire() {
fgets(buffer, 1024, f);
/* open the spoolfile */
FILE* cache_file = fopen(cache_filename, "a");
/* lock */
flock(fileno(cache_file), LOCK_EX);
while (! feof(f)) {
if (fgets(buffer, 1024, f) == 0) {
@ -128,6 +131,9 @@ int fetch() {
printf("fetch()\n");
FILE* cache_file = fopen(cache_filename, "r+");
/* lock */
flock(fileno(cache_file), LOCK_EX);
/* cat the cache_file to stdout */
char buffer[1024];
while (fgets(buffer, 1024, cache_file)) {