Okay, first I will begin with some background.
I’m working on a project for a class that involves a client and a server. They are two separate processes that communicate with each other through what we are calling request channels.
The client collects command line arguments for the number of requests, the size of the buffer for the request channels, and the number of worker threads that send the requests (-n, -b, and -w respectively).
How ever many worker threads the user wants is how many request channels I must create between the client and server.
RequestChannel *channel;
int main(int argc, char * argv[])
{
char *n=NULL, *b=NULL, *w=NULL;
int num, buf, workers;
char optchar=0;
while((optchar=getopt(argc,argv,"n:b:w:"))!=-1)
switch(optchar)
{
case 'n':
n=optarg;
break;
case 'b':
b=optarg;
break;
case 'w':
w=optarg;
break;
case '?':
break;
default:
abort();
}
num=atoi(n);
buf=atoi(b);
workers=atoi(w);
channel=malloc(workers*sizeof(RequestChannel));
cout << "CLIENT STARTED:" << endl;
pid_t child_pid;
child_pid = fork();
if(child_pid==0)
{
execv("dataserver", argv);
}
else
{
cout << "Establishing control channel... " << flush;
RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
cout << "done." << endl;
for(int i=0;i<workers;i++)
RequestChannel channel[i](chan.send_request("newthread"), RequestChannel::CLIENT_SIDE);
}
}
I’m getting compile errors with the malloc line and I don’t know what the issue is. I just want to be able to access each RequestChannel like channel[i].
The way it is now, I receive an error saying
invalid conversion from
void*toRequestChannel*
When I replace the sizeof(RequestChannel) with sizeof(*RequestChannel), I receive an error saying
expected primary-expression before ‘)’ token.
The malloc line is correct (but please check if it returns NULL). A C++ compiler (not C) complains since in C++ you need a cast:
or, with C syntax: