-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
35 lines (27 loc) · 914 Bytes
/
Copy pathbuffer.c
File metadata and controls
35 lines (27 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "buffer.h"
void initBuffer(Buffer *b) {
b->p_index = 0;
b->c_index = 0;
b->count = 0;
pthread_mutex_init(&(b->mutex), NULL);
pthread_cond_init(&(b->full), NULL);
pthread_cond_init(&(b->empty), NULL);
}
void printBuffer(Buffer *b) {
pthread_mutex_lock(&(b->mutex));
printf("=======================================\n");
printf("Buffer State:\n");
printf("Producer Index: %d\n", b->p_index);
printf("Consumer Index: %d\n", b->c_index);
printf("Item Count: %d\n", b->count);
printf("---------------------------------------\n");
for (int i = 0; i < b->count; i++) {
int index = (b->c_index + i) % BUFFER_SIZE;
printf("Line %d: %s", index, b->lines[index]);
}
if (b->count == 0) {
printf("Buffer is empty.\n");
}
printf("=======================================\n");
pthread_mutex_unlock(&(b->mutex));
}