CPU Scheduling Algorithms and Evaluation Criteria: A Comprehensive Guide, Study notes of Material Engineering

A comprehensive overview of cpu scheduling algorithms, covering their principles, implementation, and evaluation criteria. It delves into various scheduling algorithms, including first-come, first-served (fcfs), shortest job first (sjf), round robin (rr), priority scheduling, multilevel queue scheduling, and real-time scheduling. The document also explores the concept of load balancing on multicore systems and the role of real-time scheduling in operating systems. It concludes with a discussion of linux scheduling and its implementation of the completely fair scheduler (cfs).

Typology: Study notes

2023/2024

Uploaded on 10/24/2024

hugger
hugger 🇺🇸

4.8

(12)

916 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPU Scheduling Algorithms and
Techniques
Operating System
Basic Concepts
Almost all computer resources are scheduled before use.
Maximum CPU utilization is obtained with multiprogramming.
CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU
execution and I/O wait.
CPU burst followed by I/O burst.
CPU burst distribution is of main concern.
Histogram of CPU-burst Times
The frequency curve generally shows:
A large number of short bursts.
A small number of longer bursts.
CPU Scheduler
The CPU scheduler selects one process from among the processes in
the ready queue, and allocates the CPU core to it.
The ready queue may be ordered in various ways: FIFO, priority, tree,
linked list.
CPU scheduling decisions may take place when a process:
Switches from running to waiting state.
Switches from running to ready state.
Switches from waiting to ready.
Terminates.
Scheduling under 1 and 4 is nonpreemptive, while all other scheduling
is preemptive and can result in race conditions.
Dispatcher
The dispatcher module gives control of the CPU to the process selected
by the short-term scheduler.
This involves:
Switching context.
Switching to user mode.
Jumping to the proper location in the user program to resume that
program.
Dispatch latency is the time it takes for the dispatcher to stop one
process and start another running.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CPU Scheduling Algorithms and Evaluation Criteria: A Comprehensive Guide and more Study notes Material Engineering in PDF only on Docsity!

CPU Scheduling Algorithms and

Techniques

Operating System

Basic Concepts

Almost all computer resources are scheduled before use. Maximum CPU utilization is obtained with multiprogramming. CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait. CPU burst followed by I/O burst. CPU burst distribution is of main concern.

Histogram of CPU-burst Times

The frequency curve generally shows: A large number of short bursts. A small number of longer bursts.

CPU Scheduler

The CPU scheduler selects one process from among the processes in the ready queue, and allocates the CPU core to it. The ready queue may be ordered in various ways: FIFO, priority, tree, linked list. CPU scheduling decisions may take place when a process: Switches from running to waiting state. Switches from running to ready state. Switches from waiting to ready. Terminates. Scheduling under 1 and 4 is nonpreemptive, while all other scheduling is preemptive and can result in race conditions.

Dispatcher

The dispatcher module gives control of the CPU to the process selected by the short-term scheduler. This involves: Switching context. Switching to user mode. Jumping to the proper location in the user program to resume that program. Dispatch latency is the time it takes for the dispatcher to stop one process and start another running.

Scheduling Criteria

CPU utilization – keep the CPU as busy as possible. Throughput – number of processes that complete their execution per time unit. Turnaround time – amount of time to execute a particular process. Waiting time – amount of time a process spends waiting in the ready queue. Response time – amount of time it takes from when a request was submitted until the first response is produced, not outputting the response (for time-sharing environment or in an interactive system).

Scheduling Algorithm Optimization Criteria

Maximize CPU utilization. Maximize Throughput. Minimize Turnaround time. Minimize Waiting time. Minimize Response time. In most cases, it is necessary to optimize the average measure. For interactive systems, it is more important to minimize the variance in the response time.

First-Come, First-Served (FCFS) Scheduling

Motivation: for simplicity, consider FIFO-like policy. Example: Suppose the processes arrive at time 0 in the order: P1, P2, P3. The Gantt Chart for the schedule is: Waiting time for P1 = 0, P2 = 24, P3 = 27. Average waiting time = (0 + 24 + 27)/3 = 17. Suppose the processes arrive in the order: P2, P3, P1. The Gantt chart for the schedule is: Waiting time for P1 = 6, P2 = 0, P3 = 3. Average waiting time = (6 + 0 + 3)/3 = 3. Convoy effect – short processes behind a long process, all the other processes wait for the one big process to get off the CPU.

Shortest-Job-First (SJF) Scheduling

