-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProducer_Consumer.cpp
131 lines (110 loc) · 3.13 KB
/
Producer_Consumer.cpp
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
#include<iostream>
#include<stdlib.h>
#include<vector>
// Classes and Objects, Access Specifiers,Encapsulation, Class Abstraction, Constructor ,
// Inheritence and Polymorphism (function overloading) concepts of OOPS are used.
// Semaphores are used to resolve producer-consumer problem
// stl (vector) is used to implement buffer queue
using namespace std;
class class1 //**Base Class created.
{
protected: int empty, semaphore, full, r; //**Access Specifiers implemented. Encapsulation implemented becuase protected variables are accessable only to derived and base class.
public: vector<int> buffer; int bufsize = 5;
class1()//**Constructor to initialize private variables.
{
empty = 5;
semaphore = 1;
full = 0;
}
virtual int produceitem() //** Polymorphism - virtual function defined in base class.
{
r = rand() % 100 + 1; //function will return random integer between 1 and 100
return r;
}
void wait(int S) //wait function will decrease semaphore value
{
while (S >= 0)
{
S--;
}
}
void signal(int S)//signal functon will increase semaphore value
{
S++;
}
void input_buffer_item(int item) //items randomly generated are added to buffer and function also checks if buffer is full.
{
if (buffer.size() < bufsize)
{
cout << "item " << item << " is produced." << endl;
buffer.push_back(item);
cout << "Buffer size:" << buffer.size() << endl;
}
else
{
cout << "Buffer is full" << endl;
}
}
void consume_buffer_item() //items randomly generated are removed from buffer and function also checks if buffer is empty.
{
if (buffer.size() == 0)
{
cout << "Buffer is empty " << endl;
}
else
{
cout << "item " << buffer.back() << " is deleted" << endl;
buffer.pop_back();
}
}
};
class produce_consume : public class1 //** Inheritence is implemented
{
public:
int produceitem() //** Polymorphism - function defined in derived class (function overloading).
{
r = rand() % 10 + 1; //function will return random integer between 1 and 10
return r;
}
void producer(int item)// items are added to buffer after making sure that buffer is available for input operation.
{
class1 obj;
//int item;
//item = obj.produceitem();
wait(empty);
wait(semaphore);
input_buffer_item(item);
signal(semaphore);
signal(full);
}
void consumer()// items are removed from buffer after making sure that buffer is available for consume operation.
{
wait(full);
wait(semaphore);
consume_buffer_item();
signal(semaphore);
signal(empty);
}
};
int main()
{
int choice = 0, item;
cout << "Enter choice" << endl;
produce_consume obj1;
class1 *obj2 = &obj1;
while (choice != 3)
{
cout << "1. Produce 2. Consume 3. Exit" << endl;
cin >> choice;
if (choice == 1)//** Data Abstraction, only essential items are displayed.
{
item = obj2->produceitem();//**Base class object calls Derived class method.
obj1.producer(item);
}
else if (choice == 2)
{
obj1.consumer();
}
}
return 0;
}