Week 03: Big Picture

How does the OS give each provides the illusion of its own CPU?

  • Limited direct execution by system calls → timer interrupts


Direct Execution:

  • OS loads program → jump to main() → program runs freely

OS:

  1. Create entry for process list

  2. Allocate memory for program

  3. Load program into memory

  4. Set up stack with argc/argv

  5. Clear registers

  6. Execute Call main()

  1. Free memory of process

  2. Remove from process list

Program:

  1. run main()

  2. execute return from main()

Big Problems (2):

  1. Restricted Operations: How can the OS make sure the program doesn’t do anything we don’t want it to do, while still running efficiently?

  2. Switching btwn processes: how does the OS stop the process from running and switch to another process


Limited Direct Execution:

  • Introduce the user and kernel mode so OS can maintain control over hardware access and protect system integrity by operating in a less privileged environment.

    • User Mode: restricted instructions, limited memory access

    • Kernel Mode: Full privilege

    • Trap Mechanism: Automatic mode switch on syscall


System Call Mechanism:

  1. User: Set args in register; place syscall number (ABI, Application Binary Interface)

  2. Hardware: Save PC/mode

  3. User Vector: Save registers to trapframe; switch to kernel stack

  4. User trap: read scause; dispatch

  5. Syscall(): vector to handler; validate; copy in/out; perform service

  6. Return: User trapret → usertset → stret

    1. What it does: The System Call Mechanism provides a structured procedure for user programs to request privileged services from the OS, such as interacting with hardware or accessing protected resources like files.

    2. Why we use it: It allows the OS to maintain control over the system and ensure its integrity. By acting as a controlled gateway to kernel mode, it prevents user programs from directly executing potentially dangerous operations, thereby protecting system resources and stability.

Image

Timer Flow:

  1. Boot: OS programs timer for Xms in future

  2. Timer fires → asynchronous hardware interrupt

  3. Scheduler decides: continue process A or start process A or start process B

  4. If switch - save A’s context, load B’s context


Context Switching:

Image

Context Switch: When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process

Context Includes:

  • User level context

  • Registers

  • System level context

Key Distinction: Mode switch is not the same as a context switch, as the Mode switch pertains to changing operational modes while the Context involves switching between different processes or tasks within the system.

Responsibilities:

  • Hardware - Enforce modes; perform traps; save/restore minimal state generate timer device interrupts

  • OS - set polices (e.g. which process next); manage contexts; implement syscall handlers

  • User Program - Request services via syscalls; is preempted; must follow ABI