Imprint | Privacy Policy

Scheduling

(Usage hints for this presentation)

IT Systems, Summer Term 2024
Dr. Jens Lechtenbörger (License Information)

1. Introduction

1.1. Core Questions

  • How does the OS manage the shared resource CPU? What goals are pursued?
  • How does the OS distinguish threads that could run on the CPU from those that cannot (i.e., that are blocked)?
  • How does the OS schedule threads for execution?

(Based on Chapter 3 of (Hailperin 2019))

1.1.1. CPU Scheduling

CPU scheduling

CPU scheduling

Figure © 2016 Julia Evans, all rights reserved; from julia's drawings. Displayed here with personal permission.

1.2. Learning Objectives

  • Explain thread concept (continued)
    • Including states and priorities
  • Explain scheduling mechanisms and their goals
  • Apply scheduling algorithms
    • FCFS, Round Robin

1.3. Retrieval Practice

  • Before you continue, answer the following; ideally, without outside help.

1.3.1. Thread Terminology

Agenda

2. Scheduling

2.1. CPU Scheduling

  • With multitasking, lots of threads share resources
    • Focus here: CPU
  • Scheduling (planning) and dispatching (allocation) of CPU via OS

      • Typical case for desktop OSs
      • Illusion of parallel executions, even on single-core CPU
        1. Among all threads, schedule and dispatch one, say T0
        2. Allow T0 to execute on CPU for some time, then preempt it
        3. Repeat, go to step (1)
  • (Similar decisions in operations management)

2.2. Sample Scheduling Goals

  • Scheduling is hard; various goals with trade-offs
    • Improvement for one goal may negatively affect others
  • Performance
    • Response time
      • Time from thread start or interaction to useful reaction
    • Throughput
      • Number of completed threads (computations, jobs) per time unit
      • More important for service providers than users
  • User control
    • Resource allocation
    • Mechanisms for urgency or importance, e.g., priorities

2.3. Thread Priorities

  • Different OSs (and execution environments such as Java) treat priorities differently
    • E.g., numerical priority, so-called niceness value, deadline, …
    • Upon thread creation, its priority can be specified (by the programmer, with default value)
      • Priority recorded in TCB
      • Sometimes, administrator privileges are necessary for “high” priorities
      • Also, OS tools may allow changing priorities at runtime
    • Scheduling takes priorities into account
      • Potentially with preemption

3. Thread States

3.1. OS Thread States

  • Different OSs distinguish different sets of states; typically:
    • Running: Thread(s) currently executing on CPU (cores)
    • Runnable: Threads ready to perform computations
    • Waiting or blocked: Threads waiting for some event to occur
  • OS manages states via queues (with suitable data structures)
    • Run queue(s): Potentially per CPU core
      • Containing runnable threads, input for scheduler
    • Wait queue(s): Potentially per event (type)
      • Containing waiting threads
        • OS inserts running thread here upon blocking system call
        • OS moves thread from here to run queue when event occurs

3.2. Thread State Transitions

Figure 3.3 of cite:Hai17

Figure 3.3 of cite:Hai17

Figure by Max Hailperin under CC BY-SA 3.0; converted from GitHub

3.3. Scheduling Vocabulary

4. Scheduling Mechanisms

4.1. Three Families of Schedulers

  • Families of schedulers

    • Dynamically adjusted thread priorities

      • E.g., decay usage in Mac OS X, priority boosting in Windows
    • Controlling proportional shares of processing time

      • E.g., weighted variants of other algorithms and Completely Fair Scheduler (CFS) in Linux
        • Fairness: All parties receive their share

Scheduling Mechanisms

4.1.1. Notes on Scheduling

  • For scheduling with pen and paper, you need to know arrival times and service times for threads

    • Arrival time: Point in time when thread created
    • Service time: CPU time necessary to complete thread
      • (For simplicity, blocking I/O is not considered; otherwise, you would also need to know frequency and duration of I/O operations)
  • OS does not know either ahead of time

    • OS creates threads (so, arrival time is known), inserts them into necessary data structures
    • When threads terminate, OS again participates
      • Thus, OS can compute service time after the fact
      • (Some scheduling algorithms require service time for scheduling decisions; then threads need to declare that upon start. Not considered here.)

4.2. Fixed-Priority Scheduling

  • Use fixed, numerical priority per thread

    • Threads with higher priority preferred over others
      • Smaller or higher numbers may indicate higher priority: OS dependent
    • Implementation alternatives

      • Single queue ordered by priority
      • Or one queue per priority
        • OS schedules threads from highest-priority non-empty queue
    • Beware!

4.2.1. FIFO/FCFS Scheduling

  • FIFO = First in, first out
    • (= FCFS = first come, first served)
    • Think of queue in supermarket
  • Non-preemptive strategy: Run first thread until completed (or yield or blocking)
    • For threads of equal priority

4.2.2. Round Robin Scheduling

  • Key ingredients

    • Time slice (quantum, q)
      • Timer with interrupt, e.g., every 30ms
    • Queue(s) for runnable threads
      • Newly created thread inserted at end
    • Scheduling when (1) timer interrupt triggered or (2) thread ends, yields, or is blocked

      1. Timer interrupt: Preempt running thread

        • Move previously running thread to end of runnable queue (for its priority)
        • Dispatch thread at head of queue (for highest priority) to CPU
          • With new timer for full time slice
      2. Thread ends, yields, or is blocked

        • Cancel its timer, dispatch thread at head of queue (for full time slice)

4.3. When to Schedule

4.4. Self-Study Task for Scheduling

This task is available for self-study in Learnweb.

Perform Round Robin scheduling given the following situation:

q=4 Thread Arrival Time Service Time
  T1 0 3
  T2 1 6
  T3 4 3
  T4 9 6
  T5 10 2

5. Conclusions

5.1. Summary

  • OS performs scheduling for shared resources
    • Focus here: CPU scheduling
    • Subject to conflicting goals
  • CPU scheduling based on thread states and priorities
    • Basic approaches use fixed priorities
    • Desktop OSs keep track of time on CPU, priorities, and events for more advanced scheduling

Bibliography

Hailperin, Max. 2019. Operating Systems and Middleware – Supporting Controlled Interaction. revised edition 1.3.1. https://gustavus.edu/mcs/max/os-book/.

License Information

Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.

Except where otherwise noted, the work “Scheduling”, © 2017-2024 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.