-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronization_01_Bounded-Buffer-or-Producer-Consumer.c
80 lines (70 loc) · 2.1 KB
/
Synchronization_01_Bounded-Buffer-or-Producer-Consumer.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h> // Include for the sleep function
#define bs 4
sem_t mutex, empty, full;
int buffer[bs];
int i = 1, c = 0;
//Producer process
void producer() {
while (1) {
printf("\nItem%d produced.", i);
int item = i++;
// Wait for an empty slot in buffer
sem_wait(&empty);
// Enter critical section
sem_wait(&mutex);
// Add item to buffer
buffer[c++] = item;
// Exit critical section
sem_post(&mutex);
// Signal that a slot is filled in buffer
sem_post(&full);
// Introduce a sleep to slow down the output
sleep(1);
}
}
//Consumer Process
void consumer() {
while (1) {
// Wait for a filled slot
sem_wait(&full);
// Enter critical section
sem_wait(&mutex);
// Remove an item from buffer
int consumedItem = buffer[c--];
// Exit critical section
sem_post(&mutex);
// Signal that an empty slot is available in buffer
sem_post(&empty);
// Consume the item
printf("\nItem%d consumed.", consumedItem+1);
// Introduce a sleep to slow down the output
sleep(2);
}
}
int main() {
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&empty, 0, bs);
sem_init(&full, 0, 0);
// Create threads for producer and consumer
pthread_t producerThread, consumerThread;
if (pthread_create(&producerThread, NULL, (void *)producer, NULL) != 0) {
perror("Error creating producer thread");
return 1;
}
if (pthread_create(&consumerThread, NULL, (void *)consumer, NULL) != 0) {
perror("Error creating consumer thread");
return 1;
}
// Wait for threads to finish (this is a simple example; in a real program, you would likely use thread management mechanisms)
pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);
// Destroy semaphores when no longer needed
sem_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}