Control Stack
The Control Stack is a memory structure used by operating systems and programs to manage the execution of processes and functions.
Resources
Control Stack vs. Stack Frame?
A stack consists of multiple stack frames.
- The stack frame is a section within the control stack, dedicated to one function call
- The control stack is composed of multiple stack frames
- See Stack Frame
User Stack
This is the stack that you are used to using when you are running a program. The stuff you learned in CS241E.
Kernel Stack
This can only be accessed in kernel mode. It’s used to store the context of a process before calling an ISR.
How is it possible that the kernel stack contains multiple stack frames?
Since I suppose the program is resumed as soon as the ISR is triggered. How is it possible to have multiple stack frames pushed to the kernel stack?
- In the case that there are multiple interrupts triggered, this kernel stack can be used
This conversation was helpful too: https://chatgpt.com/share/c7a6047d-b212-4da6-ad5e-6853f5cbc9eb
Each process has its own dedicated control stack.
- Each process has its own user stack
- Each process has its own kernel stack (though you need to be in kernel-mode to access that stack)
Also see Interrupt Handling.
This ensures that each process maintains its own independent execution state, including function calls, local variables, and return addresses.
Every time there is a Process Switch, we need to store the value of the registers onto the control stack.
Why use the control stack when you have the Process Control Block?
- The control stack is used for temporary, fast context saving during interrupts and system calls. It stores the immediate CPU state (registers, program counter, etc.) for quick restoration once the interrupt is handled.
- The PCB is used for long-term process management and stores the full context of a process, including additional metadata. It is updated during context switches but not necessarily for every interrupt.
So PCB is used for a Process Switch. But if it’s just an interrupt, no need to update PCB.
- The PCB is not organized in the same simple, sequential manner as a stack. Instead:
- The PCB contains various fields (process ID, process state, priority, file descriptors, etc.) that are spread across memory.
- Updating or accessing the PCB may involve accessing different memory locations, and the data is not always laid out contiguously like it is in a stack.
- The PCB is accessed less frequently (mainly during context switches), and since it stores more information than the control stack (not just the CPU registers), accessing or modifying the PCB is inherently slower and more complex than stack operations.