`

CPU Scheduling

Objective: To maximize CPU utilization and throughput while minimizing response time, turnaround time, and waiting time.

Key Tasks:

Scheduling Criteria

  1. CPU Utilization: Keep the CPU busy as much as possible.
  2. Throughput: Number of processes that complete their execution per time unit.
  3. Turnaround Time: Time taken to complete a process from submission to termination.
  4. Waiting Time: Total time a process spends waiting in the ready queue.
  5. Response Time: Time taken to respond to a request.

Scheduling Metrics

  1. CPU Utilization:
    • Fraction of time the CPU is busy.
    • Utilization = 1 - (Idle Time / Total Time)
  2. Throughput:
    • Number of processes completed per unit time.
    • Throughput = Number of processes / Total Time
  3. Average Waiting Time:
    • Average time a process spends waiting in the ready queue.
    • Waiting Time = Total Waiting Time / Number of Processes
  4. Average Completion Time:
    • Average time taken to complete a process.
    • Completion Time = Total Completion Time / Number of Processes

Some common CPU Scheduling formulas

  1. Turnaround Time = Completion Time - Arrival Time
  2. Waiting Time = Turnaround Time - Burst Time
  3. Response Time = Time of first response - Arrival Time

Common Strategies

Scheduling Algorithms

  1. First-Come, First-Served (FCFS):
    • Processes are scheduled in the order they arrive.
    • Simple to implement
    • Leads to “convoy effect” (short process behind long process)
  2. Shortest Job First(SJF):
    • Process with the smallest execution time is selected first.
    • Minimizes average waiting time.
    • Disadvantage: Requires knowledge of burst times in advance and prone to starvation of long processes.
  3. Preemptive SJF / Shortest Remaining Time First (SRTF):
    • Current process can be preempted / interrupted if a new process arrives with a shorter burst time.
    • Better responsiveness and average waiting time.
    • Increases overhead due to context switching.

    SJF SJF-Timeline

  4. Priority Scheduling:
    • Each process is assigned a priority.
    • Process with the highest priority is selected first.
    • Disadvantage: Starvation of low priority processes.
  5. Priority Inversion & Priority Inheritance:
    • Priority Inversion occues when a high priority process is waiting for a lower priority process to release a resource, but the lower priority process is preempted by a medium priority process.

    Example:

    • Low-priority process (P1) holds a mutex (lock).
    • High-priority process (P2) is waiting for the mutex.
    • A medium-priority process (P3) keeps running, preempting P1.
    • This prevents P1 from releasing the mutex, causing P2 to wait indefinitely.

    Solution -> Priority Inheritance:

    • The priority of the lower priority process is temporarily raised to match that of the higher priority waiting process.

    How it works:

    • When P2 (high priority) waits for a mutex held by P1, P1’s priority is boosted to match P2’s.
    • P1 completes and releases the resource.
    • P1’s priority is restored to its original level.

    Mutex and Priority Handling

    • Mutexes often have mechanisms like priority ceiling or priority inheritance to avoid priority inversion.
    • In priority ceiling protocols, a task that locks a mutex automatically inherits the highest priority associated with that mutex.
    • This ensures:
      • Quick access to critical sections.
      • Minimal chances of priority inversion.

    PriorityInversion

    Why Mutexes Have the Largest Priority:

    • Mutex locks protect critical resources.
    • Giving mutex operations the highest priority ensures:
      • Swift acquisition and release of resources.
      • Avoidance of deadlocks and bottlenecks in high-priority tasks.
  6. Round Robin (RR):
    • Each process is assigned a fixed time slice (time quantum).
    • Process is preempted if it exceeds its time quantum.
    • Fair and prevents starvation.
    • Disadvantage: Too small of a time quantum increases the context switching overhead, too large of a time quantum leads to FCFS.

    RR

  7. Multilevel Queue Scheduling:
    • Processes are divided into multiple queues based on their type or priority (e.g., system processes, interactive processes, batch processes).
    • Each queue has its own scheduling algorithm.
    • Queues are scheduled in a priority order.
  8. Multilevel Feedback Queue Scheduling: Issues with Multilevel queue were that,
    • How to know which queue to put a process in?
    • What happens if a task changes its behavior?
    • How to prevent starvation?
    • How to know if a task is CPU-bound or I/O-bound?

    Solution -> Multilevel Feedback Queue Scheduling:

    • Add the task to the highest priority queue.
    • If the task uses its entire time quantum, it is moved to a lower priority queue.
    • Boost the priority of a task that waits for a long time.
    • This makes sure that processes can move between queues are not stuck in a rigid structure, and can be scheduled based on their behavior.

O(1) Linux Scheduler

Designed to select the next process to run in O(1) constant time, regardless of the number of processes in the system.

Key Features:

O(1) Scheduler Algorithm:

  1. Select the highest priority process from the active array.
  2. If the active array is empty, swap the active and expired arrays.
  3. If the expired array is empty, select the highest priority process from the active array.
  4. If the active array is still empty, select the idle task.

Issues:

CFS (Completely Fair Scheduler)

Designed to provide fairness whilst maintaining efficiency.

Fairness Concept

Virtual Runtime

Red-Black Tree

Dynamic Priority Adjustment

CFS Algorithm:

  1. Select the leftmost node from the red-black tree.
  2. Update the vruntime of the selected process.
  3. Rebalance the tree if necessary.

Issues: High overhead due to red-black tree operations. -> O(log n)

Multi-Core & Multi-CPU Scheduling & Simultaneous Multi-Threading (SMT)

Shared Memory Architecture:

Scheduling Challenges:

Scheduling Strategies:

Global Scheduling:

Per-Core Scheduling:

Hybrid Scheduling:

Processor Affinity:

Soft Affinity:

Hard Affinity:

Simultaneous Multithreading (SMT):

Cache Affinity in CPU Scheduling

Keep tasks running on the same CPU to maximize cache hits and minimize cache misses.

Locality-Based Scheduling:

Load Balancing (LB):

A load balancer and scheduler work together to ensure optimal CPU utilization and cache affinity.

RAM Affinity in CPU Scheduling

Optimize memory access speed by assigning tasks to CPUs closer to the RAM node containing their data.

NUMA (Non-Uniform Memory Access):

Task Placement:

NUMA-Aware Scheduling:

Load Balancing:

Hyper-Threading (HT) in CPUs

Hardware multithreading technique where a single CPU core executes multiple threads simultaneously by sharing resources like ALUs and caches.

Execution Context with Registers: Each thread has its own set of registers to store the execution context, enabling quick switching.

Hardware Multithreading:

CMT vs SMT:

Hides Memory Latency: When one thread stalls (e.g., waiting for memory), another thread can utilize the CPU.

Performance Optimization:

Task Allocation:

How to know a task is CPU or Memory Bound?