24
votes

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?

1
yes thread specific stack!, global values are share-able among thread (local not)Grijesh Chauhan
@GrijeshChauhan What happens when we make a system call from a thread?manav m-n
for example if you call open() system call to open a file. using int f = open() then of-course f is local to thread. but remember the file descriptors are shared between the threads. If you know the value of f then you can use that value in different thread to access same file (you don't need to open again)Grijesh Chauhan
@LidongGuo No. Thread semantics are one thing on which the two committees collaborated. (The interfaces vary somewhat, since C obviously cannot provide "objects" for threads, mutex, etc., but the semantics are identical.)James Kanze

1 Answers

19
votes

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.