c - Find Thread Stack Size -


i testing usage of pthread_attr_getstacksize() on 64 bit sles linux.

#include <stdio.h> #include <limits.h> #include <errno.h> #include <stdlib.h> #include<string.h> #include <sys/file.h> #include <sys/mman.h> #include <sys/wait.h>  #include <pthread.h>   void* dosomething(void *arg) {     unsigned long = 0;     pthread_t id = pthread_self();      printf(">>> id = 0x%x\n", id);      for(i=0; i<(0xffffffff);i++);      return null; }  main(int argc, char* argv[]) {     pthread_attr_t      pattr;     pthread_t           thread;      int error = 0;     size_t ssize=0;      error = pthread_attr_init(&pattr);     if(error)         goto return;      error = pthread_attr_setdetachstate(&pattr, pthread_create_detached);     if (error)         goto return;      error = pthread_attr_getstacksize(&pattr , &ssize);     printf(" >>> ssize = %u\n", ssize);      error = pthread_create(&thread, &pattr, &dosomething, null);     if(error != 0)     {         printf("\ncan't create thread :[%s]", strerror(error));     }     else     {         printf("\n thread created successfully\n");     }  return:     return(0);  } 

it gives me different stack sizes 114500742, 2025756486, 4147952480 different times. why stack size varying?

also ulimit -a shows

# ulimit -a ... max locked memory       (kbytes, -l) 64 max memory size         (kbytes, -m) 1635484 ... stack size              (kbytes, -s) 8192 ... max user processes              (-u) 14942 virtual memory          (kbytes, -v) 1539280 

also when ulimit showing stack size 8 mb why pthread_attr_getstacksize() returns stack size in gbs?

the stack size big , not able create beyond 186 threads pthread_create fails error 12( enomem ).

it gives me different stack sizes 114500742, 2025756486, 4147952480 different times. why stack size varying?

i think bug in os.

from previous experiments variety of *nix systems (solaris, aix, hp-ux), debian/ubuntu, sles10 , rhel5, default stack size (which query , print) stable , doesn't fluctuate between runs.

the size variation have seen related 32bit vs 64bit builds of same application. expected, 64bit applications default thread stack size bigger 32bit applications.

also when ulimit showing stack size 8 mb why pthread_attr_getstacksize() returns stack size in gbs?

the ulimit shows stack size main thread, not thread stack size.

the main stack (used thread main() called) special, since created kernel, allocated @ higher memory addresses , theoretically can grow end of heap (value returned brk()).

in contrast, thread stack allocated application itself, , allocated in heap (though typically anonymous mmap() used). not treated specially: plain block of memory, allocated, freed application after thread has terminated.

the stack size big , not able create beyond 186 threads pthread_create fails error 12( enomem ).

  • report problem suse.

  • use pthread_attr_setstacksize() set size explicitly in application. *nix systems have default thread stack size around 256-512k, solaris - 2mb, ubuntu - 8mb. many applications 512k enough, if application puts large structures/arrays on stack, 8mb safer value.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -