diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp index 1b191c1..236e76f 100644 --- a/BoundedBuffer.cpp +++ b/BoundedBuffer.cpp @@ -5,7 +5,6 @@ #include void BoundedBuffer::push(string item){ - empty->P(); mutex->P(); data.push_back(item); @@ -14,12 +13,17 @@ void BoundedBuffer::push(string item){ } string BoundedBuffer::pop(){ - string item = data.back(); - full->P(); - mutex->P(); - data.pop_back(); - mutex->V(); - empty->V(); - return item; + if(data.size() > 0){ + full->P(); + mutex->P(); + string item = data.back(); + data.pop_back(); + mutex->V(); + empty->V(); + return item; + }else{ + return "quit"; + } + } \ No newline at end of file diff --git a/BoundedBuffer.h b/BoundedBuffer.h index 7c8a06c..855b21e 100644 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -12,8 +12,6 @@ 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 @@ -21,18 +19,14 @@ class BoundedBuffer{ vector data; - public: - BoundedBuffer(){empty = new Semaphore(100);}//default b value + BoundedBuffer(){empty = new Semaphore(200);}//default b value BoundedBuffer(int b){ - b_val = b; - empty = new Semaphore(b_val); + empty = new Semaphore(b); } - int get_val(){return b_val;} - - void set_b(int b){ b_val = b;} + void set_empty(int e){empty = new Semaphore(e);} void push(string item); diff --git a/dataserver.cpp b/dataserver.cpp old mode 100755 new mode 100644 index 2f91bd8..df2a709 --- a/dataserver.cpp +++ b/dataserver.cpp @@ -158,7 +158,6 @@ 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/makefile b/makefile old mode 100755 new mode 100644 index 106521b..3b51ca5 --- a/makefile +++ b/makefile @@ -3,13 +3,13 @@ all: dataserver simpleclient reqchannel.o: reqchannel.h reqchannel.cpp - g++ -std=c++11 -c -g reqchannel.cpp + g++ -std=c++11 -lpthread -c -g reqchannel.cpp dataserver: dataserver.cpp reqchannel.o - g++ -std=c++11 -g -o dataserver dataserver.cpp reqchannel.o -lpthread + g++ -std=c++11 -lpthread -g -o dataserver dataserver.cpp reqchannel.o simpleclient: simpleclient.cpp reqchannel.o BoundedBuffer.cpp - g++ -std=c++11 -g -o simpleclient simpleclient.cpp reqchannel.o BoundedBuffer.cpp + g++ -std=c++11 -lpthread -g -o simpleclient simpleclient.cpp reqchannel.o BoundedBuffer.cpp clean: $(RM) *.o diff --git a/reqchannel.cpp b/reqchannel.cpp old mode 100755 new mode 100644 diff --git a/reqchannel.h b/reqchannel.h old mode 100755 new mode 100644 diff --git a/semaphore.h b/semaphore.h old mode 100755 new mode 100644