Motivation: Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process. The shortest-next-CPU-burst algorithm: Associate with each process the length of its next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. FCFS scheduling is used if the next CPU bursts of two processes are the same. SJF is provably optimal – gives minimum average waiting time for a given set of processes.

If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. Timer interrupts every quantum to schedule the next process. Performance: q large → FIFO q small → q must be large with respect to context switch, otherwise overhead is too high

Example of RR with Time Quantum = 4

Process | Burst Time P1 | 24 P2 | 3 P3 | 3 The Gantt chart is: P1 | P2 | P3 | P1 | P1 | P Average waiting time =? Typically, higher average turnaround than SJF, but better response. q should be large compared to context switch time (q usually 10ms to 100ms, context switch < 10μsec).

Time Quantum and Context Switch Time

80% of CPU bursts should be shorter than q.

Turnaround Time Varies With The Time

Quantum

The turnaround time varies with the time quantum.

Priority Scheduling

Motivation: A priority number (integer) is associated with each process. The CPU is allocated to the process with the highest priority (smallest integer = highest priority). Equal-priority processes are scheduled in FCFS or RR. Preemptive and non-preemptive versions exist. SJF is priority scheduling where priority is the inverse of predicted next CPU burst time. Problem: Starvation – low priority processes may never execute. Solution: Aging – as time progresses, increase the priority of the process.

Example of Priority Scheduling

Process | Arrival Time | Burst Time | Priority P1 | - | 10 | 3 P2 | - | 1 | 1

P3 | - | 2 | 4

P4 | - | 1 | 5

P5 | - | 5 | 2

Priority scheduling Gantt Chart Average waiting time = 8.2 ms

Priority Scheduling with Round-Robin

Run the process with the highest priority. Processes with the same priority run round-robin. Example: Process | Arrival Time | Burst Time | Priority P1 | 4 | 3 | 3 P2 | 5 | 2 | 2 P3 | 8 | 2 | 2 P4 | 7 | 1 | 1 P5 | 3 | 3 | 3 Gantt Chart with 2 ms time quantum Average waiting time =?

Multilevel Queue

Motivation: With priority scheduling, have separate queues for each priority. Schedule the process in the highest-priority queue.

Example of Multilevel Queue

Prioritization based upon process type.

Multilevel Feedback Queue

Motivation: A process can move between the various queues; aging can be implemented this way. Multilevel-feedback-queue scheduler defined by the following parameters: Number of queues Scheduling algorithms for each queue Method used to determine when to upgrade a process Method used to determine when to demote a process Method used to determine which queue a process will enter when that process needs service This scheme leaves I/O-bound and interactive processes—which are typically characterized by short CPU bursts — in the higher-priority queues and a process that waits too long in a lower-priority queue may be moved to a higher-priority queue.

Symmetric Multiprocessing (SMP)

Each processor is self-scheduling. Two possible strategies: All threads may be in a common ready queue. Each processor may have its own private queue of threads.

Multicore Processors

Recent trend to place multiple processor cores on the same physical chip. Faster and consumes less power. Multiple threads per core also growing, taking advantage of memory stall to make progress on another thread while memory retrieve happens.

Multithreaded Multicore System

Each core has more than 1 hardware thread. If one thread has a memory stall, switch to another thread. Two levels of scheduling: The operating system deciding which software thread to run on a logical CPU. How each core decides which hardware thread to run on the physical core.

Multiple-Processor Scheduling – Load

Balancing

If SMP, need to keep all CPUs loaded for efficiency. Load balancing attempts to keep workload evenly distributed. Push migration – periodic task checks load on each processor, and if found pushes task from overloaded CPU to other CPUs. Pull migration – idle processors pulls waiting task from busy processor.

Multiple-Processor Scheduling – Processor

Affinity

When a thread has been running on one processor, the cache contents of that processor stores the memory accesses by that thread. Load balancing may affect processor affinity as a thread may be moved from one processor to another to balance loads, yet that thread loses the contents of what it had in the cache of the processor it was moved off of. Soft affinity – the operating system attempts to keep a thread running on the same processor, but no guarantees. Hard affinity – allows a process to specify a set of processors it may run on.

NUMA and CPU Scheduling

If the operating system is NUMA-aware, it will assign memory close to the CPU the thread is running on.

CPU

The CPU (Central Processing Unit) is a crucial component of a computer system that performs various computational tasks. It is responsible for executing instructions and processing data. The CPU can access memory in two different ways:

Fast Access

