1+ #include "reader.h"
12#include <stdio.h>
2- #include <string.h>
33#include <stdlib.h>
4- #include "reader.h"
4+ #include <string.h>
5+
6+ int get_nproc () {
7+ FILE * fp ;
8+ char output [64 ];
9+
10+ fp = popen ("nproc" , "r" );
11+ if (fp == NULL ) {
12+ printf ("Reading number of threads failed\n" );
13+ exit (1 );
14+ }
15+
16+ int nproc = 0 ;
17+ if (fgets (output , sizeof (output ) - 1 , fp ) != NULL ) {
18+ sscanf (output , "%d" , & nproc );
19+ }
20+
21+ pclose (fp );
22+ return nproc ;
23+ }
524
625int main () {
7- FILE * file = fopen ("/proc/stat" , "r" );
8- char line [1024 ];
9- struct proc_stat * stats = NULL ;
10- int thread_count = 0 ;
11- int capacity = 0 ;
12-
13- while (fgets (line , sizeof (line ), file )) {
14- if (strncmp (line , "cpu" , 3 ) == 0 ) {
15- if (thread_count == capacity ) {
16- capacity = capacity == 0 ? 1 : capacity * 2 ;
17- struct proc_stat * temp = realloc (stats , capacity * sizeof (struct proc_stat ));
18- if (temp == NULL ) {
19- perror ("Error reallocating memory" );
20- free (stats );
21- fclose (file );
22- return 1 ;
23- }
24- stats = temp ;
25- }
26- sscanf (line , "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu" , stats [thread_count ].name , & stats [thread_count ].user , & stats [thread_count ].nice , & stats [thread_count ].system , & stats [thread_count ].idle , & stats [thread_count ].iowait , & stats [thread_count ].irq , & stats [thread_count ].softirq , & stats [thread_count ].steal , & stats [thread_count ].guest , & stats [thread_count ].guest_nice );
27- thread_count ++ ;
28- }else {
29- break ;
30- }
31- }
26+ FILE * file = fopen ("/proc/stat" , "r" );
27+ char line [1024 ];
28+ struct proc_stat stats [get_nproc () + 1 ];
29+ int thread_count = 0 ;
3230
33- for (int i = 0 ; i < thread_count ; i ++ ) {
34- printf ("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu\n" , stats [i ].name , stats [i ].user , stats [i ].nice , stats [i ].system , stats [i ].idle , stats [i ].iowait , stats [i ].irq , stats [i ].softirq , stats [i ].steal , stats [i ].guest , stats [i ].guest_nice );
31+ while (fgets (line , sizeof (line ), file )) {
32+ if (strncmp (line , "cpu" , 3 ) == 0 ) {
33+ sscanf (line , "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu" ,
34+ stats [thread_count ].name , & stats [thread_count ].user ,
35+ & stats [thread_count ].nice , & stats [thread_count ].system ,
36+ & stats [thread_count ].idle , & stats [thread_count ].iowait ,
37+ & stats [thread_count ].irq , & stats [thread_count ].softirq ,
38+ & stats [thread_count ].steal , & stats [thread_count ].guest ,
39+ & stats [thread_count ].guest_nice );
40+ thread_count ++ ;
3541 }
42+ }
43+
44+ for (int i = 0 ; i < thread_count ; i ++ ) {
45+ printf ("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n" , stats [i ].name ,
46+ stats [i ].user , stats [i ].nice , stats [i ].system , stats [i ].idle ,
47+ stats [i ].iowait , stats [i ].irq , stats [i ].softirq , stats [i ].steal ,
48+ stats [i ].guest , stats [i ].guest_nice );
49+ }
3650
37- free (stats );
38- fclose (file );
39- return 0 ;
51+ fclose (file );
52+ return 0 ;
4053}
0 commit comments