LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions
User Name
Password
Linux - Distributions This forum is for Distribution specific questions.
Red Hat, Slackware, Debian, Novell, LFS, Mandriva, Ubuntu, Fedora - the list goes on and on... Note: An (*) indicates there is no official participation from that distribution here at LQ.

Notices

Reply
 
Search this Thread
Old 04-23-2004, 09:28 AM   #1
Vaish
LQ Newbie
 
Registered: Jan 2004
Posts: 6

Rep: Reputation: 0
How to increase number of threads per process?


When I run the following code on linux it creates only 306 threads and exits.What should I do to increase threads per process?


* compile with: gcc -lpthread -o thread-limit thread-limit.c */
/* originally from:*/

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_THREADS 10000
int i;

void run(void) {
char c;
if (i < 10)
printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
sleep(60 * 60);
}

int main(int argc, char *argv[]) {
int rc = 0;
pthread_t thread[MAX_THREADS];
printf("Creating threads ...\n");
for (i = 0; i < MAX_THREADS && rc == 0; i++) {
rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
if (rc == 0) {
pthread_detach(thread[i]);
if ((i + 1) % 100 == 0)
printf("%i threads so far ...\n", i + 1);
}
else
printf("Failed with return code %i creating thread %i.\n",
rc, i + 1);
}
exit(0);
}

When i run this program i get the o/p as :

[root@ENTERPRIZE ashish]# ./thread-limit
Creating threads ...
Address of c = 3002970 KB
Address of c = 2992726 KB
Address of c = 2982482 KB
Address of c = 2972238 KB
Address of c = 2961994 KB
Address of c = 2951750 KB
Address of c = 2941506 KB
Address of c = 2931262 KB
Address of c = 2921018 KB
Address of c = 2910774 KB
100 threads so far ...
200 threads so far ...
300 threads so far ...
Failed with return code 12 creating thread 306.
[root@ENTERPRIZE ashish]#

What should I do to increase this?

Thanks & Regards
Ashish Borkar
 
Old 04-24-2004, 03:06 PM   #2
IRIGHTI
Member
 
Registered: Oct 2003
Distribution: Slackware64 13.1 x86_64, Ubuntu 10.04 x86_64
Posts: 121

Rep: Reputation: 15
There is a thread on this somewhere that said RH inherently didn't allow many threads. I think its in the programming forum.

I run Slack 9.1 and get 1533

That would be the reason *if* you run RH.

Found it: http://www.linuxquestions.org/questi...aximum+threads

Last edited by IRIGHTI; 04-24-2004 at 03:09 PM.
 
Old 04-24-2004, 04:50 PM   #3
Technoslave
Member
 
Registered: Dec 2003
Location: Northern VA
Posts: 493

Rep: Reputation: 30
Address of c = 1130590 KB
100 threads so far ...
200 threads so far ...
Failed with return code 12 creating thread 256.

Hey, at least you got a few more threads. I have a 2.6.5 kernel I compiled, however, my gcc/glib is from Fedora FC2/rpm'd in. I've tried playing with some of the things I found on the net, such as /etc/security/limits.conf and ulimit -u, but none of those work. I ran in to a problem once where I ran out of processes for something, so this has been one of those things that I've always had on the back burner to figure out.
 
Old 04-28-2004, 10:36 AM   #4
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
I get 1533 as well on Slack 9.1

--Shade
 
Old 03-31-2005, 08:38 PM   #5
davebwithane
LQ Newbie
 
Registered: Mar 2005
Posts: 4

Rep: Reputation: 0
the way to change the pthread limit is to change the
ulimit stack_size

my ulimit -s was 10240 and I got something like 306 threads.
I changed ulimit -s to 1024 now I can create 3066 threads. Nice.

I just add ulimit -s 1024 to the .bash_profile.
 
Old 08-12-2009, 08:36 AM   #6
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
same problem with num of working threads

Hi

Im trying to run 600 threads, but only 381 threads are creating. I did ulimit -s 1024.. but still Im getting same problem.

pls suggest any other way to increase number of threads.
 
Old 11-23-2009, 04:10 AM   #7
Mohammadi
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
How to increase number of Threads?

Hello,

I am also stuck at number '381'...

But I am exiting every newly created thread soon after creation.
According to me, there should not be 381 threads running at the same time as all are exited very soon.

My Code:
--------

void *receive(void *dummy)
{
pthread_t thread_idd;

while(1)
{
...
//Create a new thread to echo the data to the same host
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
}

}

void *transmit(packet *tx)
{
void *dummy;

// Transmit the received datagram to the same host
txcount = sendto( sd, (*tx).data, (*tx).count, 0, &((*tx).host),(*tx).len );
...

pthread_exit(dummy);
}
}
 
Old 11-23-2009, 04:54 AM   #8
Mohammadi
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
Solution...

Hello All,

Even though we exit the thread, the threadi id is not released. Hence, if you are not waiting for the thread, use pthread_detach(tid) just after creating the thread. This will let you have as many threads as you want...

Code:
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
err = pthread_detach(thread_idd);
perror("Threaddetach ");


Cheers!





Quote:
Originally Posted by Mohammadi View Post
Hello,

I am also stuck at number '381'...

But I am exiting every newly created thread soon after creation.
According to me, there should not be 381 threads running at the same time as all are exited very soon.

My Code:
--------

void *receive(void *dummy)
{
pthread_t thread_idd;

while(1)
{
...
//Create a new thread to echo the data to the same host
err = pthread_create( &thread_idd, NULL, transmit, rx );
perror("Threadcreate ");
}

}

void *transmit(packet *tx)
{
void *dummy;

// Transmit the received datagram to the same host
txcount = sendto( sd, (*tx).data, (*tx).count, 0, &((*tx).host),(*tx).len );
...

pthread_exit(dummy);
}
}
 
Old 02-25-2010, 01:21 AM   #9
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
Hi..

The problem is not actually with number of threads.. It is with the stack size.
so reduce ur stacksize per thread using

Code:
ulimit -s 1024
but be careful ur stack is not using more than 1024 bytes.
Have a good day..
 
  


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
Threads/posts number in profile Ephracis LQ Suggestions & Feedback 3 08-13-2005 02:37 PM
increase the process limit of user ust Linux - General 3 10-20-2004 05:06 AM
pthread_create creates a number of threads with same tid!!!! dravya Programming 5 08-11-2004 03:18 PM
How to View Threads per Process Shashi Linux - Software 0 08-01-2003 01:37 AM
Threads And Process Penguinizer Programming 1 02-18-2003 09:39 PM


All times are GMT -5. The time now is 11:17 PM.

Main Menu
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