The CPU can quickly access and retrieve data from the memory. This fast access allows the CPU to efficiently execute instructions and process information.

Slow Access

In contrast, the CPU can also access memory in a slower manner. This slow access to memory can impact the overall performance of the computer system.

Memory

The memory of a computer system is where data and instructions are stored. The CPU can access the memory in both fast and slow ways, depending on the type of memory and its proximity to the CPU.

Computer

A computer is a complex electronic device that can perform a wide range of tasks, from simple calculations to advanced data processing and analysis. The computer's performance is heavily dependent on the interaction between the CPU and the memory.

Real-Time CPU Scheduling

Real-time CPU scheduling is a critical aspect of operating systems, as it deals with the scheduling of tasks that have specific timing requirements.

Soft Real-Time Systems

In soft real-time systems, critical real-time tasks have the highest priority, but there is no guarantee as to when these tasks will be scheduled. This means that while these tasks are given priority, their execution is not strictly bound by deadlines.

For hard real-time systems, the scheduler must also provide the ability to meet deadlines. Processes in real-time systems have new characteristics, such as periodic tasks that require CPU access at constant intervals.

Rate Monotonic Scheduling

Rate Monotonic Scheduling is a priority-based scheduling algorithm where a higher priority is assigned to tasks with shorter periods. This means that tasks with shorter periods (higher rates) are given higher priority than tasks with longer periods (lower rates).

Missed Deadlines with Rate Monotonic

Scheduling

Process P2 misses finishing its deadline at time 80 in the given scenario.

Earliest Deadline First Scheduling (EDF)

Priorities are assigned according to deadlines: The earlier the deadline, the higher the priority. The later the deadline, the lower the priority.

Proportional Share Scheduling

T shares are allocated among all processes in the system. An application receives N shares where N < T. This ensures each application will receive N / T of the total processor time.

POSIX Real-Time Scheduling

The POSIX.1b standard provides an API for managing real-time threads. It defines two scheduling classes for real-time threads: SCHED_FIFO - Threads are scheduled using a FCFS strategy with a FIFO queue. There is no time-slicing for threads of equal priority. SCHED_RR - Similar to SCHED_FIFO, except time-slicing occurs for threads of equal priority. The API includes functions for getting and setting the scheduling policy: pthread_attr_getsched_policy(pthread_attr_t *attr, int *policy) pthread_attr_setsched_policy(pthread_attr_t *attr, int policy)

