11#include "reader.h"
22#include "cputracker.h"
3+ #include <pthread.h>
4+ #include <semaphore.h>
35#include <stdio.h>
46
5- int get_nproc (int * nproc ) {
6- * nproc = sysconf (_SC_NPROCESSORS_ONLN );
7- if (* nproc == -1 ) {
8- perror ("Reading number of threads failed" );
9- return -1 ;
10- }
11- return 0 ;
12- }
13-
147struct proc_stat * get_proc_stats () {
158 FILE * file = fopen ("/proc/stat" , "r" );
169 char line [1024 ];
@@ -20,6 +13,7 @@ struct proc_stat *get_proc_stats() {
2013 // assert(strncmp(line, "cpu", 3) == 0);
2114 if (strncmp (line , "cpu" , 3 ) != 0 ) {
2215 perror ("Reading thread info failed" );
16+ fclose (file );
2317 free (stats );
2418 return NULL ;
2519 }
@@ -34,8 +28,23 @@ struct proc_stat *get_proc_stats() {
3428 return stats ;
3529}
3630
37- int print_stats () {
38- struct proc_stat * stats = get_proc_stats ();
31+ void reader () {
32+ struct proc_stat * stats = NULL ;
33+ while (1 ) {
34+ if ((stats = get_proc_stats ()) == NULL ) {
35+ return ;
36+ }
37+
38+ sem_wait (& leftSpaceSemaphore );
39+
40+ pthread_mutex_lock (& bufferMutex );
41+
42+ put_item (stats );
43+
44+ pthread_mutex_unlock (& bufferMutex );
45+
46+ sem_wait (& filledSpaceSemaphore );
47+ }
3948
4049 for (int i = 0 ; i < nproc ; i ++ ) {
4150 printf ("%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n" , stats [i ].name ,
@@ -44,6 +53,5 @@ int print_stats() {
4453 stats [i ].guest , stats [i ].guest_nice );
4554 }
4655
47- free (stats );
48- return 0 ;
56+ // free(stats);
4957}
0 commit comments