Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating Systems Dr. Jerry Shiao, Silicon Valley University Fall 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 CPU Scheduling Overview CPU Scheduling Basic Concepts Scheduling Criteria User-Level Threads and Kernel-Level Threads Multiple-Processor Scheduling First-Come-First-Serve Shortest-Job-First Priority Round Robin Multilevel Queue Scheduling Thread Scheduling Comparing CPU Scheduling Algorithms Scheduling Algorithms Scheduler in a Multi-Programming Environment. Maximize CPU Utilization. Concept of CPU Bursts and I/O Bursts. Processor Afinitity Load Balancing Multicore Processors Virtualization Computer System Schedulers Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 2 CPU Scheduling Objective of Multiprogramming Maximize CPU Utilization: To have some process running at all times. Several processes kept in memory. Alternating sequence of CPU execution – I/O Wait … . . . Load Store . . . Wait for I/O . . . Store Inc . . . Wait for I/O . . . Add Store Index Read From File Write to File CPU Burst 1-2 MSec CPU Burst 1-2 MSec I/O Bound Process has many short CPU bursts. CPU Bound Process has few long CPU bursts. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 3 CPU Scheduling CPU-Burst Duration I/O Bound Process has many short CPU bursts. CPU Bound Process has few long CPU bursts. Burst distribution important for CPU-Scheduling Algorithm. Curve showing large number of short burst (Time-Sharing processes). A smaller number of long CPU bursts (Batch processes). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 4 CPU Scheduling CPU Scheduler Scheduler manages queues to minimize queuing delay and to optimize performance in a queuing environment. Types of Scheduling: Long-Term Scheduling Medium-Term Scheduling Decision to Swap in a process, manage degree of Multiprogramming. Short-Term Scheduling Decision on which programs are admitted to the system for processing, controlling the degree of Multiprogramming. Long-Term Scheduler may limit the degree of Multiprogramming to provide satisfactory service to current set of processes. Dispatcher, makes decision on which process to execute next. Invoked whenever event can lead to blocking of current process or preempt the current running process: Clock Interrupts, I/O Interrupts, System Calls, Signals (Semphores). I/O Scheduling Decision as to which process pending I/O request shall be handled by the available I/O Device. Copyright @ 2009 Pearson Education, Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 5 CPU Scheduling CPU Scheduler Scheduling and Process State Transition Scheduling types and relation to Process State Transition. Copyright @ 2009 Pearson Education, Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 6 CPU Scheduling CPU Scheduler Queuing Diagram for Scheduling: Copyright @ 2009 Pearson Education, Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 7 CPU Scheduling CPU Scheduler Short Term Scheduler: Selects from Ready Queue the Process Control Block ( PCB ). Ready Queue can be: FIFO Queue, Priority Queue, a Binary Tree, or Unordered Link List. Preemptive and NonPreemptive Scheduling NonPreemptive or Cooperative: Process keeps the CPU until it releases the CPU or . . . Switch Running State to Waiting State (i.e. I/O Request or Wait for Child Process Termination). Process Terminates. Used by hardware platforms without special timer hardware for realtime clock. Process can monopolize CPU, hang the system. Windows 3.x, Old version of Macintosh Real-Time Operating System Cannot allow real-time event processing to be interrupted. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 8 CPU Scheduling Preemptive and NonPreemptive Scheduling (Cont) Preemptive: Switch Running State to Ready State (i.e. on Interrupt). Switch Waiting State to Ready State (i.e. I/O Completion). Uses special timer hardware for real-time clock. Problems: Process Shared Data: Using semaphores, mutexes, and spinlocks. Kernel critical data guarded from simultaneous use. Process updating critical data is interrupted and second process reads the critical data, which the interrupted process has not completed updating. Interrupt Handlers disable/enable interrupts. Windows 95, Mac OS X, Linux Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 9 CPU Scheduling Scheduling Criteria Criteria for comparing CPU-Scheduling Algorithm: Usually favors one class over another. CPU Utilization Throughput Minimize. Process execution time: Sum of periods waiting to swap into memory, waiting in the Ready Queue, waiting for I/O, and executing on CPU. Waiting Time Maximize. Schedule 10 processes per second ( Interactive Load ) to 1 process per second ( Batch Load ). Turnaround Time Maximize. CPU 40% (lightly loaded) to 90% (heavily loaded). Minimize. Sum of the periods the process spends waiting in the Ready Queue. Response Time Minimize. Measure of time from the submission of a request until the time it takes to start responding. SILICON VALLEY UNIVERSITY Copyright @ 2009 John Wiley & Sons Inc. CONFIDENTIAL 10 CPU Scheduling Scheduling Criteria Criteria for comparing CPU-Scheduling Algorithm (Cont) Short-Term Scheduling Criterias: Optimized towards System Behavior (Real-Time, Time-Sharing, Batch System). User-Oriented: System-Oriented: Behavior of the system as perceived by the user or process. Interactive System: Response Time is elapsed time between the submission of a request until the response appearing as output. Scheduling Policy minimize Response Time: Round-Robin. Switching processes frequently increases overhead, reducing throughput. Effective and efficent utilization of the processor or System Performance. Scheduling Policy minimize Turnaround Time or maximize Throughput: Shortest Job First ( SJF ). Criteria not readily measureable: Predictability: Service provided to users to exhibit same characteristics over time. Variation in Response Time or Turnaround Time mean wide swing in system workloads, needing system tuning to stabilize. Copyright @ 2009 Pearson Education, Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 11 CPU Scheduling Scheduling Algorithms First-Come, First-Served Scheduling FCFS Scheduling is nonpreemptive. Problem for Time-Sharing Systems (needs CPU at regular intervals). Simple algorithm, but long wait time. P1 P2 P1 P2 P3 P3 Average Wait Time = ((P1 = 0) + P2 + P3) / 3 P2 P2 P3 P3 P1 P1 Average Wait Time = (( P2 = 0) + P3 + P1) / 3 Wait Time. Average Wait Time varies on order and CPU bursts. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 12 CPU Scheduling Scheduling Algorithms Shortest-Job-First Scheduling CPU is assigned to the process that has the smallest next CPU burst. Minimum average waiting time for a set of processes. In Short term scheduling, predict the next CPU burst value. Preemptive SJF takes CPU when arrived process burst time is less than the remaining time of current process. NonPreemptive SJF allows current process to complete burst time before scheduling next process based on burst time. Length of CPU burst? Predict value of next CPU burst. P4 P4 P2 P2 P3 P3 P1 P1 Average Wait Time = ((P4 = 0) + P2 + P3 + P1 )/ 4 Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL Wait Time. 13 CPU Scheduling Scheduling Algorithms FirstCome, First-Served Scheduling P1 P2 0 20 40 P5 56 60 Wait Time. P1: 20, P2: 12, P3: 8, P4: 16, P5: 4 Wait Time ( Order of Arrival ). 32 P4 Burst Time: P3 P1: 0, P2: 20, P3: 32, P4: 40, P5: 56 Average Wait Time: 148 / 5 = 29.6 Shortest-Job-First Scheduling P5 0 P3 4 12 P4 24 P1 40 60 Wait Time ( In Ready Queue at Same Time ). P2 P1: 40, P2: 12, P3: 4, P4: 24, P5: 0 Wait Time. Average Wait Time: 80 / 5 = 16 Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 14 CPU Scheduling Scheduling Algorithms Priority Scheduling SJF Algorithm is Priority Scheduling where priority is inverse of predicted next CPU burst ( Lower burst, higher priority ). Preemptive: Compare arriving process priority with current running process priority. Nonpreemptive: Arriving process priority higher than current running process priority placed in front of the Ready Queue. Problem: Indefinite blocking of low priority processes until system lightly loaded. Solution: Increase priority of processes in the Ready Queue ( Aging ). Process/Burst Time (msec)/Priority: P1/10/3, P2/1/1, P3/2/4, P4/1/5, P5/5/2 = 8.2 msec Average P2 0 1 P5 Copyright @ 2009 John Wiley & Sons Inc. 6 P1 16 P3 SILICON VALLEY UNIVERSITY CONFIDENTIAL 18 19 P4 15 CPU Scheduling Scheduling Algorithms Priority Scheduling Numbering Scheme Priority 0 can be highest or lowest Priorities Internal External Importance, Process cost, Process sponsorship. Static Set according to Operating System (i.e. memory requirements, number of open files, number of I/O bursts). Fixed priority when process enter Operating System. Dynamic Changing during processing (i.e. Amount of CPU usage, or aging (Length of time waiting (a solution to starvation)) Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 16 CPU Scheduling Scheduling Algorithms Compare FCFS, SJF, Priority Scheduling FCFS (First-come, First-Served) Non-preemptive. SJF (Shortest Job First) Non-preemptive or Preemptive. When a new (shorter burst) job arrives, can preempt current job or not. Possible process starvation. Priority Non-preemptive or Preemptive. When a new (higher priority job arrives). When a processes priority changes. Possible process starvation. Linux 2.4 Non-realtime nonpreemptive. Linux 2.6 “Real-time” preemptive. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 17 CPU Scheduling Scheduling Algorithms Round-Robin Scheduling Designed for Time-Sharing or Multi-Tasking Operating Systems. Based on First-Come-First-Serve Algorithm, but use Preemption to switch between processes. Preemption: Process will be preempted when quantum expires. Quantum: Time slice (unit of time) in which a process executes. Circular Queue: Process alternates quantum execution time. Average waiting time often long (process has to wait for other processes to complete their quantum). Timer Interrupt when Quantum Expires. Current process placed at end of Circular Queue. Current process completes before Quantum Expires, next process selected, no Interrupt. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 18 CPU Scheduling Scheduling Algorithms Round-Robin Scheduling Order of Queue and CPU Burst P1: 20 P2: 12 P3: 8 P4: 16 P5: 4 Quantum = 4 Process runs until: a) Remaining CPU Burst < Quantum b) Remaining CPU Burst > Quantum Interrupt Context Switch to Next Process in Ready Queue. P4 completes P5 completes P3 completes P2 completes P1 completes P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 0 4 8 12 16 20 24 28 32 36 40 44 Waiting times: Average wait time: 30.4 P1: 60 - 20 = 40 P2: 44 - 12 = 32 P3: 32 - 8 = 24 P4: 56 - 16 = 40 P5: 20 - 4 = 16 Copyright @ 2009 John Wiley & Sons Inc. 48 SILICON VALLEY UNIVERSITY CONFIDENTIAL 52 56 60 19 CPU Scheduling Scheduling Algorithms Round-Robin Scheduling Choosing a Quantum: Time Quantum too large, RR Policy becomes FCFS Policy. Time Quantum too small, RR Policy process time affected by Context Switch time. Context Switch slows the execution of the process. Context Switch approximately less than 10 percent of the CPU time. Typically Context switch < 10 microseconds. But, more Context Switch, longer process has to wait for the CPU again. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 20 CPU Scheduling Scheduling Algorithms Round-Robin Scheduling Context Switch Process Time = 10 Copyright @ 2009 John Wiley & Sons Inc. Quantum Context Switches 12 0 6 1 1 9 SILICON VALLEY UNIVERSITY CONFIDENTIAL 21 CPU Scheduling Scheduling Algorithms Sample distribution based on Nonpreemptive FCS and SFJ. Preemptive RR. Response time Measure of time from the submission of a request until the time it takes to start responding. Assume all jobs submitted at same time, in order given. Turnaround time Process execution time. Time interval from submission of process until completion of process. Assume all jobs submitted at same time SJF P5 P3 4 RR P2 P4 12 24 P1 40 60 P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 4 Copyright @ 2009 John Wiley & Sons Inc. 8 12 16 20 24 28 32 36 40 SILICON VALLEY UNIVERSITY CONFIDENTIAL 44 48 52 56 60 22 CPU Scheduling Scheduling Algorithms Sample Response Time Calculation (Measure of time from the submission of a request until the time it takes to start responding). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 23 CPU Scheduling SJF P5 P3 4 Scheduling Algorithms P2 12 P4 24 P1 40 60 Sample Response Time Calculation (Measure of time from the submission of a request until the time it takes to start responding). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 24 RR CPU Scheduling P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 4 8 12 16 20 24 28 32 36 40 44 48 Scheduling Algorithms Sample Response Time Calculation (Measure of time from the submission of a request until the time it takes to start responding). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 52 56 25 60 CPU Scheduling Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait periods). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 26 SJF CPU Scheduling P5 P3 4 P2 12 P4 24 P1 40 60 Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait periods). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 27 RR CPU Scheduling P1 P2 P3 P4 P5 P1 P2 P3 P4 P1 P2 P4 P1 P4 P1 4 8 12 16 20 24 28 32 36 40 44 48 52 56 Scheduling Algorithms Sample Turnaround Time Calculation (Process burst execution time and sum of wait periods). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 28 60 CPU Scheduling Scheduling Algorithms Performance Characteristics of Scheduling Algorithms Round Robin ( RR ) Average waiting time, often high, causing above average Turnaround Time. Quantum provides good response time. Turnaround Time good when CPU bursts shorter than Quantum. Quantum too long, approach FCFS Policy behavior. Quantum too short, causes additional Context Switch overhead, Quantum 10 to 100 milliseconds. Important for Interactive or Timesharing. Shortest-Job-First ( SJF ). Best average waiting time, causing best Turnaround Time. CPU burst length or estimates difficult to predict. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 29 CPU Scheduling Scheduling Algorithms Processes are classified into different process types. Multilevel Queue Scheduling Partition Ready Queue into several separate queues. Queues are defined based on process property: interactive process type, background process type, process priority. Each queue has its own Scheduling Algorithm. Time-Sharing Queue scheduled by RR Algorithm. Batch Queue scheduled by FCFS Algorithm. Each queue has priority: No process in the lower priority queues will run until the higher priority queues has completed. When process in higher priority queue is received, the lower priority queue processing is preempted. Prevent Starvation, Time-Slice between queues, each queue has % of CPU. Inflexible: Process assigned to queue, process “type” does not change. System Processes Interactive Processes Batch Processes Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 30 CPU Scheduling Scheduling Algorithms Multilevel Feedback Queue Scheduling Multilevel Queues, but allow process to move between queues. Process exceeds quantum, placed in lower priority queue. Process not getting CPU time, placed in higher priority queue. Defined with: The number of queue. Scheduling algorithm for each queue. Policy to upgrade a process to a higher-priority queue. Policy to downgrade a process to a lower-priority queue. Policy to determine which queue to place a process. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 31 CPU Scheduling Scheduling Algorithms Multilevel Feedback Queue Scheduling Multilevel Queue (Fixed) Copyright @ 2009 John Wiley & Sons Inc. Multilevel Feedback Queue (Movement) SILICON VALLEY UNIVERSITY CONFIDENTIAL 32 CPU Scheduling Thread Scheduling User-level and Kernel-level Threads User-level thread mapped to kernel-level thread. Operating System schedules kernel-level threads. Process-Contention Scope ( PCS ) Scheduling Lightweight Process ( LWP ) User-level threads scheduled by the User-level Thread Library. Kernel scheduler is NOT involved. The User-level thread mapped to Kernel-level thread for scheduling by Operating System ( One-to-One, Many-to-One, Many-to-Many ). PTHREAD SCOPE PROCESS schedules threads using PCS. System-Contention Scope ( SCS ) Scheduling Kernel-level threads scheduled by the Operating System. PTHREAD SCOPE SYSTEM schedules threads using SCS. LWP creates/bind User-level thread to LWP (One-to-One policy). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 33 CPU Scheduling Thread Scheduling #include <pthread.h> #include <stdio.h> int main(int argc, char *argv[]) ... /* set the scheduling algorithm to PCS or SCS */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* create the threads */ for (i = 0; i < 5; i++) pthread create(&tid[i],&attr,runner,NULL); /* now join on each thread */ for (i = 0; i < 5; i++) pthread join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { /* do some work ... */ pthread exit(0); } Copyright @ 2009 John Wiley & Sons Inc. Sets the attribute set for the thread as Process or System Contention scope. This attribute set to System Contention. Create the thread based on the attributes. Wait for the threads to complete. NOTE: Contention Scopes are Operating System Dependent. Linux and MAC OS X Operating Systems allow only PTHREAD_SCOPE_SYSTEM (One-to-One Mapping). SILICON VALLEY UNIVERSITY CONFIDENTIAL 34 CPU Scheduling Multiple-Processor Scheduling Processor Affinity Cache Memory: Process prefers running on a specific CPU. Keep Cache Memory Validated. Soft Affinity: Operating System has policy to keep process on same CPU, but not guaranteed. Hard Affinity: Process cannot migrate to other CPU. Linux, Solaris. Main Memory Architecture: Non-Uniform Memory Access (NUMA) when memory access speed non-uniform. CPU and Memory Affinity. CPU Slow Access ( NUMA ) CPU Fast Access Memory Copyright @ 2009 John Wiley & Sons Inc. Fast Access Memory SILICON VALLEY UNIVERSITY CONFIDENTIAL 35 CPU Scheduling Multiple-Processor Scheduling Load Balancing Keep CPU processing balanced. Distributing processes between CPUs can counteracts Processor Affinity ( Imbalance determined by queue threshold ). Push Migration: Linux/FreeBSD Only when processors have separate CPU Ready Queues. Special process checks CPU Ready Queues and redistributes processes by pushing process from one CPU Ready Queue to another. Pull Migration: Linux/FreeBSD Idle processor pulls a waiting process from a CPU’s Ready Queue. Ready Queue CPU Copyright @ 2009 John Wiley & Sons Inc. Migration Thread Ready Queue Every 200 Milliseconds CPU SILICON VALLEY UNIVERSITY CONFIDENTIAL 36 CPU Scheduling Multicore Processors Scheduling Multiple Processor Cores on Same Physical Chip Each core separate set of registers. Multicore SMPs are faster and consume less power than single core multiprocessor SMPs. Multithreading processor cores: Two or more hardware threads assigned to each core. Memory Stall: A hardware thread will block when waiting for I/O to arrive or write, cache memory miss. Computer Cycle Allows another hardware thread to use the CPU. Memory Stall Cycle Thread 0 Thread 1 C C M C M C M M C M C M C C Time Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 37 CPU Scheduling Multicore Processors Scheduling Each hardware thread is a logical processor. Dual-core dual-threaded CPU has four logical processors. Multithread Processor Implementation Coarse-grained: Hardware thread executes until memory stall ( or long-latency event ) occurs. Processor now switch to another hardware thread. Cost of thread switching is high ( instruction pipeline has to be flushed ). Fine-grained ( Interleaved ): Hardware thread switches at boundary of instruction cycle. All interleaved multithreading processor has hardware support for thread switching and is fast. Two Levels of Scheduling: Scheduling Algorithm by Operating System to select software thread to run. Each core decides which hardware thread to run. Round Robin (UltraSPARC T1). Events to trigger thread switch given priority (Intel Itanium). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 38 CPU Scheduling Virtualization and Scheduling Virtualization Software Schedules Physical CPU (Host OS) for Each Virtual Machine (Guest OS). Multiple Scheduling Layers All Sharing CPU Cycle Host Operating System Scheduling. Virtualization Software Scheduling Virtual Machine. Virtual Machine Operating System Running Own Scheduler. 100 Millisecond Virtual CPU time might take 1 second. Poor response times to uses logged into Virtual Machine. Time-of-day clocks are incorrect, timers take longer to trigger. Make a Virtual Machine scheduling algorithm appear poor. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 39 CPU Scheduling Solaris Operating System Priority-Based Thread Scheduling Thread belongs to one of six classes: each Class maintains different priorities and Scheduling Algorithm. Time Sharing Class Default Class. Dynamic alters priorities using multilevel feedback queue. Inverse Relationship between priorities and time slices (quantum). Higher priority, lower time slice. Good Response time for Interactive processes and good throughput for CPU-bound processes. Interactive Class Similar Scheduling Policy as Time Sharing Class. Used by KDE or GNOME Window Managers for higher priority. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 40 CPU Scheduling Solaris Operating System Priority-Based Thread Scheduling Real Time Class System Class Executes kernel threads only (Scheduler and Paging Daemon). Priority fixed. Fair Share Highest Priority. Guaranteed Response within bounded time period. Use CPU shares (not priorities), indicate entitlement to CPU. Solaris 9 (One-to-One Model). Fixed Priority Same priority range as Time-Sharing Class, but not dynamically adjusted. Solaris 9 (One-to-One Model). Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 41 Lowest Priority has highest Time Quantum CPU Scheduling Solaris Operating System Time-Sharing and Interactive Priority Time Quantum New Priority when used entire Quantum. Threads Time Quantum Expired Return From Sleep 0 200 0 50 5 200 0 50 10 160 0 15 160 5 51 New Priority when NOT used entire Quantum51 (waiting for I/O). 20 120 10 52 25 120 15 52 30 80 20 53 35 80 25 54 40 40 30 55 45 40 35 56 50 40 40 58 55 40 45 58 20 49 59 Copyright @ 2009 John Wiley & Sons59 Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 42 CPU Scheduling Solaris Operating System Highest 169 Scheduling Classes Interrupt Threads First 159 RealTime (RT) Threads Global Priority Scheduling Order 99 System (SYS) Threads 59 Lowest Copyright @ 2009 John Wiley & Sons Inc. Fair Share (FSS) Threads Fixed Priority (FX) Threads TimeShare (TS) Threads Interactive (IA) Threads 0 SILICON VALLEY UNIVERSITY CONFIDENTIAL Last 43 CPU Scheduling Windows XP Operating System Priority-Based, Preemptive Scheduling Scheduling on the basis of threads Highest priority thread always be dispatched, and runs until: Preempted by a higher priority thread Terminates Time quantum ends Makes a blocking system call (e.g., I/O) 32 priorities, each with own queue: Mem. Mgmt.: 0; Variable: 1 to 15; Real-Time: 16 to 31 Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 44 CPU Scheduling Windows XP Operating System Within Priority Class contains Relative Priority All Classes Except Real-Time are Variable Thread Quantum Runs Out: Lower Priority. Thread Released From Wait State: Raise Priority. Priority Classes Relative Priority Real-Time High Above Normal Normal Below Normal Idle Priority Time-Critcal 31 15 15 15 15 15 Highest 26 15 12 10 8 6 Above Normal 25 14 11 9 7 5 Normal 24 13 10 8 6 4 Below Normal 23 12 9 7 5 3 Lowest 22 11 8 6 4 2 Idle 16 1 1 1 1 1 Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 45 CPU Scheduling Linux Operating System Preemptive, Priority-Based Scheduler Real-Time Priority Range: 0 – 99 Nice Range: 100 - 140 Relative Priority Highest Numeric Priority 0 Time Quantum 200 MSec Higher Priority Tasks are assigned longer Time Quantums. Real-Time Tasks 99 100 Other Tasks Lowest Copyright @ 2009 John Wiley & Sons Inc. 140 SILICON VALLEY UNIVERSITY CONFIDENTIAL 10 MSec 46 CPU Scheduling Linux Operating System Real-Time Tasks Assigned Static Priorities Other Tasks Has Dynamic Priorities Nice Value and how long task is sleeping. Longer sleep time has higher priority adjustments ( typically I/O bound tasks ). Shorter sleep time has lower priority adjustments ( typically CPU-bound tasks ). Runqueue Data Structure per CPU Active Priority Array: All tasks with time remaining in Quantum. Expired Priority Array: All tasks with expired Quantum. Scheduler selects highest priority task from the Active Priority Array. When Active Priority Array is empty (transitioned to Expired Priority Array or in Wait Queues), arrays are swapped. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 47 CPU Scheduling Linux 2.6 Scheduler Algorithm runqueue Active [0] [1] [2] ... [140] Expired [0] [1] [2] ... [140] Priority 140 Bitmap 1 0 Task 3 Task 2 Task 1 Task 4 Task 5 Task 6 Task 7 Task 68 Migration Thread Fall 2015 0 ... 3 2 1 ... 0 0 1 ... Task N Task M ... Priority Arrays exchanged when all tasks have executed. Task X Task Z Used to Load Balance when CPU heavily utilized. Tries to distribute some processes. SILICON VALLEY UNIVERSITY CONFIDENTIAL 48 CPU Scheduling Algorithm Evaluation Methods Criteria: CPU Utilization, Response Time, Throughput Analytic Evaluation: Uses given algorithm and system workload, produce a formula that evaluates performance of algorithm. Deterministic Modeling: Predetermined workload ( i.e. measure wait time for FCFS, SJF, RR algorithms). Problem: Requires exact numbers and ONLY applies to specific cases. Queueing Modeling: Estimate arrival rate and service rate, compute utilization, average queue length, average wait time. Problem: Approximations of real systems and may not be accurate. Simulations: Programming model of computer system, software data structures represents Operating System components. Data ( i.e. CPU burst times, arrivals, departures) randomly generated (i.e. uniform, Poisson) or empirical. Problem: Designing simulator is expensive. Only completely accurate way to evaluate a scheduling algorithm is to implement it and observe the behavior in the Operating System. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 49 CPU Scheduling Summary CPU Scheduling selects a waiting process from Ready Queue. Scheduling Algorithms FCFS: Simple, but Short Processes Waits for Long Processes. SJF: Shortest Average Wait Time, but Predicting Next CPU Burst is Difficult, and if Preemptive, can cause Starvation of Long Jobs. RR: Time-Shared (Interactive) System, but Selecting Quantum is Problematic. Multilevel Queue Algorithms: Uses different algorithms for classes. Multilevel Feedback Queues: Moves process between queues. Multi-Processor Scheduling: Processor Affinity, Load Balancing. Operating Systems: Solaris: Priority-based Thread Scheduling with 6 Priority Classes. Windows XP: Priority-based Thread Scheduling with 32 Priority Queues. Linux: Priority-based with Real-Time and Other Priority Ranges. Algorithm Evaluation: Deterministic Modeling, Queueing Models, Simulations. Copyright @ 2009 John Wiley & Sons Inc. SILICON VALLEY UNIVERSITY CONFIDENTIAL 50