v3
This commit is contained in:
parent
207dd17bca
commit
8735d45f39
4 changed files with 70 additions and 15 deletions
21
BoundedBuffer.cpp
Executable file
21
BoundedBuffer.cpp
Executable file
|
@ -0,0 +1,21 @@
|
|||
|
||||
void BoundedBuffer::push(string item){
|
||||
/* if(there is some empty slot){//data.size < b
|
||||
data.push_back(item);
|
||||
}else{
|
||||
wait();
|
||||
}
|
||||
*/ //replace with
|
||||
|
||||
|
||||
|
||||
empty.P();
|
||||
data.push_back(item);
|
||||
|
||||
full.V();//increment the number of full slots now.
|
||||
}
|
||||
|
||||
void BoundedBuffer::pop(){
|
||||
|
||||
}
|
||||
|
|
@ -1,8 +1,24 @@
|
|||
|
||||
|
||||
class BoundBuffer{
|
||||
Semaphore full(n);
|
||||
Semaphore empty(n);
|
||||
Semaphore mutex(n);
|
||||
class BoundedBuffer{
|
||||
Semaphore full(0); //initialized to 0, Since there are 0 full slots
|
||||
Semaphore empty(b); //initialized to b, Since all the slots are empty
|
||||
Semaphore mutex(0);
|
||||
|
||||
int b;
|
||||
|
||||
vector<string> data;
|
||||
|
||||
BoundedBuffer(
|
||||
|
||||
void set_b(int val_b){ b = val_b;}
|
||||
|
||||
void push(string item);
|
||||
|
||||
void pop();
|
||||
|
||||
|
||||
//can't be larger than b strings
|
||||
//b = user input
|
||||
}
|
||||
|
||||
|
|
11
semaphore.h
11
semaphore.h
|
@ -55,8 +55,8 @@ public:
|
|||
|
||||
Semaphore(int _val){
|
||||
value = _val;
|
||||
pthread_mutex_init(&m);
|
||||
pthread_cond_init(&c);
|
||||
pthread_mutex_init(&m, NULL);
|
||||
pthread_cond_init(&c, NULL);
|
||||
}
|
||||
|
||||
~Semaphore(){}
|
||||
|
@ -92,13 +92,6 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
/*
|
||||
class BoundBuffer{
|
||||
Semaphore full(n);
|
||||
Semaphore empty(n);
|
||||
Semaphore mutex(n);
|
||||
};
|
||||
*/
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
@ -57,7 +57,24 @@ using namespace std;
|
|||
|
||||
int main(int argc, char * argv[]) {
|
||||
|
||||
unsigned int n = 20, b = 100, w = 5;
|
||||
int option = -1;
|
||||
|
||||
while ((option = getopt(argc, argv, "n:b:w:")) != -1){
|
||||
switch (option){
|
||||
case 'n' :
|
||||
n = atoi(optarg);
|
||||
break;
|
||||
case 'b' :
|
||||
b = atoi(optarg);
|
||||
break;
|
||||
case 'w' :
|
||||
w = atoi(optarg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << n << b << w << endl;
|
||||
|
||||
Semaphore sema(5);
|
||||
cout << "CLIENT STARTED:" << endl;
|
||||
|
@ -98,3 +115,11 @@ int main(int argc, char * argv[]) {
|
|||
|
||||
usleep(1000000);
|
||||
}
|
||||
|
||||
/*
|
||||
void* Worker_Thread();
|
||||
|
||||
void* Req_Thread();
|
||||
|
||||
void*
|
||||
*/
|
Reference in a new issue