Question

I just begin to learn OS. I feel puzzle about stack. As I found the stack is attached to each thread. That means the life of the stack is when the thread is created and be reclaimed when the thread is completion. Also search from the google, the argument and some local variable are stored in the thread. But these are allocated at compile time which seems conflict with the former that the stack is attached to a thread and be reclaimed after the thread is finished. Any one could give me some detail explanation?

Was it helpful?

Solution

the argument and some local variable are stored in the thread. But these are allocated at compile time

That is not correct.

When a thread is started, a stack is associated with that thread. When a thread terminates, that stack will be reclaimed.

For an example of why that cannot be allocated at compile time, imagine a program that prompts the user for a number of threads to start, and then starts that number of threads. There is no way the compiler could allocate storage for the arguments to methods running on that thread, or for local storage associated with that thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top