apéro de samedi midi en route...

This commit is contained in:
tTh 2018-12-08 13:02:24 +01:00
parent 44befe2867
commit 2576b84d57
6 changed files with 55 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
a.out a.out
*.o *.o
fake-values fake-values
essai

View File

@ -4,6 +4,10 @@
CC = gcc CC = gcc
essai: essai.c funcs.o Makefile
gcc -Wall $< funcs.o -o $@
funcs.o: funcs.c funcs.h Makefile funcs.o: funcs.c funcs.h Makefile
gcc -Wall -c $< gcc -Wall -c $<

38
essai.c Normal file
View File

@ -0,0 +1,38 @@
/*
* essai.c
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "funcs.h"
int verbosity;
/* --------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int opt, foo;
int type = 0;
double loads[3];
while ((opt = getopt(argc, argv, "vst:")) != -1) {
switch (opt) {
case 'v': verbosity++; break;
case 's': srand(getpid()); break;
case 't': type = atoi(optarg); break;
default: break;
}
}
foo = get_loadavg(loads);
if (foo) fprintf(stderr, "get loadavg -> %d\n", foo);
printf("%f %f %f %f\n", dtime(), loads[0], loads[1], loads[2]);
return 0;
}
/* --------------------------------------------------------------- */

View File

@ -1,5 +1,5 @@
/* /*
* fake-values.c * fake-values.c
*/ */
#include <stdio.h> #include <stdio.h>
@ -30,7 +30,6 @@ if (verbosity > 1) {
fprintf(stderr, "fake values - %s %s\n", __DATE__, __TIME__); fprintf(stderr, "fake values - %s %s\n", __DATE__, __TIME__);
} }
printf("%f %d\n", dtime(), random1000(type)); printf("%f %d\n", dtime(), random1000(type));
return 0; return 0;

13
funcs.c
View File

@ -48,18 +48,25 @@ return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */
double get_loadavg(int foo) int get_loadavg(double *where)
{ {
FILE *fp; FILE *fp;
double loads[3];
int foo;
if ( ! (fp=fopen("/proc/loadavg", "r")) ) { if ( ! (fp=fopen("/proc/loadavg", "r")) ) {
perror("read loadavg"); perror("read loadavg");
return -42.51; return -1;
} }
foo = fscanf(fp, "%lf %lf %lf", loads, loads+1, loads+2);
if (3 != foo) fprintf(stderr, "%s : read %d vals\n", __func__, foo);
memcpy(where, loads, 3 * sizeof(double));
fclose(fp); fclose(fp);
return -42.51; return 0;
} }
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */

View File

@ -4,4 +4,5 @@
int random1000(int mode); int random1000(int mode);
double dtime(void); double dtime(void);
int get_loadavg(double *where);