t
This commit is contained in:
commit
feb712cfee
3 changed files with 91 additions and 89 deletions
|
@ -1,21 +0,0 @@
|
|||
|
||||
#include "semaphore.h"
|
||||
#include "BoundedBuffer.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void BoundedBuffer::push(string item){
|
||||
|
||||
empty->P();
|
||||
data.push_back(item);
|
||||
|
||||
full->V();//increment the number of full slots now.
|
||||
}
|
||||
|
||||
string BoundedBuffer::pop(){
|
||||
string item = data.back();
|
||||
full->P();
|
||||
data.pop_back();
|
||||
empty->V();
|
||||
return item;
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
|
||||
|
||||
#ifndef _BoundedBuffer_H_
|
||||
#define _BoundedBuffer_H_
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
//#include <semaphore.h>
|
||||
|
||||
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<string> data;
|
||||
|
||||
//BoundedBuffer(
|
||||
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
|
114
simpleclient.cpp
114
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,31 +83,33 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<<<<<<< HEAD
|
||||
string channel_name;
|
||||
for(int i=0; i < w; i++){
|
||||
channel_name = chan.send_request("newthread");
|
||||
|
@ -122,30 +125,95 @@ int main(int argc, char * argv[]) {
|
|||
|
||||
chan.send_request("quit");
|
||||
|
||||
}
|
||||
usleep(1000000);
|
||||
}
|
||||
=======
|
||||
|
||||
void* Req_Thread(string name){
|
||||
Request_Buffer->push("data" + name);
|
||||
|
||||
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();
|
||||
if(pid != 0){
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}else{
|
||||
execve("dataserver", argv, argv);
|
||||
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
|
||||
}
|
||||
usleep(1000000);
|
||||
}
|
||||
|
||||
void* Worker_Thread(char* arg[], RequestChannel* chan){
|
||||
<<<<<<< HEAD
|
||||
//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){
|
||||
|
||||
=======
|
||||
while(true){
|
||||
>>>>>>> 7f3859db688a7e8629c7a88e7baad87ad6df405d
|
||||
string req = Request_Buffer->pop();
|
||||
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");
|
||||
}
|
||||
|
||||
string local_send_request(){
|
||||
|
||||
void* Req_Thread(string name){
|
||||
for(int i = 0; i < n; i++){
|
||||
cout << "data " << name << endl;
|
||||
Request_Buffer->push("data" + name);
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Reference in a new issue