-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocon.c
64 lines (62 loc) · 1.52 KB
/
procon.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int count = 0; // Number of items in the buffer
sem_t empty, full, mutex;
void *producer(void *arg)
{
int item;
for (int i = 0; i < 10; i++)
{
item = rand() % 100; // Generating a random item (0 to 99)
sem_wait(&empty);
sem_wait(&mutex);
// Critical Section (Produce)
buffer[count] = item;
count++;
printf("Produced: %d\n", item);
sem_post(&mutex);
sem_post(&full);
sleep(1);
}
pthread_exit(NULL);
}
void *consumer(void *arg)
{
int item;
for (int i = 0; i < 10; i++)
{
sem_wait(&full);
sem_wait(&mutex);
// Critical Section (Consume)
item = buffer[count - 1];
count--;
printf("Consumed: %d\n", item);
sem_post(&mutex);
sem_post(&empty);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t producer_thread, consumer_thread;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
// Create producer and consumer threads
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
// Wait for threads to finish
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return 0;
}