Chapter 5 Process Scheduling April 2015 Jongmoo Choi

  • Slides: 33
Download presentation
Chapter 5. Process Scheduling April, 2015 Jongmoo Choi Dept. of software Dankook University http:

Chapter 5. Process Scheduling April, 2015 Jongmoo Choi Dept. of software Dankook University http: //embedded. dankook. ac. kr/~choijm J. Choi, DKU

Contents 5. 1 Basic Concepts 5. 2 Scheduling Criteria 5. 3 Scheduling Algorithms 5.

Contents 5. 1 Basic Concepts 5. 2 Scheduling Criteria 5. 3 Scheduling Algorithms 5. 4 Thread Scheduling 5. 5 Multiple-Processor Scheduling 5. 6 Real-Time CPU Scheduling 5. 7 Operating Systems Examples 5. 8 Algorithm Evaluation 5. 9 Summary 2 J. Choi, DKU

Chapter Objectives To introduce CPU-scheduling, which is the basis for multiprogrammed operating systems To

Chapter Objectives To introduce CPU-scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system To examine the scheduling algorithms of several operating systems 3 J. Choi, DKU

5. 1 Basic Concepts Process execution ü CPU burst and I/O burst § A

5. 1 Basic Concepts Process execution ü CPU burst and I/O burst § A cycle of CPU execution (CPU Cycle) and I/O wait (I/O Cycle) § CPU-bound (or intensive) process vs. I/O-bound process ü A large # of short CPU bursts (a small # of long CPU bursts) § To improve CPU utilization • Execute other processes during I/O bursts Need CPU scheduling (Alternating sequence of bursts) (Histogram of CPU-burst durations) 4 J. Choi, DKU

