Search code examples
multithreadingoperating-systemstackcpupintos

How does "the Stack" play into the execution of a thread?


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?

Solution

  • 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:

    1. Allocate memory for the stack.
    2. You might also want to allocate guard memory that is protected on either side of the stack to detect overflows and underflows.
    3. 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.