LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2004, 09:44 AM   #1
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question pthread_create creates a number of threads with same tid!!!!


Hi all,

I have designed a client that sends info to a server every half hour. The server is threaded and each connection is handled by a separate thread. 99% of the times it works just fine, but every now and then I get the following output at the server side:
--------------------------------------------------------------
Accepted a connection
Started thread 3205701312
Received 500 bytes in 1st iteration
Received 85 bytes in 2nd iteration
Server received 585 bytes from the client
ALL DONE OK

Accepted a connection
Started thread 3214089792
Received 500 bytes in 1st iteration
Received 139 bytes in 2nd iteration
Server received 639 bytes from the client
ALL DONE OK

Accepted a connection
Started thread 3214089792
Accepted a connection
Started thread 3214089792
Accepted a connection
Started thread 3214089792
Accepted a connection
Started thread 3214089792
.
.
.
----------------------------------------------------------------------------------

I have not been able to replicate this. Question: How come it gives the same thread id to a new incoming connection. That was my whole point in making it threaded that if one of the threads hang or something goes wrong, I should still be able to received data from the client.
Here is the code snippet:

----------------------------------------------------------------------------------
void *process_connection( void *arg) {}

for(; {
// Accept a new connection
len = sizeof (server_ssin);
if ((client = accept(server_socket, (struct sockaddr *)&server_ssin, &len)) < 0) {
perror("Error accepting connection ");
fprintf(server_outputFile,"Error accepting connection ");
fflush(server_outputFile);
break;
//return -1;
}
printf ("Accepted a connection\n");
fprintf (server_outputFile,"Accepted a connection\n");
fflush(server_outputFile);

pthread_create(&pt,NULL,process_connection,(void *) client);
printf("Started thread %u\n",pt);
fprintf(server_outputFile, "Started thread %u\n",pt);
fflush(server_outputFile);

} // end of infinite for loop

----------------------------------------------------------------------------------

Any suggestions or help whatsoever is most welcome.
thank you very much

dravya
 
Old 08-11-2004, 11:19 AM   #2
leokor
LQ Newbie
 
Registered: Aug 2004
Location: Brookline, MA, USA
Posts: 8

Rep: Reputation: 0
Re: pthread_create creates a number of threads with same tid!!!!

Quote:
Originally posted by dravya

pthread_create(&pt,NULL,process_connection,(void *) client);
printf("Started thread %u\n",pt);
fprintf(server_outputFile, "Started thread %u\n",pt);
fflush(server_outputFile);
Is pt a global variable? Can it be referenced concurrently from different threads?

Leo
 
Old 08-11-2004, 02:23 PM   #3
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Original Poster
Rep: Reputation: 15
Thanx for the reply.
No the declaration of pt is not global, it is from main. So, I don't think threads can concurrently refrence it. Here is the code snippet:

Code:
int main() {
    int ReuseAddr_flag = 1;
    
    int server_socket;
    struct sockaddr_in server_ssin;
    int dport = SERV_DPORT;
    int client, len;
    char timelog[32];
    time_t now = time (NULL);       
    pthread_t pt;    <------------------------ pt declaration
.
.
.

Any suggestions???? Any other info you need??

FYI: following are the mutex fncs.... they ok?
Code:
 

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

/* functions to grab and release the mutex  */
void grablock() {
  /* grab the lock */
  if (pthread_mutex_lock(&mtx)!=0) {
    printf("error getting mutex lock, about to shut down server...\n");
    exit(1);
  }
}

void releaselock() {
  /* release the lock */
  if (pthread_mutex_unlock(&mtx)!=0) {
    printf("error releasing mutex lock, about to shut down server...\n");
    exit(1);
  }	
}

Thanx again...
 
Old 08-11-2004, 02:38 PM   #4
leokor
LQ Newbie
 
Registered: Aug 2004
Location: Brookline, MA, USA
Posts: 8

Rep: Reputation: 0
Check the return value of the pthread_create call. In general, you should be checking it anyway, performing some error handling in case the pthread_create call fails. For example, you might already have created the maximal number of threads, etc.

Leo
 
Old 08-11-2004, 02:49 PM   #5
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Original Poster
Rep: Reputation: 15
Thanx for the suggestion.... but how does that explain that it accepts a connection and starts a thread with the same thread id every time???

if the thread number runs out... how do you reset it??? I can't control the tid can I?

Is this better:

Code:
   
for(;;) {	
	// Accept a new connection
	len = sizeof (server_ssin);
	if ((client = accept(server_socket, (struct sockaddr *)&server_ssin, &len)) < 0) {
	    perror("Error accepting connection ");
	    fprintf(server_outputFile,"Error accepting connection ");
	    fflush(server_outputFile);
	    break;
	    //return -1;
	}
	printf ("Accepted a connection\n"); 
	fprintf (server_outputFile,"Accepted a connection\n"); 
	fflush(server_outputFile);
	if (pthread_create(&pt,NULL,process_connection,(void *) client) != 0){   <------------ change
	    printf("Could not start thread \n");
	    fprintf(server_outputFile, "Could not start thread \n");
	    fflush(server_outputFile);
	} else {
	    printf("Started thread %u\n",pt);
	    fprintf(server_outputFile, "Started thread %u\n",pt);
	    fflush(server_outputFile);
	}
		
    } // end of infinite for loop
Will the above change fix the problem??? I don't think so....

cheers
dravya
 
Old 08-11-2004, 03:18 PM   #6
leokor
LQ Newbie
 
Registered: Aug 2004
Location: Brookline, MA, USA
Posts: 8

Rep: Reputation: 0
Quote:
Originally posted by dravya
Thanx for the suggestion.... but how does that explain that it accepts a connection and starts a thread with the same thread id every time???
How do you know it starts a thread? Maybe the pthread_create call failed, the value stored in the pt variable remaining unchanged since the last successful call, and you were just printing the message with that value?

Quote:
if the thread number runs out... how do you reset it??? I can't control the tid can I?
It's not about the thread number running out. You might not be able to create a new thread, for example, if you have more than PTHREAD_THREADS_MAX threads active at the moment. There might be other reasons. Read the man pages on pthread_create and related subjects.

Quote:
Will the above change fix the problem??? I don't think so....
It won't fix it, but it might help diagnose the problem. It might also help if you, in the debugging mode, provide some hooks for thread counting, etc.

Ask yourself some questions. Why do you need to create so many threads, for example, if you find out that you're creating too many. Maybe you could implement a thread pool. What do the threads do? How long would you wait on average for a busy thread to become available in the pool? That might help you decide on the strategy of when to grow the pool and when to wait, and for how long, etc. There can be all kinds of thread pool strategies, depending on the nature of your application.

Leo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to increase number of threads per process? Vaish Linux - Distributions 8 02-25-2010 01:21 AM
A problem with threads creates 300+ zombie processes zahadumy Programming 1 11-27-2005 07:15 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Threads/posts number in profile Ephracis LQ Suggestions & Feedback 3 08-13-2005 02:37 PM
pthread_create... os2 Programming 4 05-26-2005 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration