Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does each thread have its own stack?

Tags:

When I create multiple threads from a process, then does each thread have its own stack, or is it that they share the stack of their parent process. What happens when a thread makes a system call? Do threads also maintain their own kernel stack like processes?

like image 977
manav m-n Avatar asked Aug 21 '13 06:08

manav m-n


People also ask

Is there a stack for each thread?

Each thread has its own stack that holds a frame for each method executing on that thread as you can see here in the section "Per Thread".

Why each thread has its own stack?

Like a traditional process i.e., process with one thread, a thread can be in any of several states (Running, Blocked, Ready or Terminated). Each thread has its own stack. Since thread will generally call different procedures and thus a different execution history. This is why thread needs its own stack.

Can a stack owned by multiple threads?

Yes , in multithreading each thread has its own stack. having a separate stack is what makes thread's independent of each other.

Does each thread have its own stack Java?

Each thread running in the Java virtual machine has its own thread stack. The thread stack contains information about what methods the thread has called to reach the current point of execution. I will refer to this as the "call stack". As the thread executes its code, the call stack changes.


1 Answers

Yes threads have their own stacks and their own kernel stacks (e.g. linux).

When a thread makes a system call, you trap into kernel mode (from user mode), you pass the arguments to the kernel, the arguments are checked, the kernel does w/e it needs to do (in the kernel stack), returns the final value back to the thread and you go back to user mode.

like image 199
Joohwan Avatar answered Nov 09 '22 23:11

Joohwan