Key Registers for Multiprogramming
- Program Counter (PC) holds address of the next instruction in RAM
- When a process is interrupted, the OS saves the program counter in that process’s process table entry
- Instruction Register (IR) stores instruction currently being executed
- Needed so CPU knows what operation to be performing
- Stack Pointer (SP) points to the top of the execution stack
- Needed because function calls create stack frames that must be preserved across process switches
- Each process has its own SP for its own independent stack
The Stack
The stack is a section of memory that stores temporary data while a program runs
- It’s mainly used for:
- Function parameters
- Local variables
- Return addresses
- The stack pointer always points to the most recently added item
- i.e. the top of the stack
- A stack frame is a “slice” of the stack dedicated to one function call
- Created when function is called and destroyed when it returns
- Memory Address Register (MAR)
- Holds address CPU wants to read/write
- Memory Buffer Register (MBR)
- Temporarily holds data read from or to be written into memory
- Program Status Word (PSW) (or Flags register)
- Holds CPU state flags indicates current state of the CPU
- e.g. zero, carry, overflow, interrupt-enable, error bits
- Saved/restored on context switches
- Encodes important process state
- Holds CPU state flags indicates current state of the CPU
OS must save a selection of registers (including, PC, SP and PSW) to the process control block during a context switch so the process can resume exactly where it left off
Process Control Block (PCB): An OS data structure that stores all the information needed to manage and resume a process, including its CPU state (PC, registers, PSW), scheduling details, memory management info, and I/O status.