Projet

Général

Profil

Révision 339bfec6

ID339bfec68fc4c86da37c77710c16b0eee6426360
Parent 6dec8c33
Enfant 4a9b0e47

Ajouté par Steve Schnepp il y a plus de 10 ans

multicpu1sec-c: adding locking to avoid races

Voir les différences:

plugins/system/multicpu1sec/multicpu1sec-c.c
7 7

  
8 8
#include <time.h>
9 9

  
10
#include <sys/file.h>
11

  
10 12
#define PROC_STAT "/proc/stat"
11 13

  
12 14
int fail(char* msg) {
......
98 100
		fgets(buffer, 1024, f);
99 101

  
100 102
		/* open the spoolfile */
101

  
102 103
		FILE* cache_file = fopen(cache_filename, "a");
104
		/* lock */
105
		flock(fileno(cache_file), LOCK_EX);
103 106

  
104 107
		while (! feof(f)) {
105 108
			if (fgets(buffer, 1024, f) == 0) {
......
128 131
	printf("fetch()\n");
129 132
	FILE* cache_file = fopen(cache_filename, "r+");
130 133

  
134
	/* lock */
135
	flock(fileno(cache_file), LOCK_EX);
136

  
131 137
	/* cat the cache_file to stdout */
132 138
	char buffer[1024];
133 139
	while (fgets(buffer, 1024, cache_file)) {

Formats disponibles : Unified diff