Repository navigation
Expand file tree
/
Copy pathconsumer.cpp
More file actions
275 lines (246 loc) · 8.93 KB
/
Copy pathconsumer.cpp
File metadata and controls
275 lines (246 loc) · 8.93 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <bits/stdc++.h>
#include <regex>
#include <string.h>
#include <random>
#include <chrono>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <time.h>
#include <vector>
#include <iostream>
#include <ctime>
using namespace std;
// This code implements a producer-consumer problem with a single consumer and multiple producers.
// The producer and consumer threads are synchronized using a mutex and a condition variable.
// The mutex protects the buffer, the condition variable is used to signal the consumer when
// the buffer is not empty. The buffer is implemented as a queue, with the front of the queue
// representing the oldest element and the back of the queue representing the newest element.
// The producer threads push new elements to the back of the queue, while the consumer thread
// pops elements from the front of the queue. The consumer thread signals the producers when
// the buffer is not full, and the producers signal the consumer thread when the buffer is
// not empty.
struct msg
{
int producer_id;
double price_producer;
double price_mean_producer;
};
struct msg_to_display
{
int producer_id;
string producer_name;
double price_producer;
double price_mean_producer;
};
struct buffer
{
int start = 0;
int end = 0;
};
msg_to_display create_message_to_be_displayed(msg &message)
{
msg_to_display message_to_display;
message_to_display.producer_id = message.producer_id;
message_to_display.price_producer = message.price_producer;
message_to_display.price_mean_producer = message.price_mean_producer;
if (message.producer_id == 0)
{
message_to_display.producer_name = "ALUMINIUM";
}
else if (message.producer_id == 1)
{
message_to_display.producer_name == "COPPER";
}
else if (message.producer_id == 2)
{
message_to_display.producer_name = "COTTON";
}
else if (message.producer_id == 3)
{
message_to_display.producer_name = "CRUDEOIL";
}
else if (message.producer_id == 4)
{
message_to_display.producer_name = "GOLD";
}
else if (message.producer_id == 5)
{
message_to_display.producer_name = "LEAD";
}
else if (message.producer_id == 6)
{
message_to_display.producer_name = "MENTHAOIL";
}
else if (message.producer_id == 7)
{
message_to_display.producer_name = "NATURALGAS";
}
else if (message.producer_id == 8)
{
message_to_display.producer_name = "NICKEL";
}
else if (message.producer_id == 9)
{
message_to_display.producer_name = "SLIVER";
}
else if (message.producer_id == 10)
{
message_to_display.producer_name = "ZINC";
}
return message_to_display;
}
struct consumer
{
int buffer_size;
vector<msg_to_display> list = {
{0, "ALUMINUM", 0.00, 0.00},
{1, "COPPER", 0.00, 0.00},
{2, "COTTON", 0.00, 0.00},
{3, "CRUDEOIL", 0.00, 0.00},
{4, "GOLD", 0.00, 0.00},
{5, "LEAD", 0.00, 0.00},
{6, "MENTHAOIL", 0.00, 0.00},
{7, "NATURALGAS", 0.00, 0.00},
{8, "NICKEL", 0.00, 0.00},
{9, "SLIVER", 0.00, 0.00},
{10, "ZINC", 0.00, 0.00}};
vector<string> delta_price = {{"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}};
vector<string> delta_mean = {{"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}, {"→"}};
void consume(msg_to_display message)
{
for (size_t i = 0; i < list.size(); i++)
{
if ((*this).list[i].producer_id == message.producer_id)
{
if ((*this).list[i].price_producer < message.price_producer)
{
(*this).delta_price[i] = "↑";
(*this).list[i].price_producer = message.price_producer;
}
else if ((*this).list[i].price_producer > message.price_producer)
{
(*this).delta_price[i] = "↓";
(*this).list[i].price_producer = message.price_producer;
}
if ((*this).list[i].price_mean_producer < message.price_mean_producer)
{
(*this).delta_mean[i] = "↑";
(*this).list[i].price_mean_producer = message.price_mean_producer;
}
else if ((*this).list[i].price_mean_producer > message.price_mean_producer)
{
(*this).delta_mean[i] = "↓";
(*this).list[i].price_mean_producer = message.price_mean_producer;
}
}
}
}
void display()
{
printf("\e[1;1H\e[2J");
printf("+-------------------------------------+\n");
printf("| Currency | Price | AvgPrice |\n");
printf("+-------------------------------------+\n");
for (int i = 0; i < (*this).list.size(); i++)
{
printf("| %s", (*this).list[i].producer_name.c_str());
printf("\033[%d;%dH", 4 + i, 16);
printf("|");
if ((*this).delta_price[i] == "↑")
{
printf(" \033[;32m %7.2lf\033[0m", (*this).list[i].price_producer);
printf("\033[;32m%s\033[0m", (*this).delta_price[i].c_str());
}
if ((*this).delta_price[i] == "↓")
{
printf(" \033[;31m %7.2lf\033[0m", (*this).list[i].price_producer);
printf("\033[;31m%s\033[0m", (*this).delta_price[i].c_str());
}
if ((*this).delta_price[i] == "→")
{
printf(" %7.2lf", (*this).list[i].price_producer);
}
printf("\033[%d;%dH", 4 + i, 28);
printf("|");
printf("\033[%d;%dH", 4 + i, 29);
if ((*this).delta_mean[i] == "↑")
{
printf("\033[;32m %7.2lf\033[0m", (*this).list[i].price_mean_producer);
printf("\033[;32m%s\033[0m", (*this).delta_mean[i].c_str());
}
if ((*this).delta_mean[i] == "↓")
{
printf("\033[;31m %7.2lf\033[0m", (*this).list[i].price_mean_producer);
printf("\033[;31m%s\033[0m", (*this).delta_mean[i].c_str());
}
if ((*this).delta_mean[i] == "→")
{
printf(" %7.2lf", (*this).list[i].price_mean_producer);
}
printf(" |\n");
}
printf("+-------------------------------------+\n");
}
};
consumer create_consumer(int buffer_size)
{
consumer consumer;
consumer.buffer_size = buffer_size;
return consumer;
}
int main(int argc, char *argv[])
{
/*
* 1. We create the consumer and the shared memory.
* 2. We create the 3 semaphores we need for the producer-consumer relationship: empty, full and exclusion.
* 3. We initialize the semaphores to the right values.
* 4. We initialize the start and end of the buffer to 0.
*/
consumer consumer = create_consumer(stoi(argv[1]));
int buffer_size = stoi(argv[1]) * sizeof(msg);
key_t key = ftok(".", 650000);
int shmid = shmget(key, buffer_size, 0666 | IPC_CREAT);
struct msg *shared_buffer = (struct msg *)shmat(shmid, (void *)0, 0);
key_t key_start = ftok(".", 20000);
int shmid_start = shmget(key_start, sizeof(buffer), 0666 | IPC_CREAT);
struct buffer *area_pointer = (struct buffer *)shmat(shmid_start, (void *)0, 0);
area_pointer->start = 0;
area_pointer->end = 0;
key_t key_semaphore_1 = ftok(".", 10000);
int empty = semget(key_semaphore_1, 1, 0660 | IPC_CREAT); // semaphore to check if the buffer is fully consumed
semctl(empty, 0, SETVAL, consumer.buffer_size);
key_t key_semaphore_2 = ftok(".", 20000);
int full = semget(key_semaphore_2, 1, 0660 | IPC_CREAT); // semaphore to check if the buffer is full
semctl(full, 0, SETVAL, 0);
key_t key_semaphore_3 = ftok(".", 30000);
int exclusion = semget(key_semaphore_3, 1, 0660 | IPC_CREAT); // semaphore to check if the buffer is fully consumed
semctl(exclusion, 0, SETVAL, 1);
struct sembuf sem_wait, sem_signal;
sem_signal.sem_num = sem_wait.sem_num = sem_wait.sem_flg = sem_signal.sem_flg = 0;
sem_signal.sem_op = 1;
sem_wait.sem_op = -1;
while (true)
{
semop(full, &sem_wait, 1);
semop(exclusion, &sem_wait, 1);
msg_to_display msg = create_message_to_be_displayed(shared_buffer[area_pointer->start]);
(area_pointer->start)++;
if (area_pointer->start == consumer.buffer_size)
{
area_pointer->start = 0;
}
semop(exclusion, &sem_signal, 1);
semop(empty, &sem_signal, 1);
consumer.consume(msg);
consumer.display();
}
// detach from shared memory
shmdt(shared_buffer);
// destroy the shared memory
shmctl(shmid, IPC_RMID, NULL);
}