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?
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.
Related
I know each thread has its own stack.
And my textbook says:
Suppose that a particular semaphore implementation uses a LIFO stack of threads for each semaphore. When a thread blocks on a semaphore in a P operation, its ID is pushed onto the stack. Similarly, the V operation pops the top thread ID from the stack and restarts that thread
what I don't understand is. since each thread uses its own stack to store its thread ID, and obviously threads don't share stack with others, so what does LIFO stack of threads mean? because a thread only put its thread onto the stack which won't contain another thread's id, why use LIFO term here, isn't that LIFO only be meaningful if we can push multiple items on stack?
Your OS needs to know which all threads are waiting for each semaphore. To maintain that list, OS can use a LIFO data structure i.e. stack for each semaphore object.
Do not confuse it with stack of each thread.
what I don't understand is. since each thread uses its own stack to store its thread ID, and obviously threads don't share stack with others, so what does LIFO stack of threads mean?
Threads can shared their stacks with others if they want. Each thread having its own stack is no impediment to one thread accessing another thread's stack.
because a thread only put its thread onto the stack which won't contain another thread's id, why use LIFO term here, isn't that LIFO only be meaningful if we can push multiple items on stack?
The fact that each thread has its own stack doesn't prevent others from existing for other purposes. That each thread has its own stack has no effect on what a sempahore can do or not do with a stack.
Think about apples. You can have them. You can share them. You can eat them.
Now, say there are ten children and each has their own apple. Does that mean there cannot exist an eleventh apple that's shared? Does this prevent them from doing other things with other apples? No, it does not.
So, yes, each thread has its own stack. And they can also share those stacks if they want.
But also, a semaphore can have a stack. And it can do anything it wants with that stack. This has nothing whatsoever to do with any other stacks that threads might have.
Summary of my understanding:
The top memory addresses are used for the? (I initially thought there was only one call stack) stack, and the? stack grows downwards (What and where are the stack and heap?)
However, each thread gets it's own stack allocated, so there should be multiple call stacks in memory (https://stackoverflow.com/a/80113/2415178)
Applications can share threads (e.g, the key application is using the main thread), but several threads can be running at the same time.
There is a CPU register called sp that tracks the stack pointer, the current stack frame of a call stack.
So here's my confusion:
Do all of the call stacks necessary for an application (if this is even possible to know) get allocated when the application gets launched? Or do call stacks get allocated/de-allocated dynamically as applications spin off new threads? And if that is the case, (I know stacks have a fixed size), do the new stacks just get allocated right below the previous stacks-- So you would end up with a stack of stacks in the top addresses of memory? Or am I just fundamentally misunderstanding how call stacks are being created/used?
I am an OS X application developer, so my visual reference for how call stacks are created come from Xcode's stack debugger:
Now I realize that how things are here are more than likely unique to OS X, but I was hoping that conventions would be similar across operating systems.
It appears that each application can execute code on multiple threads, and even spin off new worker threads that belong to the application-- and every thread needs a call stack to keep track of the stack frames.
Which leads me to my last question:
How does the sp register work if there are multiple call stacks? Is it only used for the main call stack? (Presumably the top-most call stack in memory, and associated with the main thread of the OS) [https://stackoverflow.com/a/1213360/2415178]
Do all of the call stacks necessary for an application (if this is even possible to know) get allocated when the application gets launched?
No. Typically, each thread's stack is allocated when that thread is created.
Or do call stacks get allocated/de-allocated dynamically as applications spin off new threads?
Yes.
And if that is the case, (I know stacks have a fixed size), do the new stacks just get allocated right below the previous stacks-- So you would end up with a stack of stacks in the top addresses of memory? Or am I just fundamentally misunderstanding how call stacks are being created/used?
It varies. But the stack just has to be at the top of a large enough chunk of available address space in the memory map for that particular process. It doesn't have to be at the very top. If you need 1MB for the stack, and you have 1MB, you can just reserve that 1MB and have the stack start at the top of it.
How does the sp register work if there are multiple call stacks? Is it only used for the main call stack?
A CPU has as many register sets as threads that can run at a time. When the running thread is switched, the leaving thread's stack pointer is saved and the new thread's stack pointer is restored -- just like all other registers.
There is no "main thread of the OS". There are some kernel threads that do only kernel tasks, but also user-space threads also run in kernel space to run the OS code. Pure kernel threads have their own stacks somewhere in kernel memory. But just like normal threads, it doesn't have to be at the very top, the stack pointer just has to start at the highest address in the chunk used for that stack.
There is no such thing as the "main thread of the OS". Every process has its own set of threads, and those threads are specific to that process, not shared. Typically, at any given point in time, most threads on a system will be suspended awaiting input.
Every thread in a process has its own stack, which is allocated when the thread is created. Most operating systems will leave some space between each stack to allow them to grow if needed, and to prevent them from colliding with each other.
Every thread also has its own set of CPU registers, including a stack pointer (pointing to a location in that thread's stack).
How is stack space allocated (in the same address space) to each thread of a process in Linux or any other OS for that matter?
It depends on the type of thread library, a user space library like pthreads would allocate memory and divide it into thread stacks. On the OS side each thread would get a kernel stack.
On creation of new thread, the operating system reserves space in stack segment for current thread (parent), where the future auto variables and function call data of parent will live. Then, it allocates one guard page (this is to prevent the parent colliding into child stack, but this may vary with different operating systems). Once this is done, the stack frame for child thread is created (which is typically one-two page(s)).
This process is repeated in case the parent spawns multiple threads. All these stack frames live in stack segment of address space of process whose all these threads are part of.
We know that thread has its own stack it's implemented within the process. But my question is that when thread is implemented in his own stack that time it is the same stack which used by process or any other function?
One more doubt that thread share it's global variable,file descriptor, signal handler etc. But how it's share all these parameters within same address where all the threads executed?
Brief explanation will be appreciated.
when thread is implemented in his own stack that time it is the same stack which used by process or any other?
Can't quite parse this but I get the gist I think.
In most cases, under Linux in a multithreaded application, all of the threads share the same address space. Each thread if it is running on a separate processor may have local cached memory but the overall address space is shared by all threads. Even per-thread stack space is shared by all threads -- just that each thread gets a different contiguous memory area.
But how it's share all these parameters within same address?
This is also true of the global variables, file descriptors, etc.. They are all shared.
Most thread implementations running under Linux use the clone(2) syscall to create new thread processes. To quote from the clone man page:
clone() creates a new process, in a manner similar to fork(2). It is actually a library function layered on top of the underlying clone() system call, hereinafter referred to as sys_clone. A description of sys_clone is given toward the end of this page.
Unlike fork(2), these calls allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers.
You can see the cloned processes by using ps -eLf under Linux.
I was going through the man pages of pthread_join and its mentioned the following
"When a joinable thread terminates, its memory resources (thread
descriptor and stack) are not deallocated until another thread performs
pthread_join on it. Therefore, pthread_join must be called once for
each joinable thread created to avoid memory leaks."
The reason has for doing this has been mentioned as to avoid memory leaks. But, I am not sure why in the first place the resources for a particular thread need to be still kept upon its termination.
Jayaraj
The thread descriptor is kept around so the thread procedure return value could be retrieved through the second parameter of pthread_join(2). This is the same idea as with zombie processes.
Keeping the stack of terminated thread was probably a requirement of the particular thread library implementation at the time the API was formalized.