Search code examples
multithreadingprocessstack-overflow

Is thread or process caused stackoverflow error


I'm confused about the concept of thread and process. I have some basic questions.

I know that process allocates memory to threads. Do threads occupy all process memory? For example, a process has 1GB stack memory and it has two threads, so each thread has 512MB stack memory?

Another question is that I run a program and get a stack overflow fault. Is it caused by 'one' thread or by the process? If a thread causes stack overflow, will it 'use' another thread's stack memory or just give an error.

Thank you


Solution

  • Do threads occupy all process memory?

    Threads of the same process share the virtual address space of the process. Each thread has its own stack area reserved in the virtual address space of the process. On Linux, each threads' stack can grow up to 8MB, by default.

    Another question is that I run a program and get a stack overflow fault. Is it caused by 'one' thread or by the process?

    Yes. The first stack overflow terminates the entire process. Theoretically, all threads can cause their own stack overflow at the very same time, but these events will be serialized in the kernel and the first one will terminate the process.