5 CHAPTER PROCESS SCHEDULING Guy Martin OSLab GNU
5 CHAPTER PROCESS SCHEDULING Guy Martin, OSLab, GNU Fall-09
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 2
5 CHAPTER Objectives � To introduce CPU scheduling, which is the basis for multi-programmed Operating Systems � To describe various CPU-scheduling algorithms � To discuss evaluation criteria for selecting a CPU -scheduling algorithm for a particular system 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 3
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 4
5 CHAPTER Basic Concepts � CPU-I/O Burst Cycle � Processes alternate between CPU cycle and I/O wait � Process execution: CPU burst - I/O burst -… -CPU burst – I/O burst… � CPU Scheduler � Or Short-term scheduler selects a process to be run in the CPU from the ready processes queue in memory. The queue is not necessary a FIFO queue. � Preemptive Scheduling 1. 2. 3. 4. � Process switches from the running state to the waiting state Process switches from the running state to the ready state Process switches from the waiting state to the ready state The process terminates Dispatcher � Gives control of the CPU to the selected process by the short-term scheduler � Dispatch latency: time to stop a process and start another running process 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 5
5 CHAPTER Scheduling Criteria � CPU utilization � Keep the CPU as busy as possible � Throughput � Number of completed processes per time unit. Depends on processes’ length � Turnaround time � Interval from the submission time to the finish time � Waiting time � Sum of time spent by a process in the waiting queue � Response time � Time from the submission of a request until the first response is produced It is desirable to minimize CPU utilization and throughput and to maximize turnaround time, waiting time and response time 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 6
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 7
5 CHAPTER Scheduling Algorithms (1/6) � Problem: Decide which of the processes in the ready queue is to be allocated the CPU. � First-Come, First-Served Scheduling �The process that requests the CPU first is allocated the CPU first. �Can be implemented with a FIFO queue. �Non preemptive algorithm. �Disadvantage: the average waiting time can be long. Small processes can wait for long time a big process to finish 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 8
5 CHAPTER Scheduling Algorithms (2/6) � Shortest-Job-First Scheduling �Associates with each process the length of its next CPU burst �CPU is assigned to the process with the smallest next CPU burst. FCFS is used for processes with same CPU bursts �Problem: CPU burst is evaluated as an exponential average of the measured lengths of previous CPU bursts. �Provide the minimum average waiting time for a set of processes �Can be either preemptive or non preemptive 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 9
5 CHAPTER Scheduling Algorithms (3/6) � Priority Scheduling �Priority is associated with each process �CPU is assigned to the process with the highest priority. Equal priorities are scheduled with FCFS �Can be either preemptive or non preemptive �Problem: indefinite blocking or starvation. Rumor: when the IBM 7094 has been shut down in 1973 at MIT they found a process that had been submitted in 1967 and that had not yet been run �Solution: Aging �Gradually increase the priority of processes that wait for long time in the system 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 10
5 CHAPTER Scheduling Algorithms (4/6) � Round-Robin Scheduling � Designed especially for time sharing � Each process uses the CPU for a time quantum or time slice before be preempted � Problem: choose the right time slice (between 10 and 100 ms for most OS) How a smaller time quantum increases context switches 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 11
5 CHAPTER Scheduling Algorithms (5/6) � Multilevel Queue Scheduling ♦ Processes are easily classified into groups ♦ Common division for example ♦ Foreground(interactive) ♦ Background(batch) ♦ Multiple queues with different scheduling algorithms ♦ There is a scheduler among the queues 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 12
5 CHAPTER Scheduling Algorithms (6/6) � Multilevel Feedback Queue Scheduling ♦ Processes can move from one queue to another ♦ The idea: separate processes according to the characteristics of their CPU bursts. ♦ The process which uses too much CPU time will be moved to the lower priority queue. ♦ A process that waits too long in the lower-priority queue may be moved to the higher-priority queue. 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 13
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 14
5 CHAPTER Thread Scheduling(1/2) � Contention Scope �Two types of threads: user-level and kernel-level �User-level thread are mapped to kernel-level using Lightweight process (LWP) �Process contention scope (PCS): the thread library schedules user thread to run on available LWP �System contention scope (SCS): the kernel decides which kernel thread to schedule onto a CPU. 15 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin
5 CHAPTER Thread Scheduling(2/2) � Pthread 2020 -12 -04 Scheduling Operating Systems Concepts - Process Scheduling - Guy Martin 16
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 17
Multiple-Processor Scheduling(1/5) � Approaches 5 CHAPTER to Multiple-Processor Scheduling �Asymmetric multiprocessing: The master server handles all scheduling decisions, I/O processing and other system activities �Symmetric multiprocessing: each processor is selfscheduling. �All processes in a common ready queue �Two processors can choose the same process �Possible lost of processes inside queue �One private ready queue per processor 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 18
Multiple-Processor Scheduling(2/5) � Processor 5 CHAPTER Affinity �Keep a process running on the same processor to avoid high cost of invalidating and repopulating caches. We can distinguish between soft and hard affinity A process that is assigned affinity to a CPU should be allocated memory on the residence board of that CPU by the OS. 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 19
Multiple-Processor Scheduling(3/5) � Load 5 CHAPTER Balancing �Keep the workload balanced among all processors �Each processor should have its own private queue �Two approaches: �Push migration: a specific task periodically checks the load on each processor and distributes the load by pushing processes from overloaded to idle or less-busy processors. �Pull migration: an idle processor pulls a waiting task from a busy processor 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 20
Multiple-Processor Scheduling(4/5) � Multicore C Thread Processors M Compute cycle C M C Memory stall cycle M C M time a. Memory stall Thread 1 Thread 0 C C M C M C time b. Multithreaded multicore system 2020 -12 -04 5 CHAPTER C ♦ Memory stall: processors accessing the memory spent significant amount of time waiting for data to become available ♦ Implementation of multithreaded processor cores in which two or more hardware threads are assigned to each core. ♦ 2 ways to multithread a processor ♦ Corse-grained multithreading ♦ Fine-grained multithreading Operating Systems Concepts - Process Scheduling - Guy Martin 21
Multiple-Processor Scheduling(5/5) � Virtualization 5 CHAPTER and Scheduling �System with virtualization act like multiprocessor �One or more virtual CPU are presented to each of the running virtual machines. �The host OS creates and manages VMs that run guest operating system. �Virtualization will negatively impact any guest OS scheduler that assumes a certain amount of progress in a given amount of time. �Delays are introduced in the scheduling results �Delays can be more catastrophic for real-time guest OS 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 22
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 23
Operating System Examples(1/3) � Windows 5 CHAPTER XP Scheduling �Priority-based, preemptive scheduling algorithm �The dispatcher is the portion of the kernel that handles scheduling � 32 levels of priorities divided into 2 classes �The variable class: priorities from 1 to 15 �The real-time class: priorities from 16 to 31 �The memory management thread run at priority 0 �The idle thread is executed whenever there is ready thread to execute 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 24
Operating System Examples(2/3) � Linux 5 CHAPTER Scheduling �No distinction between process and thread task �Prior to version 2. 5 an old UNIX scheduler is used �Not adequate support for SMP �Not scale well as the number of tasks grows �With version 2. 5, an O(1) scheduler is introduced �Support for SMP, processor affinity, load balancing, etc. �Preemptive, priority-based with 2 priority ranges �Real-time: from 0 to 99, static priorities �Nice value: from 100 to 140, dynamic priorities 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 25
Operating System Examples(3/3) � Linux 5 CHAPTER Scheduling The kernel maintains a list of all runnable tasks in a runqueue data structure. A runqueue contains two priority arrays Active: tasks with time remaining in their time slices Expired: expired tasks Each processor maintains its own runqueue and schedules it itself When the active array is empty, the two priority arrays are exchanged; the expired array becomes active, and vice versa 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 26
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 27
5 CHAPTER Algorithm Evaluation(1/4) � Deterministic Modeling �Type of analytic evaluation: �Uses the given algorithm and the system workload to produce a formula or number that evaluates the performance of the algorithm for that workload �Takes a predetermined workload and defines the performances of each algorithm for that workload. �Simple and fast, produces numbers permitting to easily compare algorithms. �Results are applied only to the analyzed cases 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 28
5 CHAPTER Algorithm Evaluation(2/4) � Queueing Models �Just the distribution of CPU and I/O burst can be determined �Little’s formula: n = λ x W �n is the average queue length, W average waiting time in a queue, and λ average arrival rate for new processes in the queue �Particularly useful because it is valid for any scheduling algorithm and arrival distribution �Difficult to apply on complicated algorithms 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 29
5 CHAPTER Algorithm Evaluation(3/4) � Simulations Evaluate the scheduling algorithms using simulators. Trace tapes: monitor the real system and record the sequence of actual events. Simulations are expensive and trace tapes require more storage space. 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 30
5 CHAPTER Algorithm Evaluation(4/4) � Implementation �All the previous method are of limited accuracy �The completely accurate way to evaluate a scheduling algorithm is to code it, put it in the operating system, and see how it works �Difficulties �The real problem here is the high cost �The environment can change 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 31
5 CHAPTER Contents � Basic Concepts � Scheduling Criteria � Scheduling Algorithms � Thread Scheduling � Multiple-Processor Scheduling � Operating Systems Examples � Algorithm Evaluation � Summary 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 32
5 CHAPTER Summary � CPU Scheduling is the task of selecting a waiting process from the ready queue and allocating the CPU to it. The CPU is allocated to the selected process by the dispatcher. � Many algorithms are proposed: FCFS, SJF, RR… � Scheduler can be preemptive or non-preemptive � Multiprocessor scheduling must consider processor affinity, load balancing, multicore processing scheduling in virtualization systems. � Some OS provide threads at the kernel level and mechanisms to schedule them. � We need methods to select among the wide variety of the existing algorithm. 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 33
© Modern Operating Systems - Andrew S. Tanenbaum 2020 -12 -04 Operating Systems Concepts - Process Scheduling - Guy Martin 34
- Slides: 34