-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.c
More file actions
29 lines (23 loc) · 759 Bytes
/
consumer.c
File metadata and controls
29 lines (23 loc) · 759 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
//Isha Gupta
//3.28.2025
#include "sharedMemory.h"
void *consumeItems(void *args) {
struct sharedMemory *shm = (struct sharedMemory *) args;
while (1) {
sem_wait(shm->fullSlots); // Wait if buffer is empty
sem_wait(shm->mutex);
for (unsigned int slot = 0; slot < nItemSlots; ++slot) {
if (shm->items[slot] != 0) {
printf("Consumer: Table slot %d full. Consuming 0x%x\n", slot + 1, shm->items[slot]);
shm->items[slot] = 0;
break;
}
}
sem_post(shm->mutex);
sem_post(shm->emptySlots); // Signal producer
usleep((useconds_t)(delay * 1e6));
}
}
int main(int argc, char *argv[]) {
run(argc, argv, &consumeItems);
}