Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I decide how much stack I can use after a call to pthread_attr_setstacksize?
    primarykey
    data
    text
    <p>I am trying to debug some code regarding stack usage. I have made the following test program (just as an example to figure out how the pthread library works):</p> <pre><code>#include &lt;string.h&gt; #include &lt;pthread.h&gt; #include &lt;stdlib.h&gt; #include &lt;assert.h&gt; #include &lt;limits.h&gt; #include &lt;stdio.h&gt; static void *threadFunc1(void *arg) { char arr[5000]; printf("Hello fromt threadFunc1 address of arr:%p\n", &amp;arr); return; } static void *threadFunc2(void *arg) { char arr[10000]; printf("Hello fromt threadFunc2 adress of arr:%p\n", &amp;arr); return; } int main(int argc, char *argv[]) { pthread_t t1,t2; pthread_attr_t thread_attr; void *res; int s; size_t tmp_size=0; s=pthread_attr_init(&amp;thread_attr); assert(s==0); s=pthread_attr_setstacksize(&amp;thread_attr , PTHREAD_STACK_MIN ); assert(s==0); s=pthread_attr_getstacksize(&amp;thread_attr , &amp;tmp_size ); assert(s==0); printf("forced stack size of pthread is:%zd\n", tmp_size); printf("sizeof char is %zd\n", sizeof(char)); s = pthread_create(&amp;t1, &amp;thread_attr, threadFunc1, NULL); assert(s==0); sleep(1); s = pthread_create(&amp;t2, &amp;thread_attr, threadFunc2, NULL); assert(s==0); sleep(1); printf("Main done()\n"); exit(0); } </code></pre> <p>When I execute it I get the following output (on my x86_64 Ubuntu):</p> <pre><code>forced stack size of pthread is:16384 sizeof char is 1 Hello fromt threadFunc1 address of arr:0x7fef350d3b50 Segmentation fault (core dumped) </code></pre> <p>Is there a way to know how much is left of the requested PTHREAD_STACK_MIN when I enter my newly created thread? If I change the size of the char array when I enter the thread function it seems like the limit is somewhen between 7000 to 8000 which is not what I expected (somewhere in the near of 16384).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload