mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 02:51:03 +00:00
multicpu1sec-c: adding locking to avoid races
This commit is contained in:
parent
6dec8c3357
commit
339bfec68f
1 changed files with 7 additions and 1 deletions
|
@ -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)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue