Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling

CPU Scheduling Overview

  • CPU scheduling is fundamental to multiprogrammed operating systems.

  • The operating system increases productivity by switching the CPU among processes.

  • Topics covered in this chapter:

    • Introduction to CPU scheduling as the basis for multiprogrammed operating systems.

    • Description of various CPU-scheduling algorithms.

Basic Concepts

  • In a single-processor system, only one process runs at a time; others must wait until the CPU is free.

  • Objective of Multiprogramming:

    • Keep some process running at all times to maximize CPU utilization.

  • Process execution occurs until the process must wait (e.g., for I/O completion).

  • In a straightforward computing environment, the CPU becomes idle during waiting times, representing wasted time.

  • Multiprogramming seeks to reduce idle wait time by maintaining several processes in memory at once:

    • When one process waits, the OS allocates the CPU to another process, continuing this pattern.

CPU Burst and I/O Burst Cycles

  • Process execution alternates between CPU execution and I/O wait states:

    • Cycle: CPU burst → I/O burst → CPU burst → I/O burst (and so forth).

    • CPU burst: Period where the process executes in the CPU.

    • I/O burst: Period when the CPU waits for I/O to complete.

  • The final CPU burst concludes with a system request to terminate execution.

Preemptive and Non-Preemptive Scheduling

  • CPU Scheduler:

    • When the CPU is idle, the OS selects one process from the ready queue to execute using the short-term scheduler.

    • The scheduler allocates the CPU to the selected process from memory that is ready for execution.

  • Dispatcher:

    • Manages control of the CPU to the process selected by the short-term scheduler.

    • Dispatch latency: Time taken to stop one process and start another.

CPU Scheduling Conditions

  • CPU scheduling decisions occur under four conditions:

    1. Process transitions from running state to waiting state (e.g., I/O request).

    2. Process transitions from running state to ready state (e.g., an interrupt).

    3. Process transitions from waiting state to ready state (after I/O completion).

    4. Process termination.

  • Conditions 1 and 4 dictate scheduling with no choices. Choices exist in conditions 2 and 3 for which process to allocate CPU time.

Preemptive vs Non-Preemptive

  • Scheduling is considered non-preemptive when it occurs in conditions 1 and 4, showing cooperative scheduling; otherwise, it is preemptive.

Scheduling Criteria

  • CPU Utilization:

    • Aim to keep CPU as busy as possible (ideally between 40% - 90%).

  • Throughput:

    • Number of processes completed per unit of time, indicating work done.

  • Turnaround Time:

    • The time taken to execute a process.

    • Total time from submission to completion:

    • extTurnaroundTime=extwaittimeinmemory+extwaittimeinreadyqueue+extexecutingtimeonCPU+extI/Otimeext{Turnaround Time} = ext{wait time in memory} + ext{wait time in ready queue} + ext{executing time on CPU} + ext{I/O time}

  • Waiting Time:

    • Affected by the CPU scheduling algorithm; it only includes time spent in the ready queue.

  • Response Time:

    • Relevant in interactive systems, measured as the time from request submission to the first response produced.

    • Turnaround time is generally limited by output device speed.

Scheduling Algorithm Optimization Criteria

  • Prioritize several key metrics during scheduling:

    • Maximize CPU utilization

    • Maximize throughput

    • Minimize turnaround time

    • Minimize waiting time

    • Minimize response time (for Round Robin).

First-Come, First-Served (FCFS) Scheduling

  • Simplest CPU scheduling algorithm; the first process requesting CPU gets executed first.

  • Managed with FIFO queue:

    • When entering the queue, the process control block (PCB) is linked at the tail.

    • The CPU is allocated to the head process when free.

  • Average Waiting Time under FCFS:

    • Often long due to possible wait from longer running processes.

Example of FCFS Scheduling
  1. Burst Times:

    • Process P1: 24 ms

    • Process P2: 3 ms

    • Process P3: 3 ms

  2. Gantt Chart:

    • Waiting time: P1 = 0; P2 = 24; P3 = 27.

    • Average waiting time: rac0+24+273=17extmsrac{0 + 24 + 27}{3} = 17 ext{ ms}

  3. Reordered Processes (P2, P3, P1):

    • Waiting times: P1 = 6; P2 = 0; P3 = 3.

    • Average waiting time: rac6+0+33=3extmsrac{6 + 0 + 3}{3} = 3 ext{ ms}

    • Demonstrates substantial reduction in waiting time.

  • FCFS is non-preemptive; running processes retain the CPU until completion or I/O request.

  • Issues: Convoy Effect, leading to long waits for short processes when longer processes precede them.

