2 Dispatcher 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 restart that program Dispatch latency time it takes for the dispatcher to stop one process and start another running. Preemptive vs Non-Preemptive Non-Preemptive Scheduling: once the CPU is allocated to a process, the process keeps the CPU until it terminates or it blocks in a I/O operations and switches to the waiting state (used by Windows.). Preemptive Scheduling: when a new process arrives it can take the CPU out of the running process. Scheduling Criteria CPU utilization keep the CPU as busy as possible. Throughput # 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 has been waiting in the ready queue. Response time amount of time it takes from when a request was submitted until the first response is produced. Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

4 Shortest-Process-Next (SPN) Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. Two schemes: nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst. preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time (SRT). SPN is optimal gives minimum average waiting time for a given set of processes. Difficulty: how to know the length of the next CPU request. Non-Preemptive SPN Process Arrival Time Burst Time P P.0 P.0 P.0 SPN (non-preemptive) 0 P P P Average waiting time = ( )/ = P Example of Preemptive SRT Process Arrival Time Burst Time P P.0 P.0 P.0 SRT (preemptive) 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). Preemptive Nonpreemptive 0 P P P P P P 7 Average waiting time = ( )/ = 6 Problem Starvation low priority processes may never execute. Solution Aging as time progresses increase the priority of the process.

5 Priorities Scheduler will always choose a process of higher priority over one of lower priority. Have multiple ready queues to represent each level of priority. Lower-priority may suffer from starvation allow a process to change its priority based on its age or execution history Starvation: interesting note... When the system managers shutdown the IBM 709 at MIT in 97, they found a low-priority process that had been submitted in 967 and had not yet run... IBM 709 Round Robin (RR) Each process gets a small unit of CPU time (time quantum), usually 0-00 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue and the time quantum is q, then each process gets /n of the CPU time in chunks of at most q time units at once. No process waits more than (n-)q time units. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high.

6 Example of RR with Time Quantum = 0 Process Burst Time P P 7 P 68 P The Gantt chart is: Time Quantum and Context Switch Time P P P P P P P P P P Typically, higher average turnaround than SJF, but better response. Turnaround Time Varies With The Time Quantum Highest Response Ratio Next (HRRN) Choose next process with the highest ratio (R): R = time spent waiting + expected service time expected service time This algorithm accounts for the age of the process. Shorted jobs are favored (smaller denominator) But aging without service (waiting time) increases R. 6

7 Multilevel Queue Ready queue is partitioned into separate queues: - foreground (interactive) - background (batch) Each queue has its own scheduling algorithm, foreground RR background FCFS Scheduling must be done between the queues. Fixed priority scheduling; (i.e., serve all processes from foreground; then from background). Possibility of starvation. Time slice each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR; 0% to background in FCFS Multilevel Queue Scheduling Multilevel Feedback Queue 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 7

8 Multilevel Feedback Queues Multilevel Feedback Queue Three queues: Q 0 time quantum 8 milliseconds Q time quantum 6 milliseconds Q FCFS Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q. At Q job is again served FCFS and receives 6 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q. Scheduling Policies Process Scheduling Example 8

10 Shortest Remaining Time (SRT) Highest Response Ratio Next (HRRN) Preemptive version of shortest process next policy. Must estimate processing time. Choose next process with the highest ratio: R = time spent waiting + expected service time expected service time Multilevel Feedback Penalize jobs that have been running longer. 0

CPU Scheduling Multiprogrammed OS Efficient Use of Processor By switching between jobs Thread scheduling and process scheduling often used interchangeably Which is done depends on implementation of O/S.

Basic Concepts P CPU Scheduling CSCI 315 Operating Systems Design Department of Computer Science Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating

CPU Scheduling James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Scheduling Concepts

CPU Scheduling Dr. Yingwu Zhu Overview In discussing process/threads management and synchronization, we talked about context switching among processes/threads on the ready queue But we have not glossed

Scheduling Objectives CPU SCHEDULING Enforcement of fairness in allocating resources to processes Enforcement of priorities Make best use of available system resources Give preference to processes holding

CPU Scheduling Algorithms Notice: The slides for this lecture have been largely based on those from the course text Operating Systems Concepts, 9th ed., by Silberschatz, Galvin, and Gagne. Many, if not

Background Processor Scheduling The previous lecture introduced the basics of concurrency Processes and threads Definition, representation, management We now understand how a programmer can spawn concurrent