For some reason, I create my own stacks for all the threads in my application by using pthread_attr_setstack function before calling pthread_create. However, I also want to have a custom stack for my main thread. How can I achieve that?
If that is not possible, how can I at least get the stack address and size of the main thread?
You can’t. Stack for main thread is created by OS elf loader. The size of main stack is not fixed statically (only upper limit does, via ulimit -s). OS will grow the stack each time when it is needed.
You can only switch a stack by resetting %sp,%bp registers. You should do this very carefully and it will be better to reset them back before exiting.
You can estimate stack address by:
And you can read
/proc/pid/mapsfile of your application and check the address range marked[stack]Size of the main stack is not fixed. This stack is almost empty (contains argv/envp/auxp – filled by OS) when program starts; and it will grow (not shrink) at every access to yet unused stack page. This is a special case of page fault, OS will detect that page fault looks like stack access and will give more physical pages into virtual address space of application.