33#include "printer.h"
44#include "reader.h"
55
6- volatile sig_atomic_t done = 0 ;
7-
86int g_nproc = 0 ;
97static struct proc_stat * g_dataBuffer [BUFFER_SIZE ];
108pthread_mutex_t g_dataBufferMutex = PTHREAD_MUTEX_INITIALIZER ;
@@ -20,6 +18,12 @@ static pthread_t readerThreadId;
2018static pthread_t analyzerThreadId ;
2119static pthread_t printerThreadId ;
2220
21+ void handler (int signum ) {
22+ pthread_cancel (readerThreadId );
23+ pthread_cancel (printerThreadId );
24+ pthread_cancel (analyzerThreadId );
25+ }
26+
2327static int get_nproc (int * nproc ) {
2428 * nproc = (int )sysconf (_SC_NPROCESSORS_ONLN );
2529 if (* nproc == -1 ) {
@@ -72,6 +76,11 @@ int main(void) {
7276 pthread_create (& analyzerThreadId , NULL , analyzer , NULL );
7377 pthread_create (& printerThreadId , NULL , printer , NULL );
7478
79+ struct sigaction action ;
80+ memset (& action , 0 , sizeof (struct sigaction ));
81+ action .sa_handler = handler ;
82+ sigaction (SIGTERM , & action , NULL );
83+
7584 pthread_join (readerThreadId , NULL );
7685 pthread_join (analyzerThreadId , NULL );
7786 pthread_join (printerThreadId , NULL );
0 commit comments