From 7f3859db688a7e8629c7a88e7baad87ad6df405d Mon Sep 17 00:00:00 2001 From: Eric Buxkemper Date: Tue, 10 Nov 2015 15:47:13 -0600 Subject: [PATCH] Due Date --- BoundedBuffer.cpp | 9 ++-- BoundedBuffer.h | 2 +- simpleclient.cpp | 111 +++++++++++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 45 deletions(-) diff --git a/BoundedBuffer.cpp b/BoundedBuffer.cpp index 40f1516..1b191c1 100755 --- a/BoundedBuffer.cpp +++ b/BoundedBuffer.cpp @@ -5,17 +5,20 @@ #include void BoundedBuffer::push(string item){ - - empty->P(); - data.push_back(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; } diff --git a/BoundedBuffer.h b/BoundedBuffer.h index 8e18603..7c8a06c 100755 --- a/BoundedBuffer.h +++ b/BoundedBuffer.h @@ -21,7 +21,7 @@ class BoundedBuffer{ vector data; - //BoundedBuffer( + public: BoundedBuffer(){empty = new Semaphore(100);}//default b value diff --git a/simpleclient.cpp b/simpleclient.cpp index dac456e..0e9a59a 100755 --- a/simpleclient.cpp +++ b/simpleclient.cpp @@ -56,6 +56,7 @@ int n = 1000, b = 100, w = 5;//set defaults void* Req_Thread(string name); void* Worker_Thread(char* arg[], RequestChannel* chan); +void* local_send_request(string name, string response); /*--------------------------------------------------------------------------*/ /* MAIN FUNCTION */ @@ -82,64 +83,75 @@ int main(int argc, char * argv[]) { pid_t pid = fork(); - if(pid != 0){ - execve("dataserver", argv, argv); - }else{ + if(pid == 0){ + cout << "CLIENT STARTED:" << endl; cout << "Establishing control channel... " << flush; RequestChannel chan("control", RequestChannel::CLIENT_SIDE); cout << "done." << endl; - for(int i=0; i < n*3; i++){ - switch(i%3){ - case 0: + for(int i = 0; i < 3; i++){ + pid = fork(); + if(pid){ //if in the parent still + if(i == 0){ Req_Thread(" Joe Smith"); - cout << "Joe\n"; - break; - case 1: + }else if(i == 1){ Req_Thread(" Jane Smith"); - cout << "Jane\n"; - break; - case 2: + }else if(i == 2){ Req_Thread(" John Doe"); - cout << "John\n"; - break; + } + continue; + }else if (pid == 0){ + break; + } else { + exit(1); } } + + - // 0 is the parent. + + RequestChannel* worker_ret; + string channel_name; + + if(pid){ + RequestChannel chan("control", RequestChannel::CLIENT_SIDE); + cout << "done." << endl; + for(int i=0; i < w; i++){ + pid = fork(); + if(pid) { + pid = fork(); + channel_name = chan.send_request("newthread"); + cout << "Chan: " << channel_name << endl; + worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); + Worker_Thread(argv, worker_ret); + continue; + }else if(pid == 0){ + break; + }else{ + exit(1); + } + } + wait(); + //chan.send_request("quit"); + } else { + //this is where the child threads from the + //req_thread threads end up. + } for(int i=0; i < 3; i++){ //stat thread - pid = fork(); + //pid = fork(); if(pid != 0){ break; } - - } + } - RequestChannel* worker_ret; - string channel_name; - if(pid == 0){ - for(int i=0; i < w; i++){ - - channel_name = chan.send_request("newthread"); - cout << "Chan: " << channel_name << endl; - worker_ret = new RequestChannel(channel_name, RequestChannel::CLIENT_SIDE); - - pid = fork(); - if(pid != 0) { - Worker_Thread(argv, worker_ret); - break; - } - } - chan.send_request("quit"); - } else { - - } + }else{ + execve("dataserver", argv, argv); } usleep(1000000); } @@ -150,14 +162,31 @@ void* Worker_Thread(char* arg[], RequestChannel* chan){ cout << "Request: " << req << endl; string response = chan->send_request(req); cout << "Response: " << response << endl; + //local_send_request(req, response); } - chan->send_request("quit"); + //chan->send_request("quit"); } void* Req_Thread(string name){ - Request_Buffer->push("data" + name); + for(int i = 0; i < n; i++){ + cout << "data " << name << endl; + Request_Buffer->push("data" + name); + } } -void* local_send_request(){ - +void* local_send_request(string name, string response){ + //send to proper response buffer based on which name is found! + if(name.compare("data Joe Smith") == 0){ + Response_Buffers[0]->push(response); + }else if(name.compare("data Jane Smith") == 0){ + Response_Buffers[1]->push(response); + }else if(name.compare("data John Doe") == 0){ + Response_Buffers[2]->push(response); + }else{ + cout << "ERROR: Invalid push into Response Buffers\n"; + } } + + + +