5. 1 Basic Concepts CPU scheduler ü Short-term scheduler (from the terms defined in

5. 1 Basic Concepts CPU scheduler ü Short-term scheduler (from the terms defined in Chapter 3) ü CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting(or new) to ready state 4. Switches from running to terminated state ü ü Non-preemptive (or cooperative) scheduling: when scheduling takes place under circumstance 1 and 4 only. Preemptive scheduling: when scheduling takes place under the all circumstances (1 ~ 4). 5 J. Choi, DKU

5. 1 Basic Concepts Dispatcher ü ü One component involved in the CPU-scheduling facility

5. 1 Basic Concepts Dispatcher ü ü One component involved in the CPU-scheduling facility Give control of the CPU to the selected process § Switching context § Switching to user mode § Jumping to the proper location in the user program to restart that program ü Need to reduce the dispatch latency (context switch overhead) 6 J. Choi, DKU

5. 2 Scheduling Criteria ü ü ü CPU utilization: the proportion of time that

5. 2 Scheduling Criteria ü ü ü CPU utilization: the proportion of time that CPU is busy Throughput: the # of processes that complete their execution per time unit (system perspective) Turnaround time: the interval from the time of submission of a process to the time of completion (user perspective) Waiting time: the amount of time a process has been waiting in the ready queue (scheduler perspective) Response time: the time from the submission of a request until the first response is produced (interactive perspective) Issues ü ü Various tradeoffs (throughput vs. response time) Average value (efficient) vs. Variance (predictable) 7 J. Choi, DKU

5. 3 Scheduling Algorithms Representative policies ü FCFS (First Come, First Served) § Choose

5. 3 Scheduling Algorithms Representative policies ü FCFS (First Come, First Served) § Choose first coming process § Non-preemptive ü Round-Robin (RR) § FCFS with Preemptive (at time quantum) ü Shortest Job First (Shortest Process Next: SPN) § Choose shortest process § Non-preemptive ü Shortest Remaining Time First (SRT) § SPN with Preemptive (at arrival time) ü Highest Response Ratio Next (HRRN) § Response ratio = (waiting time + service time) / service time § Non-preemptive ü Multilevel Feedback Queues § Multiple queues with different priority § Preemptive (at time quantum) (Example of MFQ: RR can be used instead of FCFS) 8 J. Choi, DKU

5. 3 Scheduling Algorithms Scheduling example: process set (Source: “Operating systems: Internals and Design

5. 3 Scheduling Algorithms Scheduling example: process set (Source: “Operating systems: Internals and Design Principle” by W. Stalling) J. Choi, DKU

5. 3 Scheduling Algorithms Scheduling example: execution order (Source: “Operating systems: Internals and Design

5. 3 Scheduling Algorithms Scheduling example: execution order (Source: “Operating systems: Internals and Design Principle” by W. Stalling) J. Choi, DKU

5. 3 Scheduling Algorithms Tradeoffs ü FCFS § Simple but the average waiting time

5. 3 Scheduling Algorithms Tradeoffs ü FCFS § Simple but the average waiting time is often quite long • short process behind long process (convoy effect) § Case 1: P 1 (24), P 2 (3), P 3(3) • /* Pid (service time) */ average waiting time = (0+24+27)/3 § Case 2: P 2 (3), P 3(3), P 1 (24) • /* Pid (service time) */ average waiting time = (6+0+3)/3 11 J. Choi, DKU

5. 3 Scheduling Algorithms Tradeoffs ü SJF (also SRT) § Give the minimum average

5. 3 Scheduling Algorithms Tradeoffs ü SJF (also SRT) § Give the minimum average waiting time (provably optimal) § Difficult to know the length of the next CPU request § May cause starvation need to consider waiting time § Example for SJF: P 1(6), P 2(8), P 3(7), P 4(3) • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 § Example for SRT: P 1(0, 8), P 2(1, 4), P 3(2, 9), P 4(3, 5) /* pid (arrival time, service time) • Average waiting time = (9 + 10 + 15+ 2) / 4 = 26/4 = 6. 5 • 12 J. Choi, DKU

5. 3 Scheduling Algorithms Tradeoffs ü RR § Suitable for time sharing system (interactive

5. 3 Scheduling Algorithms Tradeoffs ü RR § Suitable for time sharing system (interactive applications) § Eg. P 1 (24), P 2 (3), P 3(3) with time quantum 4 • ü Compared with SJF: better response time (as the time quantum decrease) but higher average turnaround time due to context switch overhead MFQ § Give less chance to CPU-bound processes (prefer short CPU burst jobs) § Can differentiate btw foreground (interactive) and background (batch) § Issues: 1) # of queues, 2) scheduling algorithms for each queue, 3) demote policy, 4) upgrade policy, 5) initialization policy, . . § cf) Multilevel queue scheduling: multiple queues with different policies (usually no moving btw queues) 13 J. Choi, DKU

5. 3 Scheduling Algorithms Issues ü How to predict the service time (the length

5. 3 Scheduling Algorithms Issues ü How to predict the service time (the length of the next CPU burst)? § By user specification § By prediction (approximation) • The next CPU burst will be similar in length to the previous ones (characteristics of program behavior) exponential moving average where • Validation with =0. 5 and 0=10 ( determines the weight of each history) 14 J. Choi, DKU

5. 3 Scheduling Algorithms Issues ü The performance of the RR scheduling algorithm heavily

5. 3 Scheduling Algorithms Issues ü The performance of the RR scheduling algorithm heavily depends on the time quantum § Too large: degenerate FCFS may degrade response time § Too small: incur considerable context switch overhead § Rule of thumb: 80% of the CPU bursts should be shorter than the time quantum (usually 10 to 200 ms in Linux) (Smaller time quantum increases context switch) (Turnaround time varies with the time quantum, Note we assume there is no context switch overhead) 15 J. Choi, DKU

5. 3 Scheduling Algorithms Priority scheduling ü ü A priority number (integer) is associated

5. 3 Scheduling Algorithms Priority scheduling ü ü A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) SJF is priority scheduling where priority is the inverse of predicted next CPU burst time Two categories § Preemptive § Nonpreemptive ü ü Problem: Starvation – low priority processes may never execute Solution: Aging – as time progresses increase the priority of the process (eg. HRRN) 16 J. Choi, DKU

5. 4 Thread Scheduling Thread model ü Kernel thread § Scheduled by the CPU

5. 4 Thread Scheduling Thread model ü Kernel thread § Scheduled by the CPU scheduler in kernel (actual CPU allocation) ü User thread § Many-to-one: scheduled by the User-level scheduler in a library (kernel is unaware of them), PCS (Process Contention Scope) § One-to-one: scheduled by the CPU scheduler in kernel, SCS (System Contention Scope) time K 1 K 2 K 3 U 1 U 2 U 1 CPU scheduler (thread sets: K 1, K 2, K 3 and U 1, U 2 on K 3) … K 1 (thread scheduling scenario) User level scheduler A user thread can be the running state while its associated kernel thread is the ready state (or block) and vice versa. 17 J. Choi, DKU

5. 4 Thread Scheduling (Optional) Pthread example ü ü PTHREAD_SCOPE_PROCESS or SYSTEM Linux and

5. 4 Thread Scheduling (Optional) Pthread example ü ü PTHREAD_SCOPE_PROCESS or SYSTEM Linux and Max OX support PTHREAD_SCOPE_SYSTEM only #include <pthread. h> #include <stdio. h> #define NUM_THREADS 5 int main(int argc, char *argv[]) { int i, scope; pthread_t tid[NUM THREADS]; pthread_attr_t attr; /* get the default attributes */ pthread_attr_init(&attr); /* first inquire on the current scope */ if (pthread_attr_getscope(&attr, &scope) != 0) fprintf(stderr, "Unable to get scheduling scopen"); else { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS"); else if (scope == PTHREAD_SCOPE_SYSTEM) printf("PTHREAD_SCOPE_SYSTEM"); else fprintf(stderr, "Illegal scope value. n"); } 18 J. Choi, DKU

5. 4 Thread Scheduling (Optional) Pthread example (cont’) /* set the scheduling algorithm to

5. 4 Thread Scheduling (Optional) Pthread example (cont’) /* set the scheduling algorithm to PCS or SCS */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* create threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], &attr, runner, NULL); /* now 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); } 19 J. Choi, DKU

5. 5 Multiple-Processor Scheduling Issues ü ü ü AMP vs. SMP Local queue vs.

5. 5 Multiple-Processor Scheduling Issues ü ü ü AMP vs. SMP Local queue vs. Global queue vs. hybrid Load balancing § Process migration to fully utilize multiple processors ü (NUMA architecture) Processor affinity § Attempt to keep a process running on the same processor for making use of cache contents § NUMA: computing follows memory or memory follows computing 20 J. Choi, DKU

5. 5 Multiple-Processor Scheduling (Optional) Issues ü Multithreaded processor (usually multicore) § Intel’s hyperthread,

5. 5 Multiple-Processor Scheduling (Optional) Issues ü Multithreaded processor (usually multicore) § Intel’s hyperthread, Ultra. SPARC’s hardware thread § To remedy the memory stall (Multithreaded processor) (50% CPU utilization due to memory stall) ü Virtualization and Scheduling § Present one or more virtual CPUs to each of the virtual machine § Using CPU scheduling: virtual CPUs on physical CPU § Need to consider the difference btw physical and virtual CPU time 21 J. Choi, DKU

5. 6 Real-Time CPU Scheduling Real-time system ü ü Require to meet deadline Hard

5. 6 Real-Time CPU Scheduling Real-time system ü ü Require to meet deadline Hard RT system vs. Soft RT system: absolute guarantee vs probabilistic guarantee Real-time scheduling ü RM (Rate-Monotonic) scheduling § Algorithm for periodic tasks: task (t, d, p) where t is execution time, d is deadline and p is period (t <= d <= p) § Scheduling larger rate first where rate = 1/p (preemptive, static priority) • P 1 (20, 50), P 2 (35, 100) 22 J. Choi, DKU

5. 6 Real-Time CPU Scheduling Real-time scheduling ü EDF (Earliest Deadline First) scheduling §

5. 6 Real-Time CPU Scheduling Real-time scheduling ü EDF (Earliest Deadline First) scheduling § The earlier the deadline, the higher the priority § Preemptive, dynamic priority(for both periodic and non-periodic) • • P 1 (25, 50), P 2 (35, 80) On the contrary, RM misses deadline for the same workloads (There exists the upper bound about CPU utilization according to the # of tasks)) 23 J. Choi, DKU

5. 6 Real-Time CPU Scheduling Real-time scheduling ü Proportional share scheduling § § §

5. 6 Real-Time CPU Scheduling Real-time scheduling ü Proportional share scheduling § § § ü For soft RT system (Qo. S supporting) T shares are allocated among all processes in the system An application receives N shares where N < T Ensures each application will receive N / T of the total processor time Example: Lottery scheduling, fair-share scheduling POSIX RT Scheduling § The POSIX. 1 b standard § API provides functions for managing real-time threads § 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 24 J. Choi, DKU

5. 6 Real-Time CPU Scheduling (Optional) Real-time system consideration ü Event response time §

5. 6 Real-Time CPU Scheduling (Optional) Real-time system consideration ü Event response time § Eg) radioactive event, lane detector, hovering, … ü Need to optimize interrupt and dispatch latency (WCET) § Interrupt: 1) interrupt latency, 2) interrupt processing § Conflict: 1) preemption of any process at the event triggered time, 2) release by low-priority process of resources needed by RT processes § Dispatch: context save and restore 25 J. Choi, DKU

5. 7 Operating System Examples Solaris Scheduling Example ü ü Priority-based preemptive scheduling Each

5. 7 Operating System Examples Solaris Scheduling Example ü ü Priority-based preemptive scheduling Each thread belongs to one of six priority classes § TS(default), IA, RT, SYS, FSS, FP (FX in the figure) § Priorities ordering: RT > SYS > TS/IA/FP/FSS § Each class uses different scheduling algorithms ü TS § Default class, priority ranging from 0 (lowest) to 59 (highest) § Assigns time slices of different lengths based on priority (the higher the priority, the smaller the time slice) § Dynamically alters priorities (new priority after timeout or wakeup) MFQ 26 J. Choi, DKU

5. 7 Operating System Examples Solaris Scheduling Example ü IA § For Windowing applications

5. 7 Operating System Examples Solaris Scheduling Example ü IA § For Windowing applications (KDE, GNOME) § Use the same scheduling mechanisms as TS § Interactive processes typically have a higher priority (since they are mainly returned from sleep) good response time § CPU-bound processes have a lower priority, having lengthy time slice good throughput ü RT § Highest priority for supporting a guaranteed response time (deadline) ü SYS § Scheduler, paging daemon, … ü FP and FSS § Introduced with Solaris 9, Same priority range as TS § FP’s priority are not dynamically adjusted § FSS uses share (credit) instead of priority 27 J. Choi, DKU

5. 7 Operating System Examples (Optional) Windows XP Scheduling Example ü Priority-based preemptive scheduling

5. 7 Operating System Examples (Optional) Windows XP Scheduling Example ü Priority-based preemptive scheduling § Priority range: 0~31 § Priority class: Real-time, High, (Above/below) Normal, Idle • The class is specified when the process is created (default: normal) § A process within a given priority classes also has a relative priority: timecritical, highest, (above/below) normal, lowest, idle • Default: normal, Lower if a thread is timeout, Boost if a thread is waked up. § Give more quantum to the foreground processes (typically factor of 3) § Windows 7 added UMS (User-mode Scheduling) 28 J. Choi, DKU

5. 7 Operating System Examples (Optional) Linux Scheduling Example ü Priority-based preemptive scheduling §

5. 7 Operating System Examples (Optional) Linux Scheduling Example ü Priority-based preemptive scheduling § Priority range: 0~99 (real time task), 100~140 (normal task, time-sharing) • • • Note that numerically lower values indicate higher priorities Unlike Solaris and Windows, Linux assigns longer time quantum to higherpriority tasks Dynamic adjustments based on interactivity (higher priority for interactive task) § O(1) algorithm: Active array and expired array • Active array contains all tasks with remaining time in their time quantum § CFS (Completely Fair Scheduler) • time slice depends on weight, instead of priority (for enhance response time) § SMP supports: processor affinity and load balancing § Real time tasks: FCFS or RR (POSIX. 1 b) 29 J. Choi, DKU

5. 7 Operating System Examples (Optional) Detail of CFS (Complete Fair Scheduler) Queue ü

5. 7 Operating System Examples (Optional) Detail of CFS (Complete Fair Scheduler) Queue ü Introduce virtual runtime for scheduling § Each task has weight based on priority (roughly 1. 5 times) § Virtual runtime = execute time * (WEIGHT_NICE_0 / task’s weight) • • More execution, larger virtual runtime (decay factor) More weight, smaller virtual runtime (priority factor) § Select a next task with the smallest virtual time ü Use red-black tree to manage tasks in run queue § Key = virtual runtime. § Choose leftmost node (minimum virtual time) § No multiple queue, (no active queue, expired queue) 30 J. Choi, DKU

5. 8 Algorithm Evaluation How to select a CPU-scheduling algorithm for a particular system?

5. 8 Algorithm Evaluation How to select a CPU-scheduling algorithm for a particular system? ü Analytic models: deterministic evaluation § Take a particular predetermined workload analyze the performance of each algorithm for that workload (see page 9 and 10) ü Queueing models: mathematical evaluation § Process creation is usually a random variable, not fixed (distribution) § Based on arrival and service rate, we can estimate utilization, average queue length, average waiting time and so on (eg. Little’s law) ü ü Simulation: programming a model. executing it with real traces. Implementation: materialize as a real system § Most accurate, but most expensive (time and effort) 31 J. Choi, DKU

5. 8 Algorithm Evaluation Example of Deterministic modeling ü Workload assumption ü Among FCFS,

5. 8 Algorithm Evaluation Example of Deterministic modeling ü Workload assumption ü Among FCFS, SJF and RR (q=10), which algorithm would give the minimum average waiting time? § FCFS § SJF § RR 32 J. Choi, DKU

5. 9 Summary CPU scheduling ü ü ü Definition: select a process from the

5. 9 Summary CPU scheduling ü ü ü Definition: select a process from the ready queue Two category: preemptive, non-preemptive Policies: FCFS, SJF, RR, MFQ, Real-time, … Multiprocessor support ü ü Processor affinity Load balancing Operating systems example Algorithm evaluation Homework 3: Make a C program that displays the output shown in page 10. I. Requirement: 1. Report should contain program, execution snapshot and discussion 2. The snapshot must include the ID (or IDs of the team)! 3. The discussion must include the role of each member for this project II. Deadline: The same day in the next week III. Bonus: Algorithm evaluation with various workloads (different set of processes) 33 J. Choi, DKU