80 likes | 109 Views
The Stack. This is a special data structure: The first item to be placed into the stack will be the last item taken out. Two basic operations: Push: Places items on the stack New items will be placed on top of old ones. Pop: Removes items from the stack.
E N D
The Stack • This is a special data structure: • The first item to be placed into the stack will be the last item taken out. • Two basic operations: • Push: Places items on the stack • New items will be placed on top of old ones. • Pop: Removes items from the stack. • Removes most recently placed item from the stack and returns it. • Cannot pop a specific item from the stack without popping items pushed after it.
The Process Context • Programs running in memory are called “processes” • A process would make use of registers, while the processor will use the Program Counter to keep track of which instruction in the program is to be executed next. • The contents of the registers and the Program Counter is called the “context” of the process.
Saving the Process Context • When an interrupt occurs, the currently executing process must be suspended so that the interrupt handler can handle the interrupt. • We must save the address of the next instruction of the process to be executed so that we can resume the process at the point it was interrupted. • The handler may modify register values, and the original values must be saved. • The suspended process must be able to resume after the handler has finished handling the interrupt, and it must continue as though the interrupt never occurred. • To achieve this it is vital to save the context of the process.
Using the Stack • The stack is a natural tool for saving process contexts. We can modify the interrupt handling procedure thus: • Suppose a process P with context Cp is running when an interrupt I occurs. • Save the context of P into the stack: push(Cp). • The result is that the address of the next instruction to be executed is saved together with the current register values. • Place the address of the interrupt handler into the program counter (you can obtain the address from an interrupt vector table, or from a fixed location. E.g. in MIPS the interrupt handler is always at address 0x4000040). This will cause the interrupt handler to be executed.
Using the Stack • When the interrupt handler finishes, do a pop() to restore the context of Process P. • The PC is now restored to the address of the next instruction to be executed in Process P, the registers are restored to their former values • This causes the process P to continue executing at the point where it left off.
Nested Interrupts • As an interrupt handler is executing, it is possible that a higher priority interrupt occurs. • In this case, the interrupt handler is treated like any other process • The context of the interrupt handler is pushed onto the stack. • The new interrupt handler is executed. • When it ends, the context of the previous handler is restored, and the previous handler executes to completion.
Nested Interrupts • When the previous interrupt handler ends, it restores the context of the interrupted process, and the process continues where it left off.