Hi,

I am seeking your help on following pthread_create issue on linux OS.
The senario is

I have a main program which generates new child threads on user request.The new child executes a module and closes. It is working perfectly if there are less then 245 new threads after that "pthread_create" method return status code 12.
It looks like a limit on number of threads but my question here is that I can see all my 245 threads completing there execution till end before 246th thread starts.

I am copying an extract of my code in this message

iret1 = pthread_create( &thread1, NULL, runModule, (void*) &t_data);



void *runModule(void* Thread_Data)
	{
	
	
	thread_data* t_data = (thread_data*) Thread_Data;

	char * sessionid = t_data->SessionID;
	Session *sn = t_data->sn;
	Request *rq = t_data->rq;
	iOnePortalSpecified = SendHTTPRequest(sessionid,url,ip,port,sn,rq);
	if( iOnePortalSpecified == 1 )
		{
		printf("Refresh Session Success!");
		
		}


	}


int SendHTTPRequest(char *strSessionID,char *strURL,char *strIP,char *strPort,Session *sn, Request *rq)
{
	//----------------------
	// Declare and initialize variables.
	int iResult = -1;//Return code for socket APIs
	int iReturn = -1;
	int i = 0;//Loop counter
	int ConnectSocket = 0;//Socket reference number
	int recvbuflen = DEFAULT_BUFLEN;//Recieve buffer length
	char recvbuf[DEFAULT_BUFLEN] = "";//Recieve buffer

	struct sockaddr_in clientService;//ClientService structure

	char *sendbuf = MALLOC(500);//Send buffer	
	//----------------------
	// Construct the HTTP Request
	strcpy(sendbuf,"GET ");
	strcat(sendbuf,strURL);
	strcat(sendbuf," HTTP/1.0\nCookie: JSESSIONID=");
	strcat(sendbuf,strSessionID);
	strcat(sendbuf,";secure\n\n");

	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"HTTP Request : %s",sendbuf);

	//----------------------
	// Create a SOCKET for connecting to server
	ConnectSocket = socket(AF_INET, SOCK_STREAM, 0);

	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Create socket = %i",ConnectSocket );
	if (ConnectSocket == -1)
	{
		//log_error(LOG_WARN,"SendHTTPRequest",sn,rq,"socket failed");
		return -1;
	}

	//----------------------
	// The sockaddr_in structure specifies the address family,
	// IP address, and port of the server to be connected to.
	clientService.sin_family = AF_INET;

	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Connecting to %s port %s",strIP,strPort);
	clientService.sin_addr.s_addr = inet_addr(strIP);//Specify IP address
	clientService.sin_port = htons((short)atoi(strPort));//Specify Port number


	//----------------------
	// Connect to server.
	iResult = connect( ConnectSocket, (struct sockaddr*) &clientService, sizeof(clientService) );
	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Connect = %i",iResult);
	if (iResult == -1)
	{
		//log_error(LOG_WARN,"SendHTTPRequest",sn,rq,"Connect failed");
		return -1;
	}


	//----------------------
	// Send an initial buffer
	iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Send = %i",iResult);
	if (iResult == -1)
	{
		//log_error(LOG_WARN,"SendHTTPRequest",sn,rq,"Send failed");
		return -1;
	}


	//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Bytes Sent: %d", iResult);


	//----------------------
	// Recieve response message
	iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);

	if( iResult > 0 )//If message recieved
	{
		//Try to find the word "HTTP/1.1 200 OK" to indicate success
		char* ResultOk = strstr(recvbuf,HTTP_RESULT_OK);
		if( ResultOk != NULL )//If found then success
		{
			//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Refresh session success");
			iReturn = 1;
		}
		else//If not, then there is error response
		{
			//log_error(LOG_INFORM,"SendHTTPRequest",sn,rq,"Result Not Ok : %s",recvbuf);
			iReturn = -1;
		}
	}
	else if( iResult == 0 )//Here means connection closed from peer
	{
		//log_error(LOG_WARN,"SendHTTPRequest",sn,rq,"Connection closed");
		iReturn = -1;
	}
	else
	{
		//log_error(LOG_WARN,"SendHTTPRequest",sn,rq,"Recieve HTTP response failed");
		iReturn = -1;
	}


	return iReturn;
}
//------------------------------------------------------------------//

Kindly let me know if I have any problems in my code or there is anything else that I can do to prevent this problem

I don't use *nix but it sounds like a memory leak if there are only a few threads running at the same time. Could also be a limitation of the max number of open sockets at the same time.

> after that "pthread_create" method return status code 12.
Which if you look up in errno.h, or print using perror(), you would find out why.

iret1 = pthread_create( &thread1, NULL, runModule, (void*) &t_data);
if ( iret1 != 0 ) {
  printf( "Tread Create failed:%s\n", strerror(iret1) );
}

The next problem is whether t_data is in itself thread-safe?
Does it's scope last as long as the thread(s) which depend on it?
Is the data constant for as long as the thread(s) exist?
Is the data read-only for as long as the thread(s) exist?
If not, then you have race conditions.

Do you also make use of pthread_exit() and pthread_join() in your code, to tidy up threads as they exit?

Also, neither send() nor recv() guarantee to transfer the whole message in a single call. It is your responsibility to deal with message fragmentation and reassembly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.