Shortest-Job-First (SJF) Scheduling

  • SJF algorithm schedules processes based on their next CPU burst length.

    • Allocates CPU to the process with the shortest future CPU burst.

  • Can be preemptive (interruption possible) or non-preemptive (no interruption).

  • Optimal for minimum average waiting time; knowing next CPU request length is challenging.

Example of SJF Scheduling (Non-Preemptive)
  • Processes with burst times:

    • P1: 6 ms

    • P2: 8 ms

    • P3: 7 ms

    • P4: 3 ms

  • Average waiting time: 7 ms (compared to FCFS's 10.25 ms).

Example of SJF Scheduling (Preemptive)
  • Processes:

    • P1: 8 ms (at time 0)

    • P2: 4 ms (at time 1)

    • P3: 9 ms (at time 2)

    • P4: 5 ms (at time 3)

  • Preemptive SJF also referred to as Shortest-Remaining-Time-First Scheduling.

  • Average Waiting Time Calculation leads to 6.5 ms.

Problems with SJF Scheduling

  • Difficulty lies in knowing the next CPU request length; thus, it may be impractical in real-time scheduling.

  • Solutions: Approximate SJF by predicting based on previous burst lengths.

Priority Scheduling

  • Allocates CPU based on process priority; equal priorities handled via FCFS.

  • SJF serves as a type of priority scheduling based on burst lengths as inversely proportional to priority.

  • Issues:

    • Starvation: Low-priority processes may wait indefinitely.

    • Solution: Aging, increasing priority over time to ensure execution opportunity.

Example of Non-Preemptive Priority Scheduling

  • Given processes:

    • P1: 10 ms (priority 3)

    • P2: 1 ms (priority 1)

    • P3: 2 ms (priority 4)

    • P4: 1 ms (priority 5)

    • P5: 5 ms (priority 2)

  • Gantt Chart and calculations yield an average waiting time of 8.2 ms.

Problems with Priority Scheduling

  • Indefinite blocking/starvation may occur for low-priority processes, especially under heavy load.

  • Aging solutions integrate priority increases over time to combat starvation effects.

Round Robin (RR) Scheduling

  • Specifically designed for time-sharing systems; incorporates preemption.

  • Each process receives a time quantum (10-100 ms).

  • Timer interrupts schedule the next process post the time quantum.

Implementation of Round Robin (RR) Scheduling
  • Assigns a time quantum for each process; after expiration, the process is moved to the queue end.

  • Two scenarios prevail after an interrupt:

    1. Process completes before quantum.

    2. Process exceeds quantum and is context-switched.

Example of RR Scheduling
  • Given processes with burst times:

    • P1: 24 ms

    • P2: 3 ms

    • P3: 3 ms

  • Gantt chart represented and calculations yield average turnaround time = 15.66 ms and average waiting time = 5.66 ms.

Scheduling Algorithm Characteristics

Algorithm

Preemptive

Non-Preemptive

FCFS

No

Yes

SJF

Yes

Yes

RR

Yes

No

Priority

Yes

Yes

Multilevel Queue Scheduling

  • Ideal for processes that can be classified into distinct groups (foreground vs background processes).

  • Different queues are implemented based on criteria such as memory size or process type.

  • Each queue can have its scheduling algorithm, e.g., RR for foreground and FCFS for background processes.

Implementation Example
  • Foreground queue to use RR; Background queue follows FCFS.

  • Fixed-priority preemptive scheduling may prioritize foreground queues over background.

Summary

  • Understanding CPU scheduling is critical for optimizing CPU performance in multiprogrammed operating systems. Various algorithms exist, each with distinct advantages and drawbacks influencing process execution times and system responsiveness. From FCFS to SJF, each algorithm reflects a unique approach to managing how processes interact with CPU resources, ultimately dictating overall system efficiency.