Mechanism: Limited Direct Execution

  • By time sharing the CPU, virtualization is achieved

    • Challenges

      • Performance: how can we implement virtualization without adding excessive overhead to the system?

      • Control: how can we run processes efficiently while retaining control over the CPU?

6.1 Basic Technique: Limited Direct Execution

Problem: program might do something harmful, and how is OS able to stop the program from running and switch to another process?

6.2 Problem #1: Restricted Operations

  • Restricted operations: I/O request to disk, or gaining access to more system resources like CPU or memory

    • system call is a procedure call, but hidden inside that procedure call is the trap instruction

  • Approach

    • User mode: code that runs in user mode is restricted in what it can do

    • Kernel mode: OS (or kernel) runs in, code can do what it likes, including privileged operations

    • System call: allows the kernel to carefully expose certain key pieces of functionality to user programs

      • Trap + return from trap (trap table)

  • system call number

    • A form of protection

  • LDE protocol

    • At boot time, kernel initializes the trap table, and the CPU remembers its location for subsequent use

    • When running a process, the kernel sets up a few things (e.g. allocating a node on the process list, allocating memory) before using a return-from-trap instruction to start the execution of the process; this switches the CPU to user mode and begins running the process

6.3 Problem #2: Switching Between Processes

Key: How can the OS regain control of the CPU so that it can switch between processes?

A Cooperative Approach: Wait for System Calls

  • yield system call

  • Or, when the applications do some illegal operations

A Non-Cooperative Approach: The OS Takes Control

  • How can the OS gain control of the CPU even if processes are not being cooperative? What can the OS do to ensure a rogue process does not take over the machine?

  • A timer interrupt (hardware feature)

    • Pre-configured interrupt handler in the OS runs, regain control of the CPU. Can stop and resume another one.

  • Saving and restoring context

    • Whether to continue running or switch to another one. Decision made by scheduler

      • If switch: executes a low-level piece of code which is context switch

        • save the general purpose registers, PC, and the kernel stack pointer of the currently-running process, and then restore said registers, PC, and switch to the kernel stack for the soon-to-be-executing process

6.3 Concurrency?

  • what happens when, during a system call, a timer interrupt occurs?

  • What happens when you’re handling one interrupt and another one happens? Doesn’t that get hard to handle in the kernel?

  • Methods

    • Disable interrupts when handling (but need to be careful that disabling interrupts for too long could lead to lost interrupts

    • Locking mechanism


  • Context switch / system call time measuring tool:

  • Note: many OS operations are memory intensive, and memory bandwidth has not improved as dramatically as processor speed over time. Depending on the workload, buying the latest and greatest processor may not speed up OS as much as you might hope!

Last updated