Browse Source

apéro de samedi midi en route...

tTh 1 year ago
parent
commit
2576b84d57
6 changed files with 55 additions and 5 deletions
  1. 1
    0
      .gitignore
  2. 4
    0
      Makefile
  3. 38
    0
      essai.c
  4. 1
    2
      fake-values.c
  5. 10
    3
      funcs.c
  6. 1
    0
      funcs.h

+ 1
- 0
.gitignore View File

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

+ 4
- 0
Makefile View File

@@ -4,6 +4,10 @@
4 4
 
5 5
 CC = gcc
6 6
 
7
+essai:	essai.c funcs.o Makefile
8
+	gcc -Wall $< funcs.o -o $@
9
+
10
+
7 11
 funcs.o:	funcs.c funcs.h Makefile
8 12
 	gcc -Wall -c $<
9 13
 

+ 38
- 0
essai.c View File

@@ -0,0 +1,38 @@
1
+/*
2
+ *		essai.c
3
+ */
4
+
5
+#include  <stdio.h>
6
+#include  <unistd.h>
7
+#include  <stdlib.h>
8
+
9
+#include  "funcs.h"
10
+
11
+int verbosity;
12
+
13
+/* --------------------------------------------------------------- */
14
+
15
+int main(int argc, char *argv[])
16
+{
17
+int		opt, foo;
18
+int		type = 0;
19
+double		loads[3];
20
+
21
+while ((opt = getopt(argc, argv, "vst:")) != -1) {
22
+	switch (opt) {
23
+		case 'v':	verbosity++;		break;
24
+		case 's':	srand(getpid());	break;
25
+		case 't':	type = atoi(optarg);	break;	
26
+		default:				break;
27
+		}
28
+	}
29
+
30
+foo = get_loadavg(loads);
31
+if (foo) fprintf(stderr, "get loadavg -> %d\n", foo);
32
+
33
+printf("%f %f %f %f\n", dtime(), loads[0], loads[1], loads[2]);
34
+
35
+return 0;
36
+}
37
+
38
+/* --------------------------------------------------------------- */

+ 1
- 2
fake-values.c View File

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

+ 10
- 3
funcs.c View File

@@ -48,18 +48,25 @@ return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
48 48
 
49 49
 /* --------------------------------------------------------------- */
50 50
 
51
-double get_loadavg(int foo)
51
+int get_loadavg(double *where)
52 52
 {
53 53
 FILE		*fp;
54
+double		loads[3];
55
+int		foo;
54 56
 
55 57
 if ( ! (fp=fopen("/proc/loadavg", "r")) ) {
56 58
 	perror("read loadavg");
57
-	return -42.51;
59
+	return -1;
58 60
 	}
59 61
 
62
+foo = fscanf(fp, "%lf %lf %lf", loads, loads+1, loads+2);
63
+if (3 != foo) fprintf(stderr, "%s : read %d vals\n", __func__, foo);
64
+
65
+memcpy(where, loads, 3 * sizeof(double));
66
+
60 67
 fclose(fp);
61 68
 
62
-return -42.51;	
69
+return 0;	
63 70
 }
64 71
 
65 72
 /* --------------------------------------------------------------- */

+ 1
- 0
funcs.h View File

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

Loading…
Cancel
Save