This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProCon.c
89 lines (70 loc) · 2.17 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define tableSize 2
const int numOfData = 10;
int table[tableSize];
sem_t isEmpty;
sem_t isFull;
int in = 0;
int out = 0;
pthread_mutex_t mutex;
// Consumer to consume data. Consumer will not consume data if there is no data to consume
void *consumer() {
for(int i = 0; i < numOfData; i++) {
// Wait until table is full
sem_wait(&isFull);
// Lock mutex
pthread_mutex_lock(&mutex);
// Get data
int data = table[out];
printf("Consumer: Remove data %d from table slot %d\n", data, out + 1);
// Get next slot in table
out = (out + 1) % tableSize;
// Unlock mutex
pthread_mutex_unlock(&mutex);
// Post that consumer is empty
sem_post(&isEmpty);
}
}
// Producer to produce data. Producer should not produce data if the table is filled.
void *producer() {
for(int i = 0; i < numOfData; i++) {
// Generate a random number for data
int data = rand();
// Wait until consumer is empty
sem_wait(&isEmpty);
// Lock mutex
pthread_mutex_lock(&mutex);
// Put data in empty spot in table
table[in] = data;
printf("Producer: Insert data %d in table slot %d of %d\n", table[in], in + 1, tableSize);
// Get next slot in table
in = (in + 1) % tableSize;
// Unlock mutex
pthread_mutex_unlock(&mutex);
// Post that table is full
sem_post(&isFull);
}
}
int main() {
// Initilize mutex
pthread_mutex_init(&mutex, NULL);
// isEmpty used to see if consumer is empty
sem_init(&isEmpty, 0, tableSize);
// isFull to see if table is full
sem_init(&isFull, 0, 0);
// Create producer and consumer threads
pthread_t producerThread, consumerThread;
pthread_create(&producerThread, NULL, producer, NULL);
pthread_create(&consumerThread, NULL, consumer, NULL);
// Join threads
pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);
// Cleanup
pthread_mutex_destroy(&mutex);
sem_destroy(&isEmpty);
sem_destroy(&isFull);
return 0;
}