-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.cpp
More file actions
151 lines (139 loc) · 4.88 KB
/
Copy pathconsumer.cpp
File metadata and controls
151 lines (139 loc) · 4.88 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
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <bits/stdc++.h>
#include <cstring>
#include "MySemaphore.h"
#include <signal.h>
#define ANSI_COLOR_RED "\033[;31m"
#define ANSI_COLOR_BLUE "\033[34m"
#define ANSI_COLOR_GREEN "\033[;32m"
#define ANSI_COLOR_RESET "\033[0m"
using namespace std;
key_t IPC_key;
void* sharedMemory;
int sharedMemoryID;
map<string,int>indexOf;
map<int,vector<double>>prevValues;
map<int,double>prevAverageValues;
pair<int,double>* prices;
int bufferSize;
int* currentItem;
int* currentSize;
int semaphoreSetId;
string COMMODITIES[11]= {"ALUMINUM ", "COPPER ", "COTTON ", "CRUDEOIL ", "GOLD ","LEAD ", "MENTHAOIL ", "NATURALGAS", "NICKEL ", "SILVER ", "ZINC "};
void updateCommodityPrice(int commodityIndex,double currentPrice){
printf("\033[1;1H");
if(prevValues[commodityIndex].size()==0){//aka empty
printf("\033[%d;%dH",(4+commodityIndex),14);
printf(ANSI_COLOR_GREEN);
printf("%7.2lf ↑|",currentPrice);
printf(ANSI_COLOR_RESET);
prevValues[commodityIndex].push_back(currentPrice);
}else{
double previousValue = prevValues[commodityIndex][prevValues[commodityIndex].size()-1];
printf("\033[%d;%dH",(4+commodityIndex),14);
if(currentPrice-previousValue>0.001){
printf(ANSI_COLOR_GREEN);
printf("%7.2lf ↑",currentPrice);
printf(ANSI_COLOR_RESET);
}else if (previousValue-currentPrice>0.001){
printf(ANSI_COLOR_RED);
printf("%7.2lf ↓",currentPrice);
printf(ANSI_COLOR_RESET);
}else{
printf("%7.2lf -",currentPrice);
}
cout<<"|";
prevValues[commodityIndex].push_back(currentPrice);
if(prevValues[commodityIndex].size()>5)
prevValues[commodityIndex].erase(prevValues[commodityIndex].begin());
}
int sum =0,j=0;
for(j=0;j<5 && j<prevValues[commodityIndex].size();j++){
sum+=prevValues[commodityIndex][j];
}
double average = sum*1.0/j;
if(prevAverageValues[commodityIndex]<0.0001){
printf(ANSI_COLOR_GREEN);
printf("%7.2lf ↑",average);
printf(ANSI_COLOR_RESET);
}else{
double prevAverageValue = prevAverageValues[commodityIndex];
if(average-prevAverageValue>0.0001){
printf(ANSI_COLOR_GREEN);
printf("%7.2lf ↑",average);
printf(ANSI_COLOR_RESET);
}else if(prevAverageValue-average>0.0001){
printf(ANSI_COLOR_RED);
printf("%7.2lf ↓",average);
printf(ANSI_COLOR_RESET);
}else{
printf("%7.2lf -",average);
}
}
prevAverageValues[commodityIndex]=average;
}
void consume(){
IPC_key = ftok("interprocesscommunication",65);
sharedMemoryID = shmget(IPC_key,(bufferSize*16)+8,0666|IPC_CREAT);
sharedMemory= shmat(sharedMemoryID,NULL,0);
memset(sharedMemory,0,(bufferSize*16)+8);
currentSize = (int *) sharedMemory;
currentItem= (int *) sharedMemory+4;
prices = (pair<int,double>*)sharedMemory+8;
struct sembuf sem_op;
semaphoreSetId = semget(IPC_key,3,0666 | IPC_CREAT );
semctl(semaphoreSetId,0,SETVAL,1); //mutex at index 0
semctl(semaphoreSetId,1,SETVAL,bufferSize); // empty at index 1
semctl(semaphoreSetId,2,SETVAL,0); //full at index 2
while(true){
sem_op = waitSemaphore(2);
semop(semaphoreSetId,&sem_op,1);
sem_op = waitSemaphore(0);
semop(semaphoreSetId,&sem_op,1);
cout<<"|"<<endl;
pair<int,double>p = prices[*currentItem];
*currentItem = (*currentItem+1)%bufferSize;
int commodityIndex = p.first;
double commodityPrice = p.second;
updateCommodityPrice(commodityIndex,commodityPrice);
sem_op = signalSemaphore(0);
semop(semaphoreSetId,&sem_op,1);
sem_op = signalSemaphore(1);
semop(semaphoreSetId,&sem_op,1);
}
}
void signalHandler(int signum){
printf("\033[16;1H");
shmdt(sharedMemory);
shmctl(sharedMemoryID,IPC_RMID,NULL);
exit(signum);
}
int main(int argc, char** argv){
signal(SIGINT,signalHandler);
signal(SIGTSTP,signalHandler);
signal(SIGTERM,signalHandler);
signal(SIGABRT,signalHandler);
bufferSize= stoi(argv[1]);
printf("\e[1;1H\e[2J");
cout<<"+-------------------------------------+"<<endl;
cout<<"| Currency | Price | AvgPrice |"<<endl;
cout<<"+-------------------------------------+"<<endl;
for(int i=0;i<11;i++){
cout<<"| "<<COMMODITIES[i];
printf("|");
printf(ANSI_COLOR_BLUE);
printf("%7.2lf ",0.0);
printf(ANSI_COLOR_RESET);
printf("|");
printf(ANSI_COLOR_BLUE);
printf("%7.2lf ",0.0);
printf(ANSI_COLOR_RESET);
printf("|\n");
}
cout<<"+-------------------------------------+"<<endl;
consume();
return 0;
}