Real-Time Scheduling API

 ## include ## include ## define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, policy; pthread_t tid[NUM_THREADS]; pthread_attr_t attr; // Get the default attributes pthread_attr_init(&attr); // Get the current scheduling policy if (pthread_attr_getschedpolicy(&attr, &policy) != 0) fprintf(stderr, 'Unable to get policy.\n'); else { if (policy == SCHED_OTHER) printf('SCHED_OTHER\n'); else if (policy == SCHED_RR) printf('SCHED_RR\n'); else if (policy == SCHED_FIFO) printf('SCHED_FIFO\n'); } // Set the scheduling policy - FIFO, RR, or OTHER if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) fprintf(stderr, 'Unable to set policy.\n'); // Create the threads for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], &attr, runner, NULL); // Join on each thread for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } // Each thread will begin control in this function void _runner(void_ param) { // Do some work... pthread_exit(0); } ``` ## Operating System Examples Linux scheduling Windows scheduling Solaris scheduling Real-time tasks have static priorities. Real-time plus normal map into a global priority scheme. Nice value of -20 maps to global priority 100. Nice value of +19 maps to priority 139. ## Linux Scheduling (Cont.) Linux supports load balancing, but is also NUMA-aware. Scheduling domain is a set of CPU cores that can be balanced against one another. Domains are organized by what they share (i.e., cache memory). The goal is to keep threads from migrating between domains. ## Windows Scheduling Windows uses priority-based preemptive scheduling. Highest-priority thread runs next. Dispatcher is the scheduler. Thread runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority thread. Real-time threads can preempt non-real-time. 32-level priority scheme: Variable class is 1-15, real-time class is 16-31. Priority 0 is the memory-management thread. Queue for each priority. If no runnable thread, runs the idle thread. ## Windows Priority Classes Win32 API identifies several priority classes to which a process can belong: REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS. All are variable except REALTIME. A thread within a given priority class has a relative priority: TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST, IDLE. Priority class and relative priority combine to give a numeric priority. Base priority is NORMAL within the class. If the quantum expires, priority is lowered, but never below the base. If a wait occurs, priority is boosted depending on what was waited for. Foreground window is given a 3x priority boost. Windows 7 added user-mode scheduling (UMS), where applications create and manage threads independently of the kernel, which is much more efficient for a large number of threads. ## Solaris Scheduling Solaris uses priority-based scheduling. Six scheduling classes are available: Time sharing (default) (TS), Interactive (IA), Real time (RT), System (SYS), Fair Share (FSS), and Fixed priority (FP). A given thread can be in only one class at a time. Each class has its own scheduling algorithm. Time sharing is a multi-level feedback queue, with a loadable table configurable by the system administrator. ## Solaris Dispatch Table The Solaris dispatch table shows the mapping of class-specific priorities to global priorities. ## Solaris Scheduling (Cont.) The scheduler converts class-specific priorities into a per-thread global priority. The thread with the highest priority runs next. A thread runs until (1) it blocks, (2) it uses its time slice, or (3) it is preempted by a higher-priority thread. Multiple threads at the same priority are selected via round-robin. **Algorithm Evaluation** How to select a CPU-scheduling algorithm for an operating system? Determine the criteria, then evaluate the algorithms. ## Deterministic Modeling A type of analytic evaluation. Takes a particular predetermined workload and defines the performance of each algorithm for that workload. ## Deterministic Evaluation For each algorithm, calculate the minimum average waiting time. Simple and fast, but requires exact numbers for input and applies only to those inputs. Examples: FCFS is 28ms. Non-preemptive SJF is 13ms. RR is 23ms. ## Queueing Models Describes the arrival of processes and CPU and I/O bursts probabilistically, commonly using exponential distributions. Computes average throughput, utilization, waiting time, and other metrics. The computer system is described as a network of servers, each with a queue of waiting processes. must voluntarily relinquish control of the CPU). Almost all modern operating systems are preemptive. **Scheduling Algorithms and Evaluation** **Criteria** ## Evaluation Criteria for Scheduling Algorithms Scheduling algorithms can be evaluated according to the following five criteria: 1. CPU utilization 2. Throughput 3. Turnaround time 4. Waiting time 5. Response time ## First-Come, First-Served (FCFS) Scheduling FCFS scheduling is the simplest scheduling algorithm, but it can cause short processes to wait for very long processes. ## Shortest-Job-First (SJF) Scheduling SJF scheduling is provably optimal, providing the shortest average waiting time. However, implementing SJF scheduling is difficult because predicting the length of the next CPU burst is challenging. ## Round-Robin (RR) Scheduling RR scheduling allocates the CPU to each process for a time quantum. If the process does not relinquish the CPU before its time quantum expires, the process is preempted, and another process is scheduled to run for a time quantum. ## Priority Scheduling Priority scheduling assigns each process a priority, and the CPU is allocated to the process with the highest priority. Processes with the same priority can be scheduled in FCFS order or using RR scheduling. ## Multilevel Queue Scheduling Multilevel queue scheduling partitions processes into several separate queues arranged by priority, and the scheduler executes the processes in the highest-priority queue. ## Multilevel Feedback Queues Multilevel feedback queues are similar to multilevel queues, except that a process may migrate between different queues. ## Multicore Processors Multicore processors place one or more CPUs on the same physical chip, and each CPU may have more than one hardware thread. From the perspective of the operating system, each hardware thread appears to be a logical CPU. ## Load Balancing on Multicore Systems Load balancing on multicore systems equalizes loads between CPU cores, although migrating threads between cores to balance loads may invalidate cache contents and therefore may increase memory access times. ## Real-Time Scheduling Soft real-time scheduling gives priority to real-time tasks over non-real-time tasks. Hard real-time scheduling provides timing guarantees for real-time tasks. ## Rate-Monotonic Real-Time Scheduling Rate-monotonic real-time scheduling schedules periodic tasks using a static priority policy with preemption. ## Proportional Share Scheduling Proportional share scheduling allocates T shares among all applications. If an application is allocated N shares of time, it is ensured of having N/T of the total processor time. ## Linux Scheduling Linux uses the completely fair scheduler (CFS), which assigns a proportion of CPU processing time to each task. ## Windows Scheduling Windows scheduling uses a preemptive, 32-level priority scheme to determine the order of thread scheduling. CPU-intensive threads are generally assigned lower priorities (and longer time quantums), and I/O- bound threads are usually assigned higher priorities (with shorter time quantums). ## Modeling and Simulations Modeling and simulations can be used to evaluate a CPU scheduling algorithm.