From 8735d45f3933dcf07f7e78cf9a9559917291677c Mon Sep 17 00:00:00 2001 From: Eric Buxkemper Date: Fri, 6 Nov 2015 11:30:46 -0600 Subject: [PATCH] v3 --- BoundedBuffer.cpp | 21 +++++++++++++++++++++ BoundedBuffer.h | 24 ++++++++++++++++++++---- semaphore.h | 11 ++--------- simpleclient.cpp | 29 +++++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 15 deletions(-) create mode 100755 BoundedBuffer.cpp diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp new file mode 100755 index 0000000..0447048 --- /dev/null +++ b/BoundedBuffer.cpp @@ -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(){ + +} + \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h index 225328c..9a2e73d 100755 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -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 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 } diff --git a/semaphore.h b/semaphore.h index 25df369..d32f9db 100755 --- a/semaphore.h +++ b/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 diff --git a/simpleclient.cpp b/simpleclient.cpp index 6188ac0..4e7e0db 100755 --- a/simpleclient.cpp +++ b/simpleclient.cpp @@ -56,8 +56,25 @@ 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* +*/ \ No newline at end of file