LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-17-2003, 01:33 AM   #1
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Rep: Reputation: 15
Problem with huge number of pthreads


Good time of day!
The problem is the following:
System: Intel, Linux RedHat 7.2, Kernel 2.4.19, gcc-3.3.
The program works as a server.
When new request comes, it creates a pthread.
There can not be created 2 threads at the same time,
in this case we just do not create new thread.
Every called thread completes and exits, allowing next thread to be called.
After a number of such calls (200,000 on my system) operational system does not
allows to create a new thread et all, in any case returning EAGAIN error,
so the system stops to work.
In case i just create one infinite thread, wich makes corresponding tasks,
and starting it using flags, this problem is never arrise.

Could you please, explain me a possible source of this error,
Best Regards,
Oleg.
 
Old 12-17-2003, 03:56 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Maybe the threads are not being deleted correctly afterwards. I can't know if you don't have a testcase to show. (that is, smallest possible code that is needed to reproduce the problem).
 
Old 12-17-2003, 06:05 AM   #3
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Have you cleaned up the thread properly?
 
Old 12-17-2003, 06:34 AM   #4
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Original Poster
Rep: Reputation: 15
2ToniT:
Here it is:

#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
pthread_attr_t *attr;
pthread_t processor;
int flag;
void test();
main()
{
int pthread_res;
long i;
for(flag=0,i=0;i<(long)1000000; )
{
if(flag==0)
{
usleep(100000);
flag=1;
i++;
pthread_res=pthread_create(&processor, attr, (void*)&test,NULL);
if(pthread_res)
fprintf(stderr,"can not open thread:%d(%ld)\n",pthread_res,i),exit(1);
}
}
}
void test()
{
int i;
usleep(100);
fprintf(stdout,".");fflush(stdout);
flag=0;
pthread_exit(0);
return;
}


Result:
can not open thread:11(1023)

Last edited by Berng; 12-17-2003 at 06:40 AM.
 
Old 12-17-2003, 07:20 AM   #5
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
How about adding pthread_join(processor, NULL); after pthread_res = pthread_create (&processor, attr, (void *) &test, NULL);

After making the changes, the program worked fine on my machine (RH 7.1,
gcc 2.96).
 
Old 12-17-2003, 07:51 AM   #6
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Original Poster
Rep: Reputation: 15
Kumar,
Thank you, looks like it works fine, but if you put this code before pthread_create,
in other case it will stop the main program when pthread works
But why this error arrise, is 0.1 second is not enough for finishing pthread?
Thanks a lot,
Oleg.
 
Old 12-17-2003, 08:17 AM   #7
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Actually, by adding pthread_join(processor, NULL); you make sure that the main program waits for the child thread to terminate. So, it's like

1.main threads creates child thread
2.main thread waits for the child thread to terminate.(due to pthread_join)
3.when the child thread terminates, all the resources used my the child thread is freed.
4. go back to step 1.

Hope this helps.

Last edited by Kumar; 12-17-2003 at 08:18 AM.
 
Old 12-17-2003, 08:33 AM   #8
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Original Poster
Rep: Reputation: 15
Kumar, Thanks a lot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
a HUGE problem! please help... :( phead Linux - Newbie 3 09-30-2005 07:41 PM
Very huge problem...need help wardialer Suse/Novell 7 04-24-2005 03:47 PM
Huge Huge Problem With Forums!!! The_Insomniac Linux - General 1 06-07-2004 09:15 AM
Problem with atoi in pthreads jones507 Programming 1 10-16-2003 03:42 PM
Huge login problem!!! madhacker Linux - Newbie 2 07-20-2003 06:40 PM


All times are GMT -5. The time now is 02:42 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration