[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] max number of threads



Torsdag 16 september 2010 skrev Kristian Nørgaard:
> Hvorfor kan mit program max skabe 381 tråde?
> Derefter får jeg fejlen:
> 
> ERROR: pthread_create() returned 11: Resource temporarily unavailable
> 
> 1) Hvilken limit bliver jeg ramt af her og hvor er denne limit defineret?
> 2) Er det korrekt at denne limit gælder antallet af tråde i hele
> processens levetid, og altså ikke kun antallet af samtidige tråde?
> 
> Herunder er mit test program, som fejler.

Du mangler ihvertfald en pthread_join på de døde tråde. Det er derfor det 
hjælper dig at lave dem som detach.

At den stopper ved 381 skyldes formentlig at du kører på en 32 bit maskine, 
med default indstillinger af stakstørrelsen på tråde.

Den allokerer vist default 8 MB pr. tråd til stak, fra det virtuelle 
adresserum. Du løber simpelthen tør for virtuelt adresserum: 381*8 ~ 3 GB - 
vupti, ikke mere virtuel adresserum.

Hvis du vil have mere end 381 samtidige tråde på din maskine, skal du enten 
sætte stakstørrelsen ned, eller køre på en 64 bit maskine.

Og, det er altså en god ide at joine dine tråde en gang imellem... :-)

Nedenstående sætter stakstørrelsen til 1 MB - denne gang rammer du så en limit 
på antallet af åbne fildeskriptorer - hver tråd koster sådan een, default er 
1024. Den må du selv ligge og rode med at få ændret :-)

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

#define NUM_THREADS 500

void *PrintHello(void *threadid)
{
    long tid;
    tid = (long)threadid;
    printf("Hello World! It's me, thread #%ld!\n", tid);
    pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;

    // Query stacksize
    size_t stacksize;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_getstacksize (&attr, &stacksize);
    printf("Default stack size = %zi\n", stacksize);
    
    // Set stack size in bytes to one MB.
    pthread_attr_setstacksize (&attr, 1 * 1024 * 1024);
    pthread_attr_getstacksize (&attr, &stacksize);
    printf("Default stack size now = %zi\n", stacksize);

    for(t=0; t<NUM_THREADS; t++){
       printf("In main: creating thread %ld\n", t);
       rc = pthread_create(&threads[t], &attr, PrintHello, (void *)t);
       if (rc){
          printf("ERROR: pthread_create() returned %d: %s\n", rc, 
strerror(rc));
          sleep( 1000 ); // To be able to view memory usage after error...
          exit(-1);
       }
    }
    pthread_exit(NULL);
}

Mvh

Mads


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

-- 
Mads Bondo Dydensborg.                               sslug@sslug
"What we're seeing with Web sites that are viewable only with IE is the
privatization of the Web," said Mozilla's Baker. "And that's a dangerous
setting. We're moving toward a world where all the capabilities of the
Internet are reprocessed through a single filter, with Microsoft's business
plan behind it."
               - news.com, 20020709


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2010-10-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *