Skip to content

Commit f97342a

Browse files
committed
code cleaning
1 parent 9015f48 commit f97342a

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

cputracker.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ sem_t g_leftSpaceSemaphore;
1111
int get_nproc(int *nproc) {
1212
*nproc = sysconf(_SC_NPROCESSORS_ONLN);
1313
if (*nproc == -1) {
14-
perror("Reading number of threads failed");
1514
return -1;
1615
}
1716
return 0;
@@ -25,16 +24,15 @@ int get_semaphore_value(sem_t *sem) {
2524

2625
struct proc_stat *get_item() {
2726
int index = get_semaphore_value(&g_filledSpaceSemaphore);
28-
// if (index > BUFFER_SIZE) {
29-
// return NULL;
30-
// }
3127
return g_buffer[index];
3228
}
3329

3430
int main() {
3531
if (get_nproc(&g_nproc) == -1) {
3632
exit(EXIT_FAILURE);
3733
}
34+
g_nproc++;
35+
3836
for (int i = 0; i < BUFFER_SIZE; i++) {
3937
g_buffer[i] = malloc(g_nproc * sizeof(struct proc_stat));
4038
if (g_buffer[i] == NULL) {
@@ -47,12 +45,18 @@ int main() {
4745
exit(EXIT_FAILURE);
4846
}
4947

50-
g_nproc++;
5148
pthread_t readerThreadId;
5249
pthread_t analyzerThreadId;
5350
pthread_create(&readerThreadId, NULL, reader, NULL);
5451
pthread_create(&analyzerThreadId, NULL, analyzer, NULL);
5552
pthread_join(readerThreadId, NULL);
5653
pthread_join(analyzerThreadId, NULL);
54+
55+
pthread_mutex_destroy(&g_bufferMutex);
56+
sem_destroy(&g_filledSpaceSemaphore);
57+
sem_destroy(&g_leftSpaceSemaphore);
58+
for (int i = 0; i < BUFFER_SIZE; i++) {
59+
free(g_buffer[i]);
60+
}
5761
return 0;
5862
}

reader.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ void *reader() {
2929
pthread_mutex_lock(&g_bufferMutex);
3030

3131
struct proc_stat *stats = get_item();
32-
if (stats == NULL) {
33-
continue;
34-
}
3532
if (get_proc_stats(stats) == -1) {
33+
pthread_mutex_unlock(&g_bufferMutex);
3634
continue;
3735
}
3836

0 commit comments

Comments
 (0)