Processes
Mode Transfer Wrap up
- mode switch control flow
- enters the kernel mode, hw saves process's PC & SP, switch to kernel stack, set PC to a kernel handler (from IDT)
- kernel handler pushes the rest of process's states (registers) onto the kernel stack
- handles the specific syscall, exception, or interrupt
- restores most of the process's state (pops off the stack)
- returns back to user mode, hw restores saved PC and stack pointer, process resumes execution
- system call arguments & return value
- how are syscall arguments passed in?
- what kind of validation must kernel perform on these arguments?
- how does the kernel set the return value for a system call?
Process Abstraction
- a running instance of a program, consists of:
- execution stream (thread)
- instruction pointer, stack, registers
- virtual address space
- includes code, data, stack, and heap regions
- process metadata, also called Process Control Block (PCB)
- information tracked by kernel for process management
- includes: process id, kernel stack, virtual memory metadata, open files, etc.
- isolation and protection boundary for user programs
- a process's failure shouldn't affect other processes
- a process shouldn't have visibility into other processes
- unless explicitly permitted through OS APIs (e.g. fork, shared memory)
Process Implementation
- how does a program become a process?
- ELF loading
- contains entry point of the program & how the initial address space should look like
- kernel needs to set up the execution state of the process
- the user stack
- allocates a stack page
int main(int argc, char** argv)
- sets up the argv array on the stack
- initial values for the registers
%rdi
=> argc, %rsi
=> argv
%rsp
=> current stack position, %rip
=> entry point of the program
- PCB initialization
- allocates a kernel stack
- sets up the trapframe for the process so it can return to user mode with proper states
- how do processes share a CPU
- scheduling
- a OS level policy deciding which process runs on the CPU
- easiest option: let each process run to completion in FIFO order
- what would the user experience look like?
- want to appear as processes are running simulatenously
- processes take turn making progress intead of running till completion
- must have a way to seamlessly switch between processes
- context switching: switching from one process/thread to another
- need to save & restore the kernel execution state of each process
- common policy: Round Robin
- processes take turn running x ms at a time
- x => time slice/quantum, typically 10-100 ms