From 713415f1536a98af16929c04dd605a3e973209b5 Mon Sep 17 00:00:00 2001 From: shadow8t4 Date: Mon, 9 Nov 2015 17:18:35 -0600 Subject: [PATCH 1/3] s --- BoundedBuffer.cpp | 3 +-- BoundedBuffer.h | 4 +++- dataserver.cpp | 1 + simpleclient.cpp | 6 +++--- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp index 40f1516..09d1da8 100755 --- a/BoundedBuffer.cpp +++ b/BoundedBuffer.cpp @@ -18,5 +18,4 @@ string BoundedBuffer::pop(){ data.pop_back(); empty->V(); return item; -} - \ No newline at end of file +} \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h index f058c87..46b1227 100755 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -31,6 +31,8 @@ public: } void set_b(int b){ b_val = b;} + + int get_val(){ return b_val; } void push(string item); @@ -40,4 +42,4 @@ public: //b = user input }; -#endif +#endif diff --git a/dataserver.cpp b/dataserver.cpp index df2a709..2f91bd8 100755 --- a/dataserver.cpp +++ b/dataserver.cpp @@ -158,6 +158,7 @@ void handle_process_loop(RequestChannel & _channel) { cout << "New request is " << request << endl; if (request.compare("quit") == 0) { + cout << "\nquit\n"; _channel.cwrite("bye"); usleep(10000); // give the other end a bit of time. break; // break out of the loop; diff --git a/simpleclient.cpp b/simpleclient.cpp index 6e0ebcc..80bba0d 100755 --- a/simpleclient.cpp +++ b/simpleclient.cpp @@ -109,8 +109,8 @@ int main(int argc, char * argv[]) { string channel_name; for(int i=0; i < w; i++){ - channel_name = chan.send_request("newthread"); - cout << "Chan: " << channel_name << endl; + channel_name = chan.send_request("newthrread"); + cout << "\nChan: " << channel_name << endl; RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); Worker_Thread(argv, worker_ret); } @@ -135,7 +135,7 @@ void* Worker_Thread(char* arg[], RequestChannel* chan){ //control chan("control", RequestChannel::CLIENT_SIDE); //string channel_name = control.send_request("newthread"); //RequestChannel * channel_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); - while(true){ + while(Request_Buffer->get_val() != 0){ string req = Request_Buffer->pop(); From 4545d514a2f24514239244c8699a4d4a7d014dff Mon Sep 17 00:00:00 2001 From: shadow8t4 Date: Tue, 10 Nov 2015 15:49:32 -0600 Subject: [PATCH 2/3] t --- BoundedBuffer.h | 2 -- simpleclient.cpp | 13 +++++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/BoundedBuffer.h b/BoundedBuffer.h index a34afa4..6ba44a9 100755 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -33,8 +33,6 @@ public: int get_val(){return b_val;} void set_b(int b){ b_val = b;} - - int get_val(){ return b_val; } void push(string item); diff --git a/simpleclient.cpp b/simpleclient.cpp index b71f4e8..97e558b 100755 --- a/simpleclient.cpp +++ b/simpleclient.cpp @@ -109,12 +109,17 @@ int main(int argc, char * argv[]) { string channel_name; for(int i=0; i < w; i++){ - channel_name = chan.send_request("newthrread"); + channel_name = chan.send_request("newthread"); cout << "\nChan: " << channel_name << endl; RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); - Worker_Thread(argv, worker_ret); + pid = fork(); + if(pid != 0) { + Worker_Thread(argv, worker_ret); + break; + } } - + + chan.send_request("quit"); } @@ -130,7 +135,7 @@ void* Worker_Thread(char* arg[], RequestChannel* chan){ //control chan("control", RequestChannel::CLIENT_SIDE); //string channel_name = control.send_request("newthread"); //RequestChannel * channel_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); - while(Request_Buffer->get_val() != 0){ + while(true){ string req = Request_Buffer->pop(); cout << "Request: " << req << endl; From 4a988cc17197484a727d1eafa31fc5dc8788b952 Mon Sep 17 00:00:00 2001 From: shadow8t4 Date: Tue, 10 Nov 2015 15:56:32 -0600 Subject: [PATCH 3/3] oops --- BoundedBuffer.cpp | 25 +++++++++++++++++++++++++ BoundedBuffer.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 BoundedBuffer.cpp create mode 100644 BoundedBuffer.h diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp new file mode 100644 index 0000000..1b191c1 --- /dev/null +++ b/BoundedBuffer.cpp @@ -0,0 +1,25 @@ + +#include "semaphore.h" +#include "BoundedBuffer.h" +#include +#include + +void BoundedBuffer::push(string item){ + + empty->P(); + mutex->P(); + data.push_back(item); + mutex->V(); + full->V();//increment the number of full slots now. +} + +string BoundedBuffer::pop(){ + string item = data.back(); + full->P(); + mutex->P(); + data.pop_back(); + mutex->V(); + empty->V(); + return item; +} + \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h new file mode 100644 index 0000000..7c8a06c --- /dev/null +++ b/BoundedBuffer.h @@ -0,0 +1,45 @@ + + +#ifndef _BoundedBuffer_H_ +#define _BoundedBuffer_H_ + +#include +#include +//#include + +using namespace std; + +//extern unsigned int n, b, w; + +class BoundedBuffer{ + + int b_val; + + Semaphore *full = new Semaphore(0); //initialized to 0, Since there are 0 full slots + Semaphore *empty;// = new Semaphore(b); //initialized to b, Since all the slots are empty + Semaphore *mutex = new Semaphore(1); + + vector data; + + +public: + BoundedBuffer(){empty = new Semaphore(100);}//default b value + + BoundedBuffer(int b){ + b_val = b; + empty = new Semaphore(b_val); + } + + int get_val(){return b_val;} + + void set_b(int b){ b_val = b;} + + void push(string item); + + string pop(); + + //can't be larger than b strings + //b = user input +}; + +#endif