Re: I can't create thread more than 256 on Redhat 9

From: Waldek Hebisch (hebisch_at_math.uni.wroc.pl)
Date: 01/07/04


Date: 7 Jan 2004 14:42:21 GMT

Kihong Lee (sisapinusa@yahoo.com) wrote:
: hebisch@math.uni.wroc.pl (Waldek Hebisch) wrote in message news:<btej3d$jaj$1@panorama.wcss.wroc.pl>...
: > It looks that each thread takes 8MB of virtual memory for the stack.
: > So IMHO you are running out of virtual memory. So either set lower
: > limit on treads stack size (I do not know how, but that is a standard
: > parameter) or get 64-bit machine.

: You mean, I might have to modify kernel header file to adjust stack
: size, and do kernel complie again, right?

No, it is a parameter in the thread library. On RedHat 9 (nptl) the following
program went up to 4091 threads (on 512 MB machine):

/* compile with: gcc -lpthread -o thread-limit thread-limit.c */
/* originally from: http://www.volano.com/linuxnotes.html */

#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];
  pthread_attr_t attribites;
  pthread_attr_init(&attribites);
  /* Set low stack limit to allow more threads */
  pthread_attr_setstacksize (&attribites, 64*1024);
  printf("Creating threads ...\n");
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), &attribites, (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);
}

It seems that earlier threads libraries (linuxthreads) may ignore
stack settings, then you would be forced to recompile `linuxthreads'
(probably the whole glibc).

--
                              Waldek Hebisch
hebisch@math.uni.wroc.pl 


Relevant Pages

  • Re: Segmentation fault
    ... it does a seg fault when run (ie ... int i, j, data; ... and maybe you do not have so much virtual memory. ... is beyond your quota, or your stack limits. ...
    (comp.lang.c)
  • Stack in thread-local virtual memory?
    ... foo a_foo; ... when it tried to access the foo via the passed pointer. ... use of virtual memory tricks which limit the running applications but make ... same with the stack? ...
    (comp.programming.threads)
  • Re: Segmentation fault
    ... it does a seg fault when run (ie ... int i, j, data; ... and maybe you do not have so much virtual memory. ... is beyond your quota, or your stack limits. ...
    (comp.lang.c)
  • Re: Segmentation fault
    ... int i, j, data; ... and maybe you do not have so much virtual memory. ... is beyond your quota, or your stack limits. ...
    (comp.lang.c)
  • Re: Stack pointer etc
    ... > that use virtual memory, where does the stack pointer start ... There is no pat answer to stack overflow on a virtual memory ... allocate a very large object and jump right over your guard ...
    (comp.lang.asm.x86)