Chapter 5

Chapter 5: CPU Scheduling

Introduction to CPU Scheduling

  • CPU scheduling is a crucial component of multiprogrammed operating systems.

  • By efficiently switching the CPU among various processes, the operating system can enhance overall productivity.

  • Key topics include:

    • Fundamentals of CPU scheduling as the basis of multiprogrammed systems.

    • Various CPU-scheduling algorithms that govern how processes are allocated CPU time.

Basic Concepts

  • In a single-processor system, only one process can be executed at any given time, meaning other processes must wait their turn for CPU resources.

    • Objective of Multiprogramming: To maximize CPU utilization by ensuring that some process is always running.

  • A process may execute until it is required to wait, typically due to I/O requests. In basic systems, this leads to CPU idle time without productive work.

  • Multiprogramming Approach: Multiple processes are kept in memory simultaneously.

    • When one process must wait, the OS allocates the CPU to another process, ensuring that some process is always running.

CPU and I/O Burst Cycles

  • Process execution entails a cycle involving both CPU and I/O operations:

    • CPU Burst: The time during which the process is actively executed on the CPU.

    • I/O Burst: The time when the CPU is idle, waiting for I/O operations to complete.

  • The execution cycle alternates between CPU and I/O bursts:

    1. CPU burst

    2. I/O burst

    3. Another CPU burst

    4. Another I/O burst

    • This cycle continues until the final CPU burst, which ends with a termination request.

Preemptive vs Non-Preemptive Scheduling

  • CPU Scheduler: Responsible for selecting a process from the ready queue whenever the CPU becomes idle.

    • The decision-making is performed by the short-term scheduler.

  • Dispatcher: The module that transfers control of the CPU to the selected process, including a delay termed dispatch latency.

  • Scheduling Circumstances: Decisions may arise in four key scenarios:

    1. A process moves from running to waiting (I/O).

    2. A process moves from running to ready (due to an interrupt).

    3. A process moves from waiting to ready (I/O completion).

    4. A process terminates.

  • In scenarios 1 and 4, scheduling is non-preemptive; other cases allow for preemptive selections.

CPU Scheduling Criteria

  • CPU Utilization: Aimed at keeping the CPU busy, ideally between 40% and 90% for practical systems.

  • Throughput: Measures the number of completed processes per time unit, indicative of work being done.

  • Turnaround Time: Total time from submission to completion of a process, including waiting in the queue and I/O times.

  • Waiting Time: Total time a process spends waiting in the ready queue, unaffected by execution or I/O times.

  • Response Time: In interactive systems, the time from request submission to the first response, crucial since turnaround time can be limited by output device speeds.

Optimization Criteria for Scheduling Algorithms

  • The objectives when selecting scheduling algorithms include:

    • Maximizing CPU utilization

    • Maximizing throughput

    • Minimizing turnaround time

    • Minimizing waiting time - best achieved by Shortest Job First (SJF)

    • Minimizing response time - best achieved by Round Robin (RR)

First-Come, First-Served (FCFS) Scheduling

  • Simplest CPU scheduling algorithm based on a First In, First Out (FIFO) queue structure.

    • Processes that request the CPU first receive allocation first.

    • Queue management involves adding a process at the tail and removing it from the head when selected.

  • Average Waiting Time in FCFS:

    • Example:

    • Given processes: P1 (24), P2 (3), P3 (3)

    • Gantt Chart produces:

      • Waiting time for P1 = 0, P2 = 24, P3 = 27

    • Average waiting time = (0 + 24 + 27) / 3 = 17

  • If arrival orders change, the waiting times may vary significantly, affecting overall efficiency.

  • FCFS is non-preemptive, meaning a running process will not be interrupted until completion or I/O request.

  • Convoy Effect: Long processes can delay smaller jobs, severely impacting system responsiveness.

Shortest-Job-First (SJF) Scheduling

  • SJF scheduling assigns the CPU to the process with the shortest CPU burst time, requiring knowledge of process burst lengths.

    • Can be non-preemptive or preemptive; preemptive case takes priority over currently running processes if a shorter job arrives.

  • Known to minimize average waiting time and is considered optimal, though practical implementation is complex due to the unpredictability of burst lengths.

  • Average Waiting Time in Example (Non-preemptive):

    • For processes P1, P2, P3, P4:

    • Gantt Chart and execution timing yield:

      • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

  • For preemptive scenarios, SJF can lead to dynamic scheduling based on process arrival times and current execution states.

Problems with SJF Scheduling

  • The major difficulty lies in predicting the length of the next CPU burst, hindering effective short-term scheduling.

    • Solutions include approximating future CPU burst lengths based on historical performance data.

Priority Scheduling

  • Each process has an associated priority: higher priority processes are scheduled before those of lower priority.

    • In cases of equal priority, FCFS rules apply.

    • The SJF can be viewed as a priority scheduling method based on the inverse of CPU burst times (shorter bursts = higher priority).

  • Communication of Problems:

    • Starvation may occur for lower priority processes; resolution involves aging, where the process priority gradually increases over time to ensure execution.

Implementation of Round Robin (RR) Scheduling

  • Designed for time-sharing systems; RR is akin to FCFS but includes preemption to switch among processes frequently.

  • Each process receives a small CPU time unit, called a time quantum (typically 10-100 milliseconds).

  • Upon time quantum expiration, the process is placed at the end of the ready queue, allowing continuous cycling among processes.

  • RR Example with a Time Quantum of 4:

    • Given processes:

    • P1 = 24, P2 = 3, P3 = 3

    • Gantt chart and calculations yield average turnaround time and waiting time metrics as crucial for understanding efficiency in timesharing systems.

Multilevel Queue Scheduling

  • Effective in scenarios where processes naturally fall into different classes with distinct scheduling requirements.

    • Example classifications include foreground (interactive) and background (batch) processes.

  • A multilevel queue system utilizes different scheduling algorithms for each queue, with permanent process assignment based on properties like priority or memory size.

    • Scheduling between these queues commonly employs fixed-priority preemptive rules to manage process execution effectively.

  • Foreground processes generally have absolute priority over background processes, ensuring responsiveness in user interactions and resource allocation.