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
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.
Related
I am working on Pintos.
Which is sort of like an educational tool for learning about building operating systems, and am on the second project which is geared around building support for user programs.
So, first order of business is to Set up The Stack! Great.
Problem is - since the beginning of the class I've been shuddering at those words The Stack - because I can never quite get a grasp around what The Stack is and how it plays into the execution of a program or thread. So I understand it is an area of memory set up in RAM, but that's about it.
My questions are as follows:
What is the function of the stack?
How does "The Stack" play into the execution of a thread in the CPU, with respect to the Program Counter, Registers, and Stack Pointer?
How are things added to the stack and how are they removed from it?
Furthermore, even if you don't know about Pintos, what does it mean to "set up the stack" when building support for user programs in an operating system?
A stack is just memory. The only thing that makes memory a stack is that the process accesses it Last In First Out.
What is the function of the stack?
The function of a stack in a computer is to support function calls. Function calls mirror the operation of a stack. Calling a function pushes it. Exiting a function pops.
How does "The Stack" play into the execution of a thread in the CPU, with respect to the Program Counter, Registers, and Stack Pointer?
From the CPU's perspective a thread is a process. Operating systems trick the CPU by having multiple processes share the same address space. Thus the process becomes a thread.
The program counter and stack pointer are registers. On most processors there are instructions that manipulate the stack pointer register. For example, a function call instruction will push the program counter on to the stack by decrementing the stack pointer and storing the program counter at the new location the referenced by the stack pointer.
How are things added to the stack and how are they removed from it?
Stack memory is allocated by decrementing the stack pointer. Something like:
SUB #32, SP
will allocate 32 bytes on the stack and
ADD #32, SP
will free that memory. The advantage of the stack is that it is very fast for allocating memory.
In addition, as mentioned above, some instructions are likely to manipulate the stack.
Furthermore, even if you don't know about Pintos, what does it mean to "set up the stack" when building support for user programs in an operating system?
To set up a stack you have to:
Allocate memory for the stack.
You might also want to allocate guard memory that is protected on either side of the stack to detect overflows and underflows.
You assign move the address of the top of the stack into the state pointer register.
As I said before, a stack is just memory. A program can easily allocate its own memory and move its address into the stack pointer to create a new stack.
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.
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.
Threads have their own call stack, then what kind of memory do different threads share. Do they have their own stack memory within the address space of a process? Is that memory sufficient for spawning 100s of threads? If a process has an object B, in case of Java it will be created on the heap. So, how are threads spawned by that process able to have access to that object on the heap ?
kind of memory do different threads share
"All the process (user mode) memory" is available on all threads, this means that you can share objects stored a thread stack to other thread.
Do they have their own stack memory within the address space of a
process
Yes each thread have its own stack for running.
Is that memory sufficient for spawning 100s of threads?
Yes check http://msdn.microsoft.com/en-us/library/windows/desktop/ms686774(v=vs.85).aspx
So, how are threads spawned by that process able to have access to
that object on the heap ?
I think that I answer that in the first question