Operating Systems Lecture 4: Process Execution Mechanisms
Understanding CPU Process Execution
Overview of Process Execution
- The lecture focuses on how processes are executed by the CPU and the mechanisms involved in switching between processes.
- It begins with an explanation of the operating system's role in creating a memory image for a process, which includes code, data, stack, and heap.
Setting Up Process Execution
- The OS configures various registers like the program counter and stack pointer before allowing the process to execute directly on the CPU.
- Once set up, the OS steps back as the process runs autonomously, executing instructions fetched by the CPU.
Function Calls vs. System Calls
Function Call Mechanism
- A function call involves jumping from one instruction to another within a program flow.
- When calling a function, a new stack frame is created; this includes pushing the old program counter value onto the stack to facilitate returning after execution.
Stack Management During Function Calls
- The stack pointer is updated to reflect this new frame, storing arguments and return addresses necessary for proper execution flow.
- Upon completion of a function call, the stack frame is popped off and control returns using the saved program counter value.
Differences Between Function Calls and System Calls
Privilege Levels in CPUs
- Modern CPUs operate at multiple privilege levels: user mode (lower privilege for user code) versus kernel mode (higher privilege for OS code).
Kernel Mode Operations
- Certain instructions can only be executed in kernel mode; if attempted in user mode, they will be denied access to maintain security.
Separate Stacks for User and Kernel Code
- In kernel mode operations, a separate kernel stack is used instead of relying on user-provided stacks due to security concerns.
Trust Issues with User Input
Understanding System Calls and Trap Instructions
Overview of System Calls
- A system call is distinct from a function call as it involves predefined addresses that the hardware recognizes, eliminating the need for user input to specify these addresses.
Mechanism of a System Call
- When a system call is invoked, the compiler or C library inserts a special trap instruction. This initiates the transition from user mode to kernel mode.
- The trap instruction elevates the CPU's privilege level, indicating that it has entered kernel mode. It also updates the stack pointer to switch from user stack to kernel stack.
- Context saving occurs on the kernel stack, where essential information such as program counter values and register states are stored for later retrieval.
- The trap instruction references an interrupt descriptor table (IDT), determining which operating system code address to jump to for executing the requested service.
Types of Trap Instruction Execution
- The trap instruction executes in three primary scenarios:
- During a system call when services are requested.
- When a program fault occurs (e.g., accessing unauthorized memory).
- Upon receiving an external interrupt (e.g., keyboard input or network packet arrival).
Returning from a Trap
- After executing an operating system service, a special return-from-trap instruction is called. This undoes changes made by the initial trap instruction and restores context.
- The return-from-trap resets CPU privilege levels back to user mode and restores registers and program counter values so that execution can continue seamlessly in user space.
Kernel Mode to User Mode: Context Switching Explained
Understanding Process Resumption
- The execution resumes after a system call, but the timing may vary based on whether the call is blocking or non-blocking.
- It is not mandatory to return to the same process; the operating system can switch back to a different process if needed.
Reasons for Switching Processes
- There are scenarios where returning to the original process isn't possible, such as when it has terminated or encountered an error (e.g., segmentation fault).
- A process might be blocked waiting for resources (like disk data), making it impractical to return to it immediately.
- The operating system may choose not to return to a long-running process in favor of time-sharing among multiple processes.
Context Switch Mechanism
- When transitioning from user mode back into kernel mode, a context switch may occur, allowing movement from one process's user mode back into another's.
- The scheduler within the OS manages this context switching and consists of two parts: policy and mechanism.
Types of Schedulers
- Non-preemptive schedulers only initiate a context switch when a running process cannot continue (blocked or terminated).
- Preemptive schedulers can forcefully switch processes even if the current one is ready, often triggered by timer interrupts.
Timer Interrupt and Scheduling Decisions
- Timer interrupts allow the OS to periodically regain control and assess whether it should continue with the current process or switch.
- Even if a running process is still ready, preemptive scheduling allows for switching away based on time limits set by interrupts.
Example of Context Switching
- In practice, during a context switch from Process A to Process B:
- The state of Process A (program counter, stack pointer, registers) is saved onto its kernel stack.
Context Switching in Operating Systems
Understanding Context Switching
- The operating system (OS) can switch out a process (B) and save its state, including the program counter and registers, allowing it to resume from where it left off when switched back in.
- When resuming process B, the CPU operates in kernel mode for B, enabling execution of tasks specific to that process before returning control back to user mode.
- During context switching, the OS saves all states of the current process in its kernel stack and then switches to another process's kernel stack, effectively running a different process.
Mechanisms of Saving Context
- Context refers to the program counter and various CPU registers that represent the current execution state of a process. This context is crucial for maintaining continuity during switches.
- Context is saved at two key moments:
- When transitioning from user mode to kernel mode due to a system call or trap.
- When performing a context switch between processes while still in kernel mode.
Scenarios for Saving Context
- The first scenario involves saving context when a system call occurs; this includes storing where execution stopped in user mode onto the kernel stack.
- The second scenario occurs during a context switch between processes. Here, the OS saves all relevant information about the current process on its kernel stack before switching contexts.
- Key situations requiring context saving include:
- System calls or traps initiated by interrupts.