This commit is contained in:
Eric Buxkemper 2015-11-09 16:44:58 -06:00
parent 950ec22244
commit 2f3436e5f9
3 changed files with 89 additions and 75 deletions

View file

@ -1,8 +1,6 @@
#include "semaphore.h" #include "semaphore.h"
#include "BoundedBuffer.h" #include "BoundedBuffer.h"
//#include "semaphore.h"
//#include <stdio>
#include <string> #include <string>
#include <vector> #include <vector>
@ -14,9 +12,11 @@ void BoundedBuffer::push(string item){
full->V();//increment the number of full slots now. full->V();//increment the number of full slots now.
} }
void BoundedBuffer::pop(){ string BoundedBuffer::pop(){
full.P(); string item = data.back();
data.pop(item); full->P();
empty.V(); data.pop_back();
empty->V();
return item;
} }

View file

@ -9,23 +9,32 @@
using namespace std; using namespace std;
extern unsigned int n, b, w; //extern unsigned int n, b, w;
class BoundedBuffer{ class BoundedBuffer{
int b_val;
Semaphore *full = new Semaphore(0); //initialized to 0, Since there are 0 full slots 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 *empty;// = new Semaphore(b); //initialized to b, Since all the slots are empty
Semaphore *mutex = new Semaphore(1); Semaphore *mutex = new Semaphore(1);
vector<string> data; vector<string> data;
//BoundedBuffer( //BoundedBuffer(
public:
BoundedBuffer(){empty = new Semaphore(100);}//default b value
void set_b(int val_b){ b = val_b;} BoundedBuffer(int b){
b_val = b;
empty = new Semaphore(b_val);
}
void set_b(int b){ b_val = b;}
void push(string item); void push(string item);
void pop(); string pop();
//can't be larger than b strings //can't be larger than b strings
//b = user input //b = user input

View file

@ -44,13 +44,17 @@ using namespace std;
/* CONSTANTS */ /* CONSTANTS */
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/* -- (none) -- */ //RequestChannel* control;
BoundedBuffer* Request_Buffer;
BoundedBuffer* Response_Buffers[3];
int n = 1000, b = 100, w = 5;//set defaults
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/* FORWARDS */ /* FORWARDS */
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/* -- (none) -- */ void* Req_Thread(string name);
void* Worker_Thread(char* arg[], RequestChannel* chan);
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/* MAIN FUNCTION */ /* MAIN FUNCTION */
@ -60,9 +64,6 @@ int main(int argc, char * argv[]) {
int option = -1; int option = -1;
unsigned int n;
unsigned int b;
unsigned int w;
while ((option = getopt(argc, argv, "n:b:w:")) != -1){ while ((option = getopt(argc, argv, "n:b:w:")) != -1){
switch (option){ switch (option){
case 'n' : case 'n' :
@ -70,6 +71,7 @@ int main(int argc, char * argv[]) {
break; break;
case 'b' : case 'b' :
b = atoi(optarg); b = atoi(optarg);
Request_Buffer = new BoundedBuffer(b);
break; break;
case 'w' : case 'w' :
w = atoi(optarg); w = atoi(optarg);
@ -77,70 +79,73 @@ int main(int argc, char * argv[]) {
} }
} }
cout << "TEST: " << n << b << w << endl;
BoundedBuffer buff();
Semaphore sema(5);
cout << "CLIENT STARTED:" << endl;
pid_t pid = fork(); pid_t pid = fork();
if(pid == 0){ if(pid != 0){
execve("dataserver", argv, argv);
}else{
cout << "CLIENT STARTED:" << endl;
cout << "Establishing control channel... " << flush; cout << "Establishing control channel... " << flush;
control chan("control", RequestChannel::CLIENT_SIDE); RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
cout << "done." << endl; cout << "done." << endl;
/* -- Start sending a sequence of requests */ for(int i=0; i < n*3; i++){
switch(i%3){
/* case 0:
string reply1 = chan.send_request("hello"); Req_Thread(" Joe Smith");
cout << "Reply to request 'hello' is '" << reply1 << "'" << endl; cout << "Joe\n";
break;
string reply2 = chan.send_request("data Joe Smith"); case 1:
cout << "Reply to request 'data Joe Smith' is '" << reply2 << "'" << endl; Req_Thread(" Jane Smith");
cout << "Jane\n";
string reply3 = chan.send_request("data Jane Smith"); break;
cout << "Reply to request 'data Jane Smith' is '" << reply3 << "'" << endl; case 2:
Req_Thread(" John Doe");
string reply5 = chan.send_request("newthread"); cout << "John\n";
cout << "Reply to request 'newthread' is " << reply5 << "'" << endl; break;
RequestChannel chan2(reply5, RequestChannel::CLIENT_SIDE); }
string reply6 = chan2.send_request("data John Doe");
cout << "Reply to request 'data John Doe' is '" << reply6 << "'" << endl;
string reply7 = chan2.send_request("quit");
cout << "Reply to request 'quit' is '" << reply7 << "'" << endl;
string reply4 = chan.send_request("quit");
cout << "Reply to request 'quit' is '" << reply4 << "'" << endl;
*/
}else{
execve("dataserver", argv, argv);
} }
string channel_name;
for(int i=0; i < w; i++){
channel_name = chan.send_request("newthread");
cout << "Chan: " << channel_name << endl;
RequestChannel* worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE);
Worker_Thread(argv, worker_ret);
}
chan.send_request("quit");
}
usleep(1000000); usleep(1000000);
} }
void Req_Thread(RequestChannel chan){
//handles the data moving between the Client and Bounded Buffer
for(int i=0; i < 3; i++){
chan.send_request("newthread");
}
}
void Worker_Thread(void* arg){
//handles the data moving between the Server and Bounded Buffer
channel_name = control.send_request("newthread");
RequestChannel * channel_ret = new RequestChannel(channel_name, CLIENT_SIDE);
while(true){
string req = BB.pull();
string resp = channel_ret -> send_request(req);
}
}
/* /*
void*
*/ */
void* Req_Thread(string name){
cout << "Name: " << name << endl;
Request_Buffer->push("data" + name);
}
void* Worker_Thread(char* arg[], RequestChannel* chan){
//handles the data moving between the Server and Bounded Buffer
//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){
string req = Request_Buffer->pop();
string response = chan->send_request(req);
//string local_send_request(req);
}
}
string local_send_request(){
}