[LCP]multi-threaded server

Chandra G K Babu S scgkiran at cse.iitk.ac.in
Sun Aug 17 13:20:02 UTC 2003


hi,

you are not calling pthread_join() for the thread you create for each 
client in the while(1) loop. Unlees you call pthread_join or the main exits 
the thread will not exit (removed from process list). but if you do it in 
while loop it will block and it will become an iterative server which you 
don't want. so think of some different logic.

// pthread_join(tidi, thread_result); in your code is in comments 

pthread_join is analogous to wait() system call for the process. unless 
the parent call wait() the child process is not removed from the process 
list.


-kiran




On 16 Aug 2003, mehul radheshyam choube wrote:

> hi all,
>      i have written code for a server program which waits for 
> clients to
> connect. seperate thread for each client connection. on 
> connection
> recieves requets, work on requets and then send the result. but 
> the
> problem is that the threads arent closed on client 
> disconnecting.
> 
> /***************** start code ******************************/
> 
> 
> #define PORT 18000
> 
> #include "dougnet.h"
> #include "session.h"
> #include "common.h"
> #include "common1.h"
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <pthread.h>
> #include <string>
> #include <iostream>
> #include <fstream>
> 
> ReadConf readConf;
> ofstream fpLog;
> 
> void *session(void *connfd)
> {
>  	std::string buf1;
>  	std::string cmd;
>  	int recvCnt;
> 
>  	pthread_detach(pthread_self());
> 
>  	recvCnt = recv_line((int)connfd, buf1);
>  	if(!recvCnt) {
>  		return NULL;
>  	}/* if(!recvCnt) */
>  	writeToLogFile<std::string, std::string, std::string>("buf1 = ", 
> buf1, "\n");
> 
>  	/*IMP. !!! login part to be added. */
>  	recvCnt = recv_line((int)connfd, cmd);
>  	if(!recvCnt) {
>  		return NULL;
>  	}/* if(!recvCnt) */
>  	writeToLogFile<std::string, std::string, std::string>("cmd = ", 
> cmd, "\n");
> 
>  	struct request *rs;
> 
>  	for (rs = requests; rs->name != NULL; ++rs) {
>  		if(strcmp (cmd.c_str(), rs->name) == 0) {
>  			(*rs->func) ((int)connfd, buf1);
>  		}/* if(strcmp (cmd.c_str(), rs->name) == 0) */
>  	}/* for */
>  	writeToLogFile<std::string>("done\n");
> 
>  	closesock((int)connfd);
>  	writeToLogFile<std::string>("socket closed\n");
> 
>  	/* pthread_exit(NULL); */
>  	return(NULL);
> }/* session() */
> 
> int main(int argc, char *argv[])
> {
>  	int  s, t, res;
>  	char tpbuf[16];
>  	pthread_t tid;
>  	void      *thread_result;
> 
>  	s = openlistener(PORT);
>  	if(s == (int) NULL) {
>  		exit(0);
>  	}/* if(s == (int) NULL) */
> 
>  	res = readConf.ReadConfFile(CONF_FILE_PATH);
>  	if(!res) {
>  		printf("error while reading config file %s\n", 
> CONF_FILE_PATH);
>  		return -1;
>  	}/* if(!res) */
> 
>  	std::string logfile;
>  	logfile = readConf.LogDir() + "/" + readConf.LogFileName();
>  	printf("logfile = %s\n", logfile.c_str());
> 
>  	fpLog.open(logfile.c_str(), ios::app);
> 
>  	while(1) {
>  		if(pollsocket(s, 10000)) {
>  			t = acceptfromlistener(s, tpbuf);
> 
>  			int res = pthread_create(&tid, NULL, &session, (void *)t);
>  			if(res) {
>  				printf("failed to create thread. %s", strerror(errno));
>  			}/* if(res) */
> 
>  			printf("Connection from %s\n", tpbuf);
> 
>  		}/* if(pollsocket(s, 10000)) */
> 
>  		/*
>  		res = pthread_join(tid, &thread_result);
>  		if(res != 0) {
>  			perror("error join");
>  			exit(1);
>  		}
>  		*/
>  	}/* while(1) */
> 
>  	return 0;
> }/* main() */
> 
> 
> /***************** end code ******************************/
> ___________________________________________________
> Meet your old school or college friends from
> 1 Million + database...
> Click here to reunite www.batchmates.com/rediff.asp
> 
> 
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
> 

-- 
Good ideas need good luck!
---------------------------------------------------------------------------
A 105, Hall-IV,				Room No. 308,
IIT Kanpur,				Dept. of Computer Science 
Pin- 208016,				and Engineering,
Phone # 				IIT Kanpur,
Res (0512) 2597114, 2597314		Dept.(0512) 2597595, 2597653
Home: http://www.cse.iitk.ac.in/users/scgkiran
----------------------------------------------------------------------------





More information about the linuxCprogramming mailing list