-
Notifications
You must be signed in to change notification settings - Fork 0
/
27-producer-consumer-multi-semaphores.c
158 lines (132 loc) · 3.56 KB
/
27-producer-consumer-multi-semaphores.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Run Using "gcc -pthread 27_producer_consumer_multi_semaphores.c && ./a.out"
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX 10
#define OPR 25
#define PRO 3
#define CON 3
sem_t empty, full;
sem_t buf_lock;
sem_t num_lock;
typedef struct str {
sem_t* empty;
sem_t* full;
sem_t* buf_lock;
sem_t* num_lock;
int* buffer;
int* head;
int* tail;
int* num;
} str;
void* producer(sem_t* empty, sem_t* full, sem_t* buf_lock, int* buffer, int* head, int* tail, int data) {
sem_wait(empty);
sem_wait(buf_lock);
buffer[*tail] = data;
*tail = (*tail + 1) % MAX;
sem_post(buf_lock);
sem_post(full);
return NULL;
}
int consume(sem_t* empty, sem_t* full, sem_t* buf_lock, int* buffer, int* head, int* tail) {
sem_wait(full);
sem_wait(buf_lock);
int data = buffer[*head];
*head = (*head + 1) % MAX;
sem_post(buf_lock);
sem_post(empty);
return data;
}
void* producer_thread(void* vargp) {
str* s = (str*)vargp;
sem_t* empty = s->empty;
sem_t* full = s->full;
sem_t* buf_lock = s->buf_lock;
sem_t* num_lock = s->num_lock;
int* buffer = s->buffer;
int* head = s->head;
int* tail = s->tail;
int* num = s->num;
free(s);
srand(time(NULL));
for (int i = 0; i < OPR; i++) {
int data = rand() % 100 + 1;
sem_wait(num_lock);
printf("num = %d + %d = %d\n", *num, data, *num + data);
*num = *num + data;
sem_post(num_lock);
producer(empty, full, buf_lock, buffer, head, tail, data);
// usleep(100);
}
return NULL;
}
void* consumer_thread(void* vargp) {
str* s = (str*)vargp;
sem_t* empty = s->empty;
sem_t* full = s->full;
sem_t* buf_lock = s->buf_lock;
sem_t* num_lock = s->num_lock;
int* buffer = s->buffer;
int* head = s->head;
int* tail = s->tail;
int* num = s->num;
free(s);
srand(time(NULL));
for (int i = 0; i < OPR; i++) {
int data = consume(empty, full, buf_lock, buffer, head, tail);
sem_wait(num_lock);
printf("num = %d - %d = %d\n", *num, data, *num - data);
*num = *num - data;
sem_post(num_lock);
// usleep(100);
}
return NULL;
}
int main() {
int buffer[MAX];
int head = 0, tail = 0;
int num = 0;
pthread_t p[PRO];
pthread_t c[CON];
sem_init(&empty, 0, 2);
sem_init(&full, 0, 0);
sem_init(&buf_lock, 0, 1);
sem_init(&num_lock, 0, 1);
// Semaphores Implementation
printf("Semaphores Implementation (Multiple Producer-Consumer):\n");
str* s1;
for (int i = 0; i < PRO; i++) {
s1 = (str*)malloc(sizeof(str));
s1->empty = ∅
s1->full = &full;
s1->buf_lock = &buf_lock;
s1->num_lock = &num_lock;
s1->buffer = buffer;
s1->head = &head;
s1->tail = &tail;
s1->num = #
pthread_create(&p[i], NULL, producer_thread, s1);
}
str* s2;
for (int i = 0; i < CON; i++) {
s2 = (str*)malloc(sizeof(str));
s2->empty = ∅
s2->full = &full;
s2->buf_lock = &buf_lock;
s2->num_lock = &num_lock;
s2->buffer = buffer;
s2->head = &head;
s2->tail = &tail;
s2->num = #
pthread_create(&c[i], NULL, consumer_thread, s2);
}
for (int i = 0; i < PRO; i++)
pthread_join(p[i], NULL);
for (int i = 0; i < CON; i++)
pthread_join(c[i], NULL);
printf("Result: %s\n", num == 0 ? "Success" : "Fail");
return 0;
}