hi,
is what somebody can explain me how a thread of pools functions because I
will like to write a server code which makes it possible to communicate with
several customers
I tested but do not function i don´t know where is the error
by exempls: here the code
int chid, pid, rcvid;
t_Message *msg;
t_Message *r_msg;
t_serverconnectioninfo *buffer;
int fd;
//Signal action structure to handle incoming signals
struct sigaction sSignalActionDriver;
void SignalHandlerDriver(int iSignal)
{
switch(iSignal)
{
case SIGINT:
free(buffer);
free(msg);
free(r_msg);
close(fd);
ChannelDestroy(chid);
break;
default: //do nothing
break;
}
exit(0);
}
//Thread 2
void *Supserver2(void *arg)
{
int iRetValue;
time_t now;
time(&now);
printf("\t Supserver2 starting at %s \n",
ctime(&now));
//Create a space
msg = (t_Message *) malloc(sizeof(t_Message));
r_msg = (t_Message *) malloc(sizeof(t_Message));
buffer = (t_serverconnectioninfo *)
malloc(sizeof(t_serverconnectioninfo) );
printf("\t Hier bin ich gewesen\n");
if(msg == NULL)
{
printf("\t Cannot Create a Space msg2\n");
perror(NULL);
exit(EXIT_FAILURE);
}
if(r_msg == NULL)
{
printf("\t Cannot Create a Space r_msg2\n");
perror(NULL);
exit(EXIT_FAILURE);
}
if(buffer == NULL)
{
printf("\t Cannot Create a Space buffer2\n");
perror(NULL);
exit(EXIT_FAILURE);
}
//Initialize the SignalHandler for cathing Ctrl-c
sSignalActionDriver.sa_handler = SignalHandlerDriver;
sigaction(SIGINT, &sSignalActionDriver, NULL);
//to get a channel ID
if( (chid = ChannelCreate(0)) == -1)
{
#ifdef _DEBUG_SERVER
printf(“error by creating channel: Error[%d]
Meaning[%s]\n”,
errno, strerror(errno));
#endif
return 0;
}
//to get a process ID
pid = getpid();
//open a file in etc
fd = open("/etc/msg_passing_data1.dat", O_WRONLY |
O_CREAT, 777);
if(fd == 0)
{
printf("\t ERROR, cannot open the file client\n");
perror(NULL);
exit( EXIT_FAILURE);
}
//initialize a Value Buffer
buffer->pid = pid;
buffer->chid = chid;
//write in the file msg_passing_data.dat
if( (iRetValue = write(fd, (void*) buffer,
sizeof(t_serverconnectioninfo) ) ) == -1)
{
perror(“Fehler beim Write in client.dat”);
exit( EXIT_FAILURE);
}
//start a Server
printf("\n\t+++++++++++++++++++++++++\n");
printf("\t SUBSERVER 2 GESTARTET\n");
printf("\t+++++++++++++++++++++++++\n\n");
while(1)
{
rcvid = MsgReceive(chid, r_msg, sizeof(t_Message),
NULL);
printf("\t rcvid = %d \n", rcvid);
if(rcvid == -1)
{
printf("\t Fatal Error MsgReceive Failed\n");
exit(EXIT_FAILURE);
}
else
{
printf("\t SubServer: hat identifier the NAME is %s
\n", r_msg->name);
strncpy(msg->type, “Subserver”, MAX_NAME_LENGTH);
strncpy(msg->name, “Reply Subserver”,
MAX_NAME_LENGTH);
//sleep(5);
MsgReply(rcvid, 1 , msg, sizeof(t_Message));
}
} // end of while(1)
return EXIT_SUCCESS;
}
//main Thread
int main(int argc, char *argv[])
{
//int iRetValue;
time_t now;
thread_pool_t *tpp;
thread_pool_attr_t pool_attr;
dispatch_t *dpp;
//resmgr_attr_t resmgr_attr;
if( (dpp = dispatch_create()) == NULL )
{
fprintf( stderr, “%s: Unable to allocate dispatch
context.\n”, argv[0] );
perror(NULL);
return EXIT_FAILURE;
}
memset( &pool_attr, 0, sizeof (pool_attr));
pool_attr.handle = dpp;
/* We are only doing resmgr-type attach */
// pool_attr.context_alloc = resmgr_context_alloc;
// pool_attr.block_func = resmgr_block;
// pool_attr.handler_func = resmgr_handler;
//pool_attr.context_free = resmgr_context_free;
pool_attr.lo_water = 2;
pool_attr.hi_water = 4;
pool_attr.increment = 1;
pool_attr.maximum = 50;
if( (tpp = thread_pool_create( &pool_attr,
POOL_FLAG_EXIT_SELF )) == NULL )
{
fprintf( stderr, “%s: Unable to initialize thread
pool.\n”, argv[0] );
perror(NULL);
return EXIT_FAILURE;
}
time(&now);
printf("\t main() starting at %s \n", ctime(&now));
pthread_create( NULL, NULL, Supserver2, NULL );
sleep(10);
//Pool Startet
thread_pool_start( tpp );
return EXIT_SUCCESS;
}
thanks