This commit is contained in:
Eric Buxkemper 2015-11-06 11:30:46 -06:00
parent 207dd17bca
commit 8735d45f39
4 changed files with 70 additions and 15 deletions

21
BoundedBuffer.cpp Executable file
View 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(){
}

View file

@ -1,8 +1,24 @@
class BoundBuffer{ class BoundedBuffer{
Semaphore full(n); Semaphore full(0); //initialized to 0, Since there are 0 full slots
Semaphore empty(n); Semaphore empty(b); //initialized to b, Since all the slots are empty
Semaphore mutex(n); 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
} }

View file

@ -55,8 +55,8 @@ public:
Semaphore(int _val){ Semaphore(int _val){
value = _val; value = _val;
pthread_mutex_init(&m); pthread_mutex_init(&m, NULL);
pthread_cond_init(&c); pthread_cond_init(&c, NULL);
} }
~Semaphore(){} ~Semaphore(){}
@ -92,13 +92,6 @@ public:
} }
}; };
/*
class BoundBuffer{
Semaphore full(n);
Semaphore empty(n);
Semaphore mutex(n);
};
*/
#endif #endif

View file

@ -57,7 +57,24 @@ using namespace std;
int main(int argc, char * argv[]) { 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); Semaphore sema(5);
cout << "CLIENT STARTED:" << endl; cout << "CLIENT STARTED:" << endl;
@ -98,3 +115,11 @@ int main(int argc, char * argv[]) {
usleep(1000000); usleep(1000000);
} }
/*
void* Worker_Thread();
void* Req_Thread();
void*
*/