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):
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
static void *threadFunc1(void *arg)
{
char arr[5000];
printf("Hello fromt threadFunc1 address of arr:%p\n", &arr);
return;
}
static void *threadFunc2(void *arg)
{
char arr[10000];
printf("Hello fromt threadFunc2 adress of arr:%p\n", &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(&thread_attr);
assert(s==0);
s=pthread_attr_setstacksize(&thread_attr , PTHREAD_STACK_MIN );
assert(s==0);
s=pthread_attr_getstacksize(&thread_attr , &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(&t1, &thread_attr, threadFunc1, NULL);
assert(s==0);
sleep(1);
s = pthread_create(&t2, &thread_attr, threadFunc2, NULL);
assert(s==0);
sleep(1);
printf("Main done()\n");
exit(0);
}
When I execute it I get the following output (on my x86_64 Ubuntu):
forced stack size of pthread is:16384
sizeof char is 1
Hello fromt threadFunc1 address of arr:0x7fef350d3b50
Segmentation fault (core dumped)
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).