Repository navigation
Expand file tree
/
Copy pathbounded_buffer.cpp
More file actions
112 lines (98 loc) · 2.37 KB
/
Copy pathbounded_buffer.cpp
File metadata and controls
112 lines (98 loc) · 2.37 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <pthread.h>
#include <atomic>
using namespace std;
typedef struct
{
atomic_bool locked;
} my_mutex_t;
void my_mutex_init(my_mutex_t *mutex)
{
atomic_init(&mutex->locked, false);
}
void my_mutex_lock(my_mutex_t *mutex)
{
while (atomic_exchange(&mutex->locked, true))
;
}
void my_mutex_unlock(my_mutex_t *mutex)
{
atomic_store(&mutex->locked, false);
}
int buffer_size = 1000;
my_mutex_t sema1;// for critical section
my_mutex_t sema2; // for checking if producer can produce
my_mutex_t sema3;// if there are any product to consume
atomic<int> check1 = 0; // no. of products in the buffer
atomic<int> check2 = buffer_size; // number of space left in the buffer
int size_ = 0; // no. of products in the buffer
void producer()
{
// sema1++;
while (sema2.locked)
;
if (++check1 == buffer_size)
{
my_mutex_lock(&sema2);
}
while (sema1.locked)
;
my_mutex_lock(&sema1);
cout << "Producer entered the critcal section\n";
cout << "producer is producing\n";
size_++;
check2--;
printf("number of product left = %d\n\n", size_);
my_mutex_unlock(&sema3);
my_mutex_unlock(&sema1);
// sema3--;
}
void consumer()
{
while (sema3.locked)
;
if (++check2 == buffer_size)
{
my_mutex_lock(&sema3);
}
while (sema1.locked)
;
my_mutex_lock(&sema1);
cout << "Consumer entered the critcal section\n";
cout << "Consumer is consuming\n";
// sema2--;
size_--;
check1--;
// sema3++;
printf("number of product left = %d\n\n", size_);
// sema1++;
my_mutex_unlock(&sema2);
my_mutex_unlock(&sema1);
}
int main()
{
int n1 = 5;
int n2 = 5;
pthread_t pro[n1], cons[n2];
my_mutex_init(&sema1);
my_mutex_init(&sema2);
my_mutex_init(&sema3);
my_mutex_lock(&sema3);
for (int i = 0; i < n1; i++)
{
pthread_create(&pro[i], NULL, (void *(*)(void *))producer, NULL);
}
for (int i = 0; i < n2; i++)
{
pthread_create(&cons[i], NULL, (void *(*)(void *))consumer, NULL);
}
for (int i = 0; i < n1; i++)
{
pthread_join(pro[i], NULL);
}
for (int i = 0; i < n2; i++)
{
pthread_join(cons[i], NULL);
}
return